Documentation
¶
Overview ¶
Package backoff provides a system for introducing delays between retries of an operation.
Index ¶
- Variables
- func Retry(ctx context.Context, s Strategy, fn func(ctx context.Context) error) (n uint, err error)
- type Counter
- type Strategy
- func CoalesceStrategy(strategies ...Strategy) Strategy
- func Constant(d time.Duration) Strategy
- func Exponential(unit time.Duration) Strategy
- func FirstStrategy(p linger.DurationPredicate, strategies ...Strategy) Strategy
- func Linear(unit time.Duration) Strategy
- func WithTransforms(s Strategy, transforms ...linger.DurationTransform) Strategy
Constants ¶
This section is empty.
Variables ¶
var DefaultStrategy = 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.
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 ¶
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.
func CoalesceStrategy ¶ added in v1.1.0
CoalesceStrategy returns a strategy that iterates over the given strategies and runs them, returning the first positive duration.
func Exponential ¶
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 ¶
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.