Documentation
¶
Overview ¶
Package backoff provides a simple exponential backoff implementation with optional symmetric jitter.
The backoff grows exponentially from a minimum duration, is capped by a maximum duration (if specified), and can apply random jitter to avoid synchronized retries (thundering herd).
Backoff is not thread-safe.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backoff ¶
type Backoff struct {
// contains filtered or unexported fields
}
Backoff implements an exponential backoff with optional jitter.
Each call to Duration advances the internal state. The base delay grows exponentially by multiplying the previous value by factor, starting from min and capped at max (if max > 0).
Jitter, if enabled, is applied only to the returned duration and does not affect the underlying exponential progression.
func New ¶
New returns a new exponential Backoff.
The backoff starts at minDur and grows exponentially by the given factor on each call to Duration, up to maxDur if maxDur is positive. If jitter is non-zero, a symmetric random jitter is applied to the returned duration to reduce retry synchronization.
The returned backoff starts in a reset state; the first call to Duration returns minDur (with jitter applied, if enabled).
func (*Backoff) After ¶
After returns a channel that will receive current time after the duration has elapsed. Each call advances the backoff state in the same way as Duration.
func (*Backoff) Duration ¶
Duration returns the next backoff duration.
On the first call, it returns the minimum duration. On subsequent calls, the duration grows exponentially by the configured factor and is capped by the maximum duration if specified.
If jitter is enabled, the returned value is randomly adjusted within +/-jitter of the base duration.