Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UntilSucceeded option to improve readability #116

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

Simple library for retry mechanism

slightly inspired by
Slightly inspired by
[Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)

# SYNOPSIS

http get with retry:
HTTP GET with retry:

url := "http://example.com"
var body []byte
Expand All @@ -31,17 +31,17 @@ http get with retry:
if err != nil {
return err
}

return nil
},
)

if err != nil {
// handle error
}

fmt.Println(string(body))

http get with retry with data:
HTTP GET with retry with data:

url := "http://example.com"

Expand All @@ -60,13 +60,14 @@ http get with retry with data:
return body, nil
},
)

if err != nil {
// handle error
}

fmt.Println(string(body))

[next examples](https://github.com/avast/retry-go/tree/master/examples)
[More examples](https://github.com/avast/retry-go/tree/master/examples)

# SEE ALSO

Expand Down Expand Up @@ -241,10 +242,10 @@ used with that library.
#### type OnRetryFunc

```go
type OnRetryFunc func(n uint, err error)
type OnRetryFunc func(attempt uint, err error)
```

Function signature of OnRetry function n = count of attempts
Function signature of OnRetry function

#### type Option

Expand Down Expand Up @@ -366,7 +367,7 @@ skip retry if special error example:
return false
}
return true
}),
})
)

By default RetryIf stops execution if the error is wrapped using
Expand All @@ -378,6 +379,14 @@ By default RetryIf stops execution if the error is wrapped using
}
)

#### func UntilSucceeded

```go
func UntilSucceeded() Option
```
UntilSucceeded will retry until the retried function succeeds. Equivalent to
setting Attempts(0).

#### func WithTimer

```go
Expand Down
10 changes: 8 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
type RetryIfFunc func(error) bool

// Function signature of OnRetry function
// n = count of attempts
type OnRetryFunc func(n uint, err error)
type OnRetryFunc func(attempt uint, err error)

// DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts.
type DelayTypeFunc func(n uint, err error, config *Config) time.Duration
Expand Down Expand Up @@ -60,6 +59,13 @@ func Attempts(attempts uint) Option {
}
}

// UntilSucceeded will retry until the retried function succeeds. Equivalent to setting Attempts(0).
func UntilSucceeded() Option {
return func(c *Config) {
c.attempts = 0
}
}

// AttemptsForError sets count of retry in case execution results in given `err`
// Retries for the given `err` are also counted against total retries.
// The retry will stop if any of given retries is exhausted.
Expand Down
11 changes: 6 additions & 5 deletions retry.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Simple library for retry mechanism

slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
Slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)

# SYNOPSIS

http get with retry:
HTTP GET with retry:

url := "http://example.com"
var body []byte
Expand All @@ -21,17 +21,17 @@ http get with retry:
if err != nil {
return err
}

return nil
},
)

if err != nil {
// handle error
}

fmt.Println(string(body))

http get with retry with data:
HTTP GET with retry with data:

url := "http://example.com"

Expand All @@ -50,13 +50,14 @@ http get with retry with data:
return body, nil
},
)

if err != nil {
// handle error
}

fmt.Println(string(body))

[next examples](https://github.com/avast/retry-go/tree/master/examples)
[More examples](https://github.com/avast/retry-go/tree/master/examples)

# SEE ALSO

Expand Down
Loading