backoff

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package backoff provides retry delay policies: exponential with full jitter (the default, base 1s, cap 24h), constant, and custom functions.

p := backoff.NewExponential()
next := now.Add(p.Delay(job.Attempt))

Index

Constants

View Source
const (
	DefaultBase = time.Second
	DefaultCap  = 24 * time.Hour
)

Default parameters of the exponential policy.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constant

type Constant struct{ D time.Duration }

Constant always waits D (negative values are treated as 0).

func (Constant) Delay

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

Delay returns D.

type Exponential

type Exponential struct {
	Base time.Duration
	Cap  time.Duration
	// Rand returns a float in [0, 1); nil uses math/rand/v2 (goroutine-safe).
	Rand func() float64
}

Exponential is min(Cap, Base*2^(attempt-1)) with full jitter: the delay is drawn uniformly from [0, ceiling).

func NewExponential

func NewExponential() Exponential

NewExponential returns the default policy: base 1s, cap 24h.

func (Exponential) Ceiling

func (e Exponential) Ceiling(attempt int) time.Duration

Ceiling returns the jitter upper bound for attempt: min(Cap, Base*2^(attempt-1)). Attempts below 1 are treated as 1; non-positive Base or Cap yield 0.

func (Exponential) Delay

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

Delay returns a jittered delay in [0, Ceiling(attempt)).

type Func

type Func func(attempt int) time.Duration

Func adapts a function to a Policy; negative results are treated as 0.

func (Func) Delay

func (f Func) Delay(attempt int) time.Duration

Delay calls f.

type Policy

type Policy interface {
	Delay(attempt int) time.Duration
}

Policy computes the delay before the next attempt of a job whose attempt number attempt (1-based) just failed.

Jump to

Keyboard shortcuts

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