terraform/vendor/github.com/cenkalti/backoff
Edward Muller 8a9b0f6628 fix up some other badly saved deps.
237e257f37 was probably done with an older
version of godep
2016-02-24 14:27:26 -08:00
..
.gitignore
.travis.yml
LICENSE
README.md
backoff.go
exponential.go
retry.go
ticker.go

README.md

Exponential Backoff GoDoc Build Status

This is a Go port of the exponential backoff algorithm from Google's HTTP Client Library for Java.

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.

How To

We define two functions, Retry() and RetryNotify(). They receive an Operation to execute, a BackOff algorithm, and an optional Notify error handler.

The operation will be executed, and will be retried on failure with delay as given by the backoff algorithm. The backoff algorithm can also decide when to stop retrying. In addition, the notify error handler will be called after each failed attempt, except for the last time, whose error should be handled by the caller.

// An Operation is executing by Retry() or RetryNotify().
// The operation will be retried using a backoff policy if it returns an error.
type Operation func() error

// Notify is a notify-on-error function. It receives an operation error and
// backoff delay if the operation failed (with an error).
//
// NOTE that if the backoff policy stated to stop retrying,
// the notify function isn't called.
type Notify func(error, time.Duration)

func Retry(Operation, BackOff) error
func RetryNotify(Operation, BackOff, Notify)

Examples

See more advanced examples in the godoc.

Retry

Simple retry helper that uses the default exponential backoff algorithm:

operation := func() error {
    // An operation that might fail.
    return nil // or return errors.New("some error")
}

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

// Operation is successful.
return nil

Ticker

operation := func() error {
    // An operation that might fail
    return nil // or return errors.New("some error")
}

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

var err error

// Ticks will continue to arrive when the previous operation is still running,
// so operations that take a while to fail could run in quick succession.
for range ticker.C {
    if err = operation(); err != nil {
        log.Println(err, "will retry...")
        continue
    }

    ticker.Stop()
    break
}

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

// Operation is successful.
return nil

Getting Started

# install
$ go get github.com/cenkalti/backoff

# test
$ cd $GOPATH/src/github.com/cenkalti/backoff
$ go get -t ./...
$ go test -v -cover