Git Product home page Git Product logo

backoff's Introduction

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

backoff's People

Contributors

cenkalti avatar fatih avatar oryband avatar pingles avatar robbiev avatar markchadwick avatar

Stargazers

曹文忠 avatar

Watchers

Garrett Johnson avatar James Cloos avatar Francisco Alberini avatar  avatar Brantley Beaird avatar Chris Sperandio avatar  avatar Han avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.