backoff

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package backoff provides a calculator for backoff with jitter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Atomic

type Atomic struct {
	// contains filtered or unexported fields
}

Atomic is a stateful backoff with internal atomic counter.

func NewAtomic

func NewAtomic(config Backoff) *Atomic

func (*Atomic) Duration

func (b *Atomic) Duration() time.Duration

Duration returns zero for the first attempt and if the time difference between now and the last call to Duration() is greater than the backoff duration, otherwise returns the backoff duration minus the time since last call.

func (*Atomic) Iter

func (b *Atomic) Iter() iter.Seq2[int, time.Duration]

Iter returns an iterator over an atomic backoff.

func (*Atomic) Reset

func (b *Atomic) Reset()

Reset resets the attempt counter.

type Backoff

type Backoff struct {
	Min        time.Duration // Minimum backoff duration (must be greater 0 and Max).
	Max        time.Duration // Maximum backoff duration.
	Factor     float64       // Exponential growth factor. Must be greater 1.0.
	Jitter     float64       // Jitter ratio in [0.0, 1.0]
	RandSource RandReader
}

Backoff is a stateless exponential backoff with jitter calculator.

func New

func New(
	min, max time.Duration, factor, jitter float64, randSource RandReader,
) (Backoff, error)

New checks the parameters and returns a new backoff if they're correct, otherwise returns an error. If randSource==nil a new default PCG randomness source is created automatically.

func (Backoff) Duration

func (b Backoff) Duration(attempt int) time.Duration

Duration returns the backoff delay for attempt. Returns 0 when attempt <1.

type RandReader

type RandReader interface{ Float64() float64 }

RandReader provides https://pkg.go.dev/math/rand/v2#Float64.

Jump to

Keyboard shortcuts

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