Documentation
¶
Overview ¶
Package backoff provides various backoff strategies.
In particular, the package implements Constant, Linear and Exponential backoff strategies as well as some decorators to adjust their behavior. These include setting a Timeout, a delay Cap, an attempt Limit, or adding random Jitter.
Index ¶
- Variables
- type Clock
- type ClockFunc
- type Random
- type Strategy
- func Cap(strategy Strategy, max time.Duration) Strategy
- func Constant(d time.Duration) Strategy
- func Exponential(d time.Duration, m float64) Strategy
- func Jitter(strategy Strategy, spread float64, random Random) Strategy
- func Limit(strategy Strategy, n int) Strategy
- func Linear(d time.Duration, k time.Duration) Strategy
- func Timeout(strategy Strategy, limit time.Duration, clock Clock) Strategy
Constants ¶
This section is empty.
Variables ¶
var Exit time.Duration = -1
Exit is returned by a backoff Strategy to signal the end of a retry cycle.
Functions ¶
This section is empty.
Types ¶
type ClockFunc ¶ added in v1.2.0
A ClockFunc is the functional implementation of the Clock interface.
type Random ¶
type Random func() float64
Random returns a pseudo-random number in the half-open interval [0,1).
type Strategy ¶
type Strategy interface { // Delay returns the time to wait after the n-th retry of a failing function // call. For implementing time-based algorithms, the function also takes the // start time of the retry cycle. To stop the cycle after n attempts, the // function must return Exit. Note that the initial attempt corresponds to // n = 1. Delay(n int, start time.Time) time.Duration }
Strategy determines the delay between consecutive retries in a backoff scenario. Implementations of this interface should be stateless because they might be used in multiple concurrent goroutines.
Once is a backoff Strategy that always returns Exit, i.e. exits after the first attempt. Mostly useful for testing purposes.
func Cap ¶
Cap wraps a backoff Strategy to cap produced delays at the given maximum. If max <= 0, no limit will be applied.
func Constant ¶
Constant returns a backoff Strategy that always returns delay d. The function panics if d < 0.
func Exponential ¶
Exponential returns a backoff Strategy producing delays that exponentially grow (m > 1), or shrink (m < 1) by the factor m, starting from the specified initial delay d. The function panics if d or m are negative.
func Jitter ¶
Jitter wraps a backoff Strategy to randomly spread produced delays around in time. The spread factor determines the relative range in which delays are scattered. It must fall in the half-open interval [0,1). For example, a spread of 0.5 results in delays ranging between 50% above and 50% below the values produced by the wrapped strategy. If spread = 0, no jitter will be applied.
func Limit ¶
Limit wraps a backoff Strategy to end the retry cycle after n attempts. If n < 1, no limit will be applied.