Skip to content
Exponential and Linear back off for Go
Go
Branch: master
Clone or download
Latest commit f7b9f7e May 17, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Godeps
.gitignore
LICENSE Keep things DRY with interface usage. Add ExponentialFullJitter algor… Mar 15, 2015
README.md
backoff.go
backoff_test.go
controlci.yml Add CI file May 17, 2017

README.md

What is it?

go-backoff implements linear, exponential and exponential with full jitter back off algorithms.

Usage

Instantiate and use a Backoff instance for the lifetime of a go routine. Call backoff.Backoff() to put the routine to sleep for the desired amount of time. Use backoff.Reset() to reset counters back to 0.

  // Back off linearly, starting at 250ms, capping at 16 seconds
  linear = backoff.NewLinear(250*time.Millisecond, 16*time.Second)
  // Back off exponentially, starting at 5 seconds, capping at 320 seconds
  exp = backoff.NewExponential(5*time.Second, 320*time.Second)
  // Back off exponentially, starting at 1 minute, with no cap
  expt = backoff.NewExponential(time.Minute, 0)
  // Back off between 0 and exponentially, starting at 30 seconds, capping at 10 minutes
  expj = backoff.NewExponentialFullJitter(30*time.Second, 10*time.Minute)

  ...

  for {
    err := tryDoThing()
    if err != nil {
      glog.Debugf("Backing off %d second(s)", exp.NextDuration/time.Second)
      exp.Backoff()
    } else {
      exp.Reset()
      return
    }
  }

Tests

go get github.com/tools/godep
godep restore
go test

PASS
ok    github.com/diggs/go-backoff 6.319s
You can’t perform that action at this time.