backoff

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMaxRetry = errors.New("exceeded maximum number of retries")

Functions

This section is empty.

Types

type Backoff

type Backoff struct {
	// contains filtered or unexported fields
}

Backoff is used to do capped exponential backoff with jitter, with a maximum number of retries. Generally, use this struct by calling Next() or NextSleep() after a failure. If configured for N max retries, Next() and NextSleep() will return an error on the call N+1. The jitter is set to 25%, so values returned will have up to 25% less than twice the previous value. The min value will also include jitter, so the first call will almost always be less than the requested minimum value. Backoff is not thread-safe.

func NewBackoff

func NewBackoff(maxRetries int, min, max time.Duration) *Backoff

NewBackoff creates a new exponential backoff with the given number of maximum retries and min/max durations.

func (*Backoff) Current

func (b *Backoff) Current() time.Duration

Current returns the next time that will be returned by Next() (or slept in NextSleep()).

func (*Backoff) Next

func (b *Backoff) Next() (time.Duration, error)

Next determines the next backoff duration that is roughly twice the current value, capped to a max value, with a measure of randomness. It returns an error if there are no more retries left.

func (*Backoff) NextSleep

func (b *Backoff) NextSleep() error

NextSleep will synchronously sleep the next backoff amount (see Next()). It returns an error if there are no more retries left.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset resets the state to the initial backoff amount and 0 retries.

func (*Backoff) Retry

func (b *Backoff) Retry(f func() error) error

Retry calls the given function until it does not return an error, at least once and up to max_retries + 1 times. If the number of retries is exceeded, Retry() will return the last error seen joined with ErrMaxRetry.

Jump to

Keyboard shortcuts

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