backoff

package
v3.0.25+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2016 License: MIT, MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const Stop time.Duration = -1

Stop is used as a signal to indicate that no more retries should be made.

Variables

This section is empty.

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.

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 {
	Next() time.Duration
	Reset()
}

Backoff is an interface for different types of backoff algorithms.

type ExponentialBackoff

type ExponentialBackoff struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ExponentialBackoff implements the simple exponential backoff described by Douglas Thain at http://dthain.blogspot.de/2009/02/exponential-backoff-in-distributed.html.

func NewExponentialBackoff

func NewExponentialBackoff(initialTimeout, maxTimeout time.Duration) *ExponentialBackoff

NewExponentialBackoff returns a ExponentialBackoff backoff policy. Use initialTimeout to set the first/minimal interval and maxTimeout to set the maximum wait interval.

func (*ExponentialBackoff) Next

func (t *ExponentialBackoff) Next() time.Duration

Next returns the next wait interval.

func (*ExponentialBackoff) Reset

func (t *ExponentialBackoff) Reset()

Reset resets the backoff policy so that it can be reused.

func (*ExponentialBackoff) SendStop

func (b *ExponentialBackoff) SendStop(doStop bool) *ExponentialBackoff

SendStop, when enables, makes Next to return Stop once the maximum timeout is reached.

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 SimpleBackoff

type SimpleBackoff struct {
	sync.Mutex
	// contains filtered or unexported fields
}

SimpleBackoff takes a list of fixed values for backoff intervals. Each call to Next returns the next value from that fixed list. After each value is returned, subsequent calls to Next will only return the last element. The caller may specify if the values are "jittered".

func NewSimpleBackoff

func NewSimpleBackoff(ticks ...int) *SimpleBackoff

NewSimpleBackoff creates a SimpleBackoff algorithm with the specified list of fixed intervals in milliseconds.

func (*SimpleBackoff) Jitter

func (b *SimpleBackoff) Jitter(doJitter bool) *SimpleBackoff

Jitter, when set, randomizes to return a value of [0.5*value .. 1.5*value].

func (*SimpleBackoff) Next

func (b *SimpleBackoff) Next() time.Duration

Next returns the next wait interval.

func (*SimpleBackoff) Reset

func (b *SimpleBackoff) Reset()

Reset resets SimpleBackoff.

func (*SimpleBackoff) SendStop

func (b *SimpleBackoff) SendStop(doStop bool) *SimpleBackoff

SendStop, when enables, makes Next to return Stop once the list of values is exhausted.

Jump to

Keyboard shortcuts

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