open-vault/vendor/github.com/jeffchao/backoff
Jeff Mitchell 9ebc57581d
Switch to go modules (#6585)
* Switch to go modules

* Make fmt
2019-04-13 03:44:06 -04:00
..
.gitignore Switch to go modules (#6585) 2019-04-13 03:44:06 -04:00
.travis.yml Switch to go modules (#6585) 2019-04-13 03:44:06 -04:00
LICENSE adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
README.md adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
backoff.go adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
exponential.go adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
fibonacci.go adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
mild.go adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
pfb.go adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00
pleb.go adding gcpkms secrets engine (#784) 2018-10-22 23:39:25 -07:00

README.md

backoff

Build Status GoDoc

Backoff algorithms are used to space out repeated retries of the same block of code. By gradually decreasing the retry rate, backoff algorithms aim to avoid congestion. This Go backoff library provides a set of backoff algorithms well-known in the computer networks research community. These algorithms are acceptable for use in generalized computation. All algorithms use a uniform distribution of backoff times.

This library provides the following backoff algorithms:

  1. Exponential
  2. Fibonacci
  3. MILD
  4. PLEB
  5. PFB

each with properties:

type <all backoff algorithms> struct {
        Retries    int
        MaxRetries int
        Delay      time.Duration
        Interval   time.Duration    // time.Second, time.Millisecond, etc.
        Slots      []time.Duration
}

all of which are based off a Backoff interface defined as:

type Backoff interface {
        Next() bool
        Retry(func() error) error  // Surface the resulting error to the user.
        Reset()
}

so you may define additional backoff algorithms of your choice.

Exponential Backoff

Gradually decrease the retry rate in an exponential manner with base 2. The algorithm is defined as n = 2^c - 1 where n is the backoff delay and c is the number of retries.

Example usage:

e := backoff.Exponential()
e.Interval = 1 * time.Millisecond
e.MaxRetries = 5

fooFunc := func() error {
        // Do some work here
}

err := e.Retry(fooFunc)
e.Reset()

Fibonacci Backoff

Gradually decrease the retry rate using a fibonacci sequence. The algorithm is defined as n = fib(c - 1) + fib(c - 2); f(0) = 0, f(1) = 1; n >= 0 where n is the backoff delay and c is the retry slot.

f := backoff.Fibonacci()
f.Interval = 1 * time.Millisecond
f.MaxRetries = 5

fooFunc := func() error {
        // Do some work here
}

err := f.Retry(fooFunc)
f.Reset()

Additionally, the FibonacciBackoff struct exposes its delay slots in the form of a slice of time.Duration.

log.Printf("%+v", f.Slots)

MILD - Multiplicative Increase and Linear Decrease

Gradually increase the retry delay by a factor of 1.5. However, upon successful transmission, decrement the index of the delay slots so that the current delay is the previous value. The retry mechanism thus will not result in a success until the slot index has been decremented to 0. Conversely, the retry mechanism will fail as usual, upon reaching the failed maximum number of retries. The algorithm is defined as follows: n = min(1.5, n, len(slots)) upon failure; n = max(slots(c) - 1, 0) upon success; n(0) = 0, n(1) = 1 where n is the backoff delay, c is the retry slot, and slots is an array of retry delays.

f := backoff.MILD()
f.Interval = 1 * time.Millisecond
f.MaxRetries = 5

fooFunc := func() error {
        // Do some work here
}

err := f.Retry(fooFunc)
f.Reset()

Author

Jeff Chao, @thejeffchao, http://thejeffchao.com