Skip to content

ChannelMeter/backoff

 
 

Repository files navigation

backoff

GoDoc Build Status

This is a Go port of the exponential backoff algorithm from google-http-java-client.

Exponential backoff is an algorithm that uses feedback to multiplicatively decrease the rate of some process, in order to gradually find an acceptable rate. The retries exponentially increase and stop increasing when a certain threshold is met.

Install

go get github.com/cenkalti/backoff

Example

Simple retry helper that uses exponential back-off algorithm:

operation := func() error {
    // An operation that might fail
}

err := backoff.Retry(operation, backoff.NewExponentialBackOff())
if err != nil {
    // handle error
}

// operation is successfull

Ticker example:

operation := func() error {
    // An operation that may fail
}

b := backoff.NewExponentialBackOff()
ticker := backoff.NewTicker(b)

var err error
for t = range ticker.C {
    if err = operation(); err != nil {
        log.Println(err, "will retry...")
        continue
    }

    ticker.Stop()
    break
}

if err != nil {
    // Operation has failed.
}

// Operation is successfull.

About

The exponential backoff algorithm in Go (Golang).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors