Package retry provides a reliable, simple way to retry operations.
go get -u github.com/AdamSLevy/retry
// The provided policies can be composed to a custom Policy.policy:= retry.LimitTotal{25*time.Minute,
retry.LimitAttempts{10,
retry.Max{10*time.Minute,
retry.Randomize{.5,
retry.Exponential{5*time.Second, 2}}}}}
// A notify function is called before each wait period.notify:=func(errerror, attemptuint, d time.Duration) {
fmt.Printf("Attempt %v returned %v. Retrying in %v...\n",
attempt, err, d)
}
// A filter function can be used to omit or wrap certain errors to tell// Run to stop immediately.filter:=func(errerror) error {
iferrors.Is(err, errors.New("unrecoverable err")) {
returnretry.ErrorStop(err)
}
returnerr
}
// A context.Context may be passed so that waits can be canceled.varctx=context.TODO()
err:=retry.Run(ctx, policy, filter, notify, func() error {
returntryWork(ctx)
})
iferr!=nil {
return
}This package was inspired by github.com/cenkalti/backoff but improves on the design by providing Policy types that are composable, re-usable and safe for repeated or concurrent calls to Run.