Documentation ¶
Overview ¶
Package retry contains code to perform retries with exponential backoff.
Example: loop until doSomething() returns true or context hits deadline or is canceled.
for r := retry.Begin(); r.Continue(ctx); { if doSomething() { break } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = Options{ BackoffMultiplier: 1.3, BackoffMinDuration: 10 * time.Millisecond, }
DefaultOptions is the default set of Options.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct { BackoffMultiplier float64 // If specified, must be at least 1. BackoffMinDuration time.Duration }
Options are the options that configure a retry loop. Before the ith iteration of a retry loop, retry.Continue() sleeps for a duration of BackoffMinDuration * BackoffMultiplier^i, with added jitter.
type Retry ¶
type Retry struct {
// contains filtered or unexported fields
}
Retry holds state for managing retry loops with exponential backoff and jitter.
func BeginWithOptions ¶
BeginWithOptions returns a new retry loop configured with the provided options.
Example: Sleep 1 second, then 2 seconds, then 4 seconds, and so on.
opts := retry.Options{ BackoffMultiplier: 2.0, BackoffMinDuration: time.Second, } for r := retry.Begin(); r.Continue(ctx); { // Do nothing. }
func (*Retry) Continue ¶
Continue sleeps for an exponentially increasing interval (with jitter). It stops its sleep early and returns false if context becomes done. If the return value is false, ctx.Err() is guaranteed to be non-nil. The first call does not sleep.
func (*Retry) Reset ¶
func (r *Retry) Reset()
Reset resets a Retry to its initial state. Reset is useful if you want to retry an operation with exponential backoff, but only if it is failing. For example:
for r := retry.Begin(); r.Continue(ctx); { if err := doSomething(); err != nil { // Retry with backoff if we fail. continue } // Retry immediately if we succeed. r.Reset() }