package retryimport ()// Function signature of retry if functiontypeRetryIfFuncfunc(error) bool// Function signature of OnRetry function// n = count of attemptstypeOnRetryFuncfunc(n uint, err error)// DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts.typeDelayTypeFuncfunc(n uint, err error, config *Config) time.DurationtypeConfigstruct { attempts uint delay time.Duration maxDelay time.Duration maxJitter time.Duration onRetry OnRetryFunc retryIf RetryIfFunc delayType DelayTypeFunc lastErrorOnly bool context context.Context maxBackOffN uint}// Option represents an option for retry.typeOptionfunc(*Config)// return the direct last error that came from the retried function// default is false (return wrapped errors with everything)func ( bool) Option {returnfunc( *Config) { .lastErrorOnly = }}// Attempts set count of retry// default is 10func ( uint) Option {returnfunc( *Config) { .attempts = }}// Delay set delay between retry// default is 100msfunc ( time.Duration) Option {returnfunc( *Config) { .delay = }}// MaxDelay set maximum delay between retry// does not apply by defaultfunc ( time.Duration) Option {returnfunc( *Config) { .maxDelay = }}// MaxJitter sets the maximum random Jitter between retries for RandomDelayfunc ( time.Duration) Option {returnfunc( *Config) { .maxJitter = }}// DelayType set type of the delay between retries// default is BackOfffunc ( DelayTypeFunc) Option {returnfunc( *Config) { .delayType = }}// BackOffDelay is a DelayType which increases delay between consecutive retriesfunc ( uint, error, *Config) time.Duration {// 1 << 63 would overflow signed int64 (time.Duration), thus 62.constuint = 62if .maxBackOffN == 0 {if .delay <= 0 { .delay = 1 } .maxBackOffN = - uint(math.Floor(math.Log2(float64(.delay)))) }if > .maxBackOffN { = .maxBackOffN }return .delay << }// FixedDelay is a DelayType which keeps delay the same through all iterationsfunc ( uint, error, *Config) time.Duration {return .delay}// RandomDelay is a DelayType which picks a random delay up to config.maxJitterfunc ( uint, error, *Config) time.Duration {returntime.Duration(rand.Int63n(int64(.maxJitter)))}// CombineDelay is a DelayType the combines all of the specified delays into a new DelayTypeFuncfunc ( ...DelayTypeFunc) DelayTypeFunc {const = uint64(math.MaxInt64)returnfunc( uint, error, *Config) time.Duration {varuint64for , := range { += uint64((, , ))if > { = } }returntime.Duration() }}// 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)// }),// )func ( OnRetryFunc) Option {returnfunc( *Config) { .onRetry = }}// 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"))// }// )func ( RetryIfFunc) Option {returnfunc( *Config) { .retryIf = }}// 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),// )func ( context.Context) Option {returnfunc( *Config) { .context = }}
The pages are generated with Goldsv0.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.