https://github.com/go-playground/backoff
:bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.
https://github.com/go-playground/backoff
auto-tuning backoff exponential-backoff retry
Last synced: 5 months ago
JSON representation
:bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.
- Host: GitHub
- URL: https://github.com/go-playground/backoff
- Owner: go-playground
- License: mit
- Created: 2017-12-31T19:37:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-31T20:34:41.000Z (almost 8 years ago)
- Last Synced: 2025-04-09T01:41:46.000Z (6 months ago)
- Topics: auto-tuning, backoff, exponential-backoff, retry
- Language: Go
- Size: 11.7 KB
- Stars: 12
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Backoff library
=============

[](https://travis-ci.org/go-playground/backoff)
[](https://coveralls.io/github/go-playground/backoff?branch=master)
[](https://goreportcard.com/report/github.com/go-playground/backoff)
[](https://godoc.org/github.com/go-playground/backoff)
Backoff library uses an exponential backoff algorithm to backoff between retries.
What makes this different from other backoff libraries?
1. Simple, by automatically calculating the exponential factor between the min and max backoff times; which properly tunes to your desired values.
2. Provides an optional `AutoTune` function which will adjust the min backoff duration based upon successful backoff duration retries.Why AutoTune?
For long running services that hit external services such as writing to a DB or that hit a 3rd party API's, where successful attempts backoff durations can vary over time as load changes. Additionally by using `AutoTune` it should provide an automatic jitter when multiple copies of a service are running.
Example Basic
------------
```go
package mainimport (
"errors"
"time""fmt"
"github.com/go-playground/backoff"
)func main() {
// backoff.New(retries, min, max)
bo := backoff.New(5, time.Second, time.Minute)
defer bo.Close()// retry function and notification function to log failures etc...
bo.Run(func() (bail bool, err error) {
// do something
return false, errors.New("ERR")
}, func(attempt uint16, waiting time.Duration, err error) {
fmt.Printf("Attempt:%d Waiting:%s Error:%s\n", attempt, waiting, err)})
}
```Example AutoTune
------------
```go
package mainimport (
"errors"
"fmt"
"time""os"
"os/signal"
"syscall""math/rand"
"github.com/go-playground/backoff"
)func main() {
// backoff.New(retries, min, max)
bo := backoff.New(5, time.Second, 10*time.Second)
// bo.AutoTune(poll, reset)
bo.AutoTune(30*time.Second, 2*time.Minute)
defer bo.Close()go func() {
for {
time.Sleep(time.Millisecond * 500)
go func() {
bo.Run(func() (bool, error) {
// simulating random success/failure
count := rand.Intn(5)
switch count {
case 1:
return false, errors.New("ERR")
case 2:
return false, errors.New("ERR")
case 3:
return false, nil
case 4:
return false, errors.New("ERR")
default: //case 5:
return false, nil
}
}, func(attempt uint16, waiting time.Duration, err error) {
fmt.Printf("Attempt:%d Waiting: %s Error:%s\n", attempt, waiting, err)
})
}()
}
}()
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
<-s
}
```Requirements
------------
\>= Go v1.9Package Versioning
---------------
I'm jumping on the vendoring bandwagon, you should vendor this package as I will not
be creating different version with gopkg.in like allot of my other libraries.Why? because my time is spread pretty thin maintaining all of the libraries I have + LIFE,
it is so freeing not to worry about it and will help me keep pouring out bigger and better
things for you the community.License
------
Distributed under MIT License, please see license file in code for more details.