backoff

package
v1.1.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package backoff provides customizable strategies for retrying operations with increasing delays.

The core of the package is the Strategy interface, which computes the next backoff duration. Implementations are stateful; the Next method returns progressively longer durations with each call. Once the retried operation is successful or abandoned, Done must be called to reset the strategy's internal state.

Usage

A default exponential backoff strategy with jitter can be created using the New function. The behavior can be customized using various Option functions, such as WithMinDelay, WithMaxDelay, WithGrowthFactor, and WithJitterAmount. Jitter is added by default to prevent multiple clients from retrying in sync (the "thundering herd" problem), which can overwhelm a recovering service.

Index

Constants

View Source
const (
	// DefaultMinDelay is the default minimum time between consecutive retries.
	DefaultMinDelay = 1 * time.Second
	// DefaultMaxDelay is the default maximum time between consecutive retries.
	DefaultMaxDelay = 1 * time.Minute
	// DefaultGrowthFactor is the default growth factor in exponential backoff.
	DefaultGrowthFactor float64 = 2.0
	// DefaultJitterAmount is the default amount of jitter applied.
	DefaultJitterAmount float64 = 0.5
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*config)

Option customizes the behavior of a backoff Strategy.

func WithGrowthFactor

func WithGrowthFactor(f float64) Option

WithGrowthFactor determines the growth factor (multiplier) for exponential backoff. A factor equal to one results in linear backoff, where the minimum delay becomes the step size. Any factor less than one is treated as one. If not customized, the DefaultGrowthFactor is used.

func WithJitterAmount

func WithJitterAmount(p float64) Option

WithJitterAmount specifies the amount of random jitter to apply to the backoff delays. It is expressed as a fraction of the delay, where 0 means no jitter and 1 means full jitter. The given number is capped between 0 and 1. If not customized, the DefaultJitterAmount is used.

Jitter scatters the retry attempts in time, which aims to mitigate the thundering herd problem, where many clients retry simultaneously.

func WithMaxDelay

func WithMaxDelay(d time.Duration) Option

WithMaxDelay sets the maximum time between consecutive retries. It is capped at zero (meaning no delay) if a negative duration is provided. If less than or equal to the minimum delay, the backoff delays remain constant at the maximum delay. If not customized, the DefaultMaxDelay is used.

func WithMinDelay

func WithMinDelay(d time.Duration) Option

WithMinDelay sets the minimum time between consecutive retries. It is capped at zero (meaning no delay) if a negative duration is provided. If equal to or greater than the maximum delay, the backoff delays remain constant at the maximum delay. If not customized, the DefaultMinDelay is used.

When jitter is introduced, the minimum delay is effectively reduced proportional to the jitter amount. Thus, the strategy might return a delay shorter than the configured minimum delay, depending on the random output.

func WithRand

func WithRand(r jitter.Rand) Option

WithRand sets the source of randomness for jittering. If not specified or nil, a default source will be seeded with the current system time.

type Strategy

type Strategy interface {
	// Next returns the backoff duration for the upcoming retry attempt.
	// This method is stateful and returns incrementally larger durations based
	// on the number of times it has been called since the last call to Done.
	// The returned duration is bounded by MinDelay() and MaxDelay().
	Next() time.Duration
	// Done resets the strategy's internal state, such as its attempt counter.
	// This must be called after the retried operation succeeds or is abandoned.
	Done()
	// MinDelay returns the lower bound for the backoff duration returned by Next.
	MinDelay() time.Duration
	// MaxDelay returns the upper bound for the backoff duration returned by Next.
	MaxDelay() time.Duration
}

Strategy defines the contract for a backoff algorithm. Implementations of this interface are expected to be safe for concurrent use.

func Constant

func Constant(delay time.Duration) Strategy

Constant produces a Strategy that always yields the same delay duration. If the provided delay is negative, it is treated as zero (meaning no delay).

func New

func New(opts ...Option) Strategy

New creates a new backoff Strategy based on the provided options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL