https://github.com/adamslevy/retry
Golang package for retrying operations with composable retry policies.
https://github.com/adamslevy/retry
Last synced: 8 months ago
JSON representation
Golang package for retrying operations with composable retry policies.
- Host: GitHub
- URL: https://github.com/adamslevy/retry
- Owner: AdamSLevy
- License: mit
- Created: 2019-10-17T08:39:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-17T18:43:33.000Z (over 6 years ago)
- Last Synced: 2025-04-14T11:06:28.904Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# retry [](http://godoc.org/github.com/AdamSLevy/retry) [](https://goreportcard.com/report/github.com/AdamSLevy/retry) [](https://travis-ci.org/AdamSLevy/retry) [](https://coveralls.io/github/AdamSLevy/retry?branch=master)
Package retry provides a reliable, simple way to retry operations.
`go get -u github.com/AdamSLevy/retry`
```golang
// 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(err error, attempt uint, 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(err error) error {
if errors.Is(err, errors.New("unrecoverable err")) {
return retry.ErrorStop(err)
}
return err
}
// A context.Context may be passed so that waits can be canceled.
var ctx = context.TODO()
err := retry.Run(ctx, policy, filter, notify, func() error {
return tryWork(ctx)
})
if err != nil {
return
}
```
This package was inspired by
[github.com/cenkalti/backoff](https://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.