Help us shape the future of Go! Take the 2026 Go Developer Survey

backoff

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package backoff provides pluggable retry delay strategies for job execution. All strategies are safe for concurrent use (they are stateless).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constant

type Constant struct {
	Interval time.Duration
}

Constant always returns the same delay regardless of attempt number.

func NewConstant

func NewConstant(interval time.Duration) *Constant

NewConstant creates a constant backoff strategy.

func (*Constant) Delay

func (c *Constant) Delay(_ int) time.Duration

Delay returns the fixed interval.

type Exponential

type Exponential struct {
	Initial time.Duration
	Max     time.Duration
}

Exponential doubles the delay each attempt. Delay = min(Initial * 2^(attempt-1), Max).

func NewExponential

func NewExponential(initial, maxDelay time.Duration) *Exponential

NewExponential creates an exponential backoff strategy.

func (*Exponential) Delay

func (e *Exponential) Delay(attempt int) time.Duration

Delay returns Initial * 2^(attempt-1), capped at Max.

type ExponentialWithJitter

type ExponentialWithJitter struct {
	Initial time.Duration
	Max     time.Duration
}

ExponentialWithJitter applies full jitter to an exponential base. Delay = random value in [0, min(Initial * 2^(attempt-1), Max)]. This prevents thundering herd when many retries happen simultaneously.

func NewExponentialWithJitter

func NewExponentialWithJitter(initial, maxDelay time.Duration) *ExponentialWithJitter

NewExponentialWithJitter creates an exponential backoff with full jitter.

func (*ExponentialWithJitter) Delay

func (e *ExponentialWithJitter) Delay(attempt int) time.Duration

Delay returns a random duration in [0, min(Initial * 2^(attempt-1), Max)].

type Linear

type Linear struct {
	Initial time.Duration
	Max     time.Duration
}

Linear increases the delay linearly with the attempt number. Delay = min(Initial * attempt, Max).

func NewLinear

func NewLinear(initial, maxDelay time.Duration) *Linear

NewLinear creates a linear backoff strategy.

func (*Linear) Delay

func (l *Linear) Delay(attempt int) time.Duration

Delay returns Initial * attempt, capped at Max.

type Strategy

type Strategy interface {
	// Delay returns how long to wait before retry attempt n (1-indexed).
	// Attempt 1 is the first retry after the initial failure.
	Delay(attempt int) time.Duration
}

Strategy computes the delay before a retry attempt.

func DefaultStrategy

func DefaultStrategy() Strategy

DefaultStrategy returns the default backoff used by the engine: ExponentialWithJitter with 1s initial and 1m max.

Jump to

Keyboard shortcuts

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