Documentation
¶
Overview ¶
Package backoff provides a system for introducing delays between retries of an operation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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.
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, DefaultBackoffConfig 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 ¶
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 ¶
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 ¶
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.
var DefaultStrategy Strategy = WithTransforms( Exponential(3*time.Second), linger.FullJitter, linger.Limiter(0, 1*time.Hour), )
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.
func Exponential ¶
Exponential returns a Strategy that uses binary exponential backoff (BEB).
The unit delay is doubled after each successive failure.
func Linear ¶
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.