Backoff strategy written in Go
Backoff is an implementation of the popular backoff strategy. It is written in Go.
go get github.com/indrasaputra/backoff
There are two actual backoff implementations. The first one is ConstantBackoff
and the second one is ExponentialBackoff
.
package main
import (
"time"
"github.com/indrasaputra/backoff"
)
func main() {
b := &backoff.ConstantBackoff{
BackoffInterval: 200 * time.Millisecond,
JitterInterval: 50 * time.Millisecond,
}
// use NextInterval() to get the next interval
interval := b.NextInterval()
// use Reset() to reset the backoff
b.Reset()
}
If you want to use ExponentialBackoff
, simply follow this code
package main
import (
"time"
"github.com/indrasaputra/backoff"
)
func main() {
b := backoff.ExponentialBackoff{
BackoffInterval: 300 * time.Millisecond,
JitterInterval: 100 * time.Millisecond,
MaxInterval: 3 * time.Second,
Multiplier: 2,
}
}