backoff

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 5 Imported by: 4

Documentation

Overview

Package backoff provides a system for introducing delays between retries of an operation.

Index

Constants

This section is empty.

Variables

DefaultStrategy is the default strategy used if none is specified.

It is a conservative policy favouring large delay times under the assumption that the operation is expensive.

Functions

func Retry

func Retry(
	ctx context.Context,
	s Strategy,
	fn func(ctx context.Context) error,
) (n uint, err error)

Retry calls the given function until it succeeds.

Each subsequent call is delayed according to the given backoff strategy. If s is nil, DefaultStrategy is used.

It returns ctx.Err() if ctx is canceled before fn() succeeds. n is the number of times that fn() failed, even if err is non-nil.

Types

type Counter

type Counter struct {
	// Strategy is used to calculate the delay duration.
	// If it is nil, DefaultStrategy is used.
	Strategy Strategy
	// contains filtered or unexported fields
}

Counter keeps track of the number of times an operation has failed to introduce delays between retries.

func (*Counter) Fail

func (c *Counter) Fail(err error) time.Duration

Fail marks the most recent attempt as a failure and returns the duration to wait before the operation should be retried.

err is the error describing the operation's failure condition, if known. A nil error does not indicate a success.

func (*Counter) Reset

func (c *Counter) Reset()

Reset marks the most recent attempt as a success, resetting the counter.

func (*Counter) Sleep

func (c *Counter) Sleep(ctx context.Context, err error) error

Sleep marks the most recent attempt as a failure and pauses the current goroutine until the wait duration has elapsed.

It sleeps until the duration elapses or ctx is canceled, whichever is first. If ctx is canceled before the duration elapses it returns ctx.Err(), otherwise it returns nil.

err is the error describing the operation's failure condition, if known. A nil error does not indicate a success.

type Strategy

type Strategy func(err error, n uint) time.Duration

Strategy is a function for computing delays between attempts to perform some application-defined operation.

err is the error describing the operation's failure, if known. A nil error does not indicate a success.

n is the number of successive failures since the last success, not including the failure indicated by err.

func CoalesceStrategy added in v1.1.0

func CoalesceStrategy(strategies ...Strategy) Strategy

CoalesceStrategy returns a strategy that iterates over the given strategies and runs them, returning the first positive duration.

func Constant

func Constant(d time.Duration) Strategy

Constant returns a Strategy that returns a fixed wait duration.

func Exponential

func Exponential(unit time.Duration) Strategy

Exponential returns a Strategy that uses binary exponential backoff (BEB).

The unit delay is doubled after each successive failure.

func FirstStrategy added in v1.1.0

func FirstStrategy(p linger.DurationPredicate, strategies ...Strategy) Strategy

FirstStrategy returns a strategy that iterates over the given strategies and runs them, returning the first duration which satisfies the predicate. Return zero if no duration satisfies the predicate.

func Linear

func Linear(unit time.Duration) Strategy

Linear returns a Strategy that increases the wait duration linearly.

The unit delay is multiplied by the number of successive failures.

func WithTransforms

func WithTransforms(s Strategy, transforms ...linger.DurationTransform) Strategy

WithTransforms returns a strategy that transforms the result of s using each of the given transforms in order.

Jump to

Keyboard shortcuts

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