backoff

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2015 License: MIT, MIT Imports: 4 Imported by: 0

README

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

Documentation

Overview

Package backoff implements backoff algorithms for retrying operations.

Also has a Retry() helper for retrying operations that may fail.

Example

This is an example that demonstrates how this package could be used to perform various advanced operations.

It executes an HTTP GET request with exponential backoff, while errors are logged and failed responses are closed, as required by net/http package.

Note we define a condition function which is used inside the operation to determine whether the operation succeeded or failed.

package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

// This is an example that demonstrates how this package could be used
// to perform various advanced operations.
//
// It executes an HTTP GET request with exponential backoff,
// while errors are logged and failed responses are closed, as required by net/http package.
//
// Note we define a condition function which is used inside the operation to
// determine whether the operation succeeded or failed.
func main() error {
	res, err := GetWithRetry(
		"http://localhost:9999",
		ErrorIfStatusCodeIsNot(http.StatusOK),
		NewExponentialBackOff())

	if err != nil {
		// Close response body of last (failed) attempt.
		// The Last attempt isn't handled by the notify-on-error function,
		// which closes the body of all the previous attempts.
		if e := res.Body.Close(); e != nil {
			log.Printf("error closing last attempt's response body: %s", e)
		}
		log.Printf("too many failed request attempts: %s", err)
		return err
	}
	defer res.Body.Close() // The response's Body must be closed.

	// Read body
	_, _ = ioutil.ReadAll(res.Body)

	// Do more stuff
	return nil
}

// GetWithRetry is a helper function that performs an HTTP GET request
// to the given URL, and retries with the given backoff using the given condition function.
//
// It also uses a notify-on-error function which logs
// and closes the response body of the failed request.
func GetWithRetry(url string, condition Condition, bck BackOff) (*http.Response, error) {
	var res *http.Response
	err := RetryNotify(
		func() error {
			var err error
			res, err = http.Get(url)
			if err != nil {
				return err
			}
			return condition(res)
		},
		bck,
		LogAndClose())

	return res, err
}

// Condition is a retry condition function.
// It receives a response, and returns an error
// if the response failed the condition.
type Condition func(*http.Response) error

// ErrorIfStatusCodeIsNot returns a retry condition function.
// The condition returns an error
// if the given response's status code is not the given HTTP status code.
func ErrorIfStatusCodeIsNot(status int) Condition {
	return func(res *http.Response) error {
		if res.StatusCode != status {
			return NewError(res)
		}
		return nil
	}
}

// Error is returned on ErrorIfX() condition functions throughout this package.
type Error struct {
	Response *http.Response
}

func NewError(res *http.Response) *Error {
	// Sanity check
	if res == nil {
		panic("response object is nil")
	}
	return &Error{Response: res}
}
func (err *Error) Error() string { return "request failed" }

// LogAndClose is a notify-on-error function.
// It logs the error and closes the response body.
func LogAndClose() Notify {
	return func(err error, wait time.Duration) {
		switch e := err.(type) {
		case *Error:
			defer e.Response.Body.Close()

			b, err := ioutil.ReadAll(e.Response.Body)
			var body string
			if err != nil {
				body = "can't read body"
			} else {
				body = string(b)
			}

			log.Printf("%s: %s", e.Response.Status, body)
		default:
			log.Println(err)
		}
	}
}
Output:

Index

Examples

Constants

View Source
const (
	DefaultInitialInterval     = 500 * time.Millisecond
	DefaultRandomizationFactor = 0.5
	DefaultMultiplier          = 1.5
	DefaultMaxInterval         = 60 * time.Second
	DefaultMaxElapsedTime      = 15 * time.Minute
)

Default values for ExponentialBackOff.

View Source
const Stop time.Duration = -1

Indicates that no more retries should be made for use in NextBackOff().

Variables

View Source
var SystemClock = systemClock{}

SystemClock implements Clock interface that uses time.Now().

Functions

func Retry

func Retry(o Operation, b BackOff) error

Retry the function f until it does not return error or BackOff stops. f is guaranteed to be run at least once. It is the caller's responsibility to reset b after Retry returns.

Retry sleeps the goroutine for the duration returned by BackOff after a failed operation returns.

Example
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
Output:

func RetryNotify

func RetryNotify(operation Operation, b BackOff, notify Notify) error

RetryNotify calls notify function with the error and wait duration for each failed attempt before sleep.

Types

type BackOff

type BackOff interface {
	// NextBackOff returns the duration to wait before retrying the operation,
	// or backoff.Stop to indicate that no more retries should be made.
	//
	// Example usage:
	//
	// 	duration := backoff.NextBackOff();
	// 	if (duration == backoff.Stop) {
	// 		// Do not retry operation.
	// 	} else {
	// 		// Sleep for duration and retry operation.
	// 	}
	//
	NextBackOff() time.Duration

	// Reset to initial state.
	Reset()
}

BackOff is a backoff policy for retrying an operation.

type Clock

type Clock interface {
	Now() time.Time
}

Clock is an interface that returns current time for BackOff.

type ConstantBackOff

type ConstantBackOff struct {
	Interval time.Duration
}

ConstantBackOff is a backoff policy that always returns the same backoff delay. This is in contrast to an exponential backoff policy, which returns a delay that grows longer as you call NextBackOff() over and over again.

func NewConstantBackOff

func NewConstantBackOff(d time.Duration) *ConstantBackOff

func (*ConstantBackOff) NextBackOff

func (b *ConstantBackOff) NextBackOff() time.Duration

func (*ConstantBackOff) Reset

func (b *ConstantBackOff) Reset()

type ExponentialBackOff

type ExponentialBackOff struct {
	InitialInterval     time.Duration
	RandomizationFactor float64
	Multiplier          float64
	MaxInterval         time.Duration
	// After MaxElapsedTime the ExponentialBackOff stops.
	// It never stops if MaxElapsedTime == 0.
	MaxElapsedTime time.Duration
	Clock          Clock
	// contains filtered or unexported fields
}

ExponentialBackOff is a backoff implementation that increases the backoff period for each retry attempt using a randomization function that grows exponentially.

NextBackOff() is calculated using the following formula:

randomized interval =
    RetryInterval * (random value in range [1 - RandomizationFactor, 1 + RandomizationFactor])

In other words NextBackOff() will range between the randomization factor percentage below and above the retry interval.

For example, given the following parameters:

RetryInterval = 2
RandomizationFactor = 0.5
Multiplier = 2

the actual backoff period used in the next retry attempt will range between 1 and 3 seconds, multiplied by the exponential, that is, between 2 and 6 seconds.

Note: MaxInterval caps the RetryInterval and not the randomized interval.

If the time elapsed since an ExponentialBackOff instance is created goes past the MaxElapsedTime, then the method NextBackOff() starts returning backoff.Stop.

The elapsed time can be reset by calling Reset().

Example: Given the following default arguments, for 10 tries the sequence will be, and assuming we go over the MaxElapsedTime on the 10th try:

Request #  RetryInterval (seconds)  Randomized Interval (seconds)

 1          0.5                     [0.25,   0.75]
 2          0.75                    [0.375,  1.125]
 3          1.125                   [0.562,  1.687]
 4          1.687                   [0.8435, 2.53]
 5          2.53                    [1.265,  3.795]
 6          3.795                   [1.897,  5.692]
 7          5.692                   [2.846,  8.538]
 8          8.538                   [4.269, 12.807]
 9         12.807                   [6.403, 19.210]
10         19.210                   backoff.Stop

Note: Implementation is not thread-safe.

func NewExponentialBackOff

func NewExponentialBackOff() *ExponentialBackOff

NewExponentialBackOff creates an instance of ExponentialBackOff using default values.

func (*ExponentialBackOff) GetElapsedTime

func (b *ExponentialBackOff) GetElapsedTime() time.Duration

GetElapsedTime returns the elapsed time since an ExponentialBackOff instance is created and is reset when Reset() is called.

The elapsed time is computed using time.Now().UnixNano().

func (*ExponentialBackOff) NextBackOff

func (b *ExponentialBackOff) NextBackOff() time.Duration

NextBackOff calculates the next backoff interval using the formula:

Randomized interval = RetryInterval +/- (RandomizationFactor * RetryInterval)

func (*ExponentialBackOff) Reset

func (b *ExponentialBackOff) Reset()

Reset the interval back to the initial retry interval and restarts the timer.

type Notify

type Notify func(error, time.Duration)

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 Operation

type Operation func() error

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

type StopBackOff

type StopBackOff struct{}

StopBackOff is a fixed backoff policy that always returns backoff.Stop for NextBackOff(), meaning that the operation should never be retried.

func (*StopBackOff) NextBackOff

func (b *StopBackOff) NextBackOff() time.Duration

func (*StopBackOff) Reset

func (b *StopBackOff) Reset()

type Ticker

type Ticker struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

Ticker holds a channel that delivers `ticks' of a clock at times reported by a BackOff.

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.

Example
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
Output:

func NewTicker

func NewTicker(b BackOff) *Ticker

NewTicker returns a new Ticker containing a channel that will send the time at times specified by the BackOff argument. Ticker is guaranteed to tick at least once. The channel is closed when Stop method is called or BackOff stops.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off a ticker. After Stop, no more ticks will be sent.

type ZeroBackOff

type ZeroBackOff struct{}

ZeroBackOff is a fixed backoff policy whose backoff time is always zero, meaning that the operation is retried immediately without waiting, indefinitely.

func (*ZeroBackOff) NextBackOff

func (b *ZeroBackOff) NextBackOff() time.Duration

func (*ZeroBackOff) Reset

func (b *ZeroBackOff) Reset()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL