https://github.com/marselester/backoff
Go port of the exponential backoff algorithm recommended in Polly project.
https://github.com/marselester/backoff
backoff golang retry
Last synced: about 1 year ago
JSON representation
Go port of the exponential backoff algorithm recommended in Polly project.
- Host: GitHub
- URL: https://github.com/marselester/backoff
- Owner: marselester
- License: mit
- Created: 2022-08-07T01:53:22.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-07T02:00:57.000Z (almost 4 years ago)
- Last Synced: 2024-05-02T00:41:38.883Z (about 2 years ago)
- Topics: backoff, golang, retry
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# backoff 🤚 [](https://pkg.go.dev/github.com/marselester/backoff) [](https://goreportcard.com/report/github.com/marselester/backoff)
This package generates sleep durations in an exponentially backing-off,
jittered manner, making sure to mitigate any correlations.
It is a Go port of the exponential backoff algorithm
[recommended in Polly project](https://github.com/Polly-Contrib/Polly.Contrib.WaitAndRetry#new-jitter-recommendation).
> a new jitter formula characterised by very smooth and even distribution of retry intervals,
> a well-controlled median initial retry delay, and broadly exponential backoff.
```go
package main
import (
"context"
"fmt"
"github.com/marselester/backoff"
)
func main() {
ctx := context.Background()
r := backoff.NewDecorrJitter(
backoff.WithMaxRetries(5),
)
err := backoff.Run(ctx, r, func(attempt int) error {
fmt.Printf("%d attempt\n", attempt)
return fmt.Errorf("timeout")
})
if err != nil {
fmt.Printf("all the attempts failed: %v\n", err)
}
}
```
There are 6 calls to a function: the first attempt and 5 retries.
```
1 attempt
2 attempt
3 attempt
4 attempt
5 attempt
6 attempt
all the attempts failed: timeout
```