package retry
Import Path
github.com/avast/retry-go (on go.dev)
Dependency Relation
imports 6 packages, and imported by one package
Involved Source Files
options.go
Simple library for retry mechanism
slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
SYNOPSIS
http get with retry:
url := "http://example.com"
var body []byte
err := retry.Do(
func() error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
},
)
fmt.Println(body)
[next examples](https://github.com/avast/retry-go/tree/master/examples)
SEE ALSO
* [giantswarm/retry-go](https://github.com/giantswarm/retry-go) - slightly complicated interface.
* [sethgrid/pester](https://github.com/sethgrid/pester) - only http retry for http calls with retries and backoff
* [cenkalti/backoff](https://github.com/cenkalti/backoff) - Go port of the exponential backoff algorithm from Google's HTTP Client Library for Java. Really complicated interface.
* [rafaeljesus/retry-go](https://github.com/rafaeljesus/retry-go) - looks good, slightly similar as this package, don't have 'simple' `Retry` method
* [matryer/try](https://github.com/matryer/try) - very popular package, nonintuitive interface (for me)
BREAKING CHANGES
3.0.0
* `DelayTypeFunc` accepts a new parameter `err` - this breaking change affects only your custom Delay Functions. This change allow [make delay functions based on error](examples/delay_based_on_error_test.go).
1.0.2 -> 2.0.0
* argument of `retry.Delay` is final delay (no multiplication by `retry.Units` anymore)
* function `retry.Units` are removed
* [more about this breaking change](https://github.com/avast/retry-go/issues/7)
0.3.0 -> 1.0.0
* `retry.Retry` function are changed to `retry.Do` function
* `retry.RetryCustom` (OnRetry) and `retry.RetryCustomWithOpts` functions are now implement via functions produces Options (aka `retry.OnRetry`)
Package-Level Type Names (total 7)
func BackOffDelay(n uint, _ error, config *Config) time.Duration
func FixedDelay(_ uint, _ error, config *Config) time.Duration
func RandomDelay(_ uint, _ error, config *Config) time.Duration
DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts.
func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc
func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc
func DelayType(delayType DelayTypeFunc) Option
var DefaultDelayType
Error type represents list of errors in retry
Error method return string representation of Error
It is an implementation of error interface
WrappedErrors returns the list of errors that this Error is wrapping.
It is an implementation of the `errwrap.Wrapper` interface
in package [errwrap](https://github.com/hashicorp/errwrap) so that
`retry.Error` can be used with that library.
Error : error
Function signature of OnRetry function
n = count of attempts
func OnRetry(onRetry OnRetryFunc) Option
Option represents an option for retry.
func Attempts(attempts uint) Option
func Context(ctx context.Context) Option
func Delay(delay time.Duration) Option
func DelayType(delayType DelayTypeFunc) Option
func LastErrorOnly(lastErrorOnly bool) Option
func MaxDelay(maxDelay time.Duration) Option
func MaxJitter(maxJitter time.Duration) Option
func OnRetry(onRetry OnRetryFunc) Option
func RetryIf(retryIf RetryIfFunc) Option
func Do(retryableFunc RetryableFunc, opts ...Option) error
Package-Level Functions (total 16)
Attempts set count of retry
default is 10
BackOffDelay is a DelayType which increases delay between consecutive retries
CombineDelay is a DelayType the combines all of the specified delays into a new DelayTypeFunc
Context allow to set context of retry
default are Background context
example of immediately cancellation (maybe it isn't the best example, but it describes behavior enough; I hope)
ctx, cancel := context.WithCancel(context.Background())
cancel()
retry.Do(
func() error {
...
},
retry.Context(ctx),
)
Delay set delay between retry
default is 100ms
DelayType set type of the delay between retries
default is BackOff
func Do(retryableFunc RetryableFunc, opts ...Option) error
FixedDelay is a DelayType which keeps delay the same through all iterations
IsRecoverable checks if error is an instance of `unrecoverableError`
return the direct last error that came from the retried function
default is false (return wrapped errors with everything)
MaxDelay set maximum delay between retry
does not apply by default
MaxJitter sets the maximum random Jitter between retries for RandomDelay
OnRetry function callback are called each retry
log each retry example:
retry.Do(
func() error {
return errors.New("some error")
},
retry.OnRetry(func(n uint, err error) {
log.Printf("#%d: %s\n", n, err)
}),
)
RandomDelay is a DelayType which picks a random delay up to config.maxJitter
RetryIf controls whether a retry should be attempted after an error
(assuming there are any retry attempts remaining)
skip retry if special error example:
retry.Do(
func() error {
return errors.New("special error")
},
retry.RetryIf(func(err error) bool {
if err.Error() == "special error" {
return false
}
return true
})
)
By default RetryIf stops execution if the error is wrapped using `retry.Unrecoverable`,
so above example may also be shortened to:
retry.Do(
func() error {
return retry.Unrecoverable(errors.New("special error"))
}
)
Unrecoverable wraps an error in `unrecoverableError` struct
Package-Level Variables (total 8)
var DefaultAttempts uint var DefaultDelay time.Duration var DefaultOnRetry func(n uint, err error) var DefaultRetryIf func(err error) bool
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |