backoff

package
v0.0.0-...-22eeb2a Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package backoff computes retry delays with jitter.

Jitter matters here beyond the usual load-smoothing argument: every follower in every cluster polls the same row, and an unjittered retry period makes them converge into a synchronised herd that all wake, all read, and all contend at the same instant.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Jitter

func Jitter(d time.Duration, jitter float64, randFloat func() float64) time.Duration

Jitter returns d reduced by a random fraction of up to jitter. It is used for the steady-state polling interval, which does not grow but still must not be synchronised across participants.

Types

type Backoff

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

Backoff produces an exponentially growing, jittered delay. The zero value is not usable; construct one with New.

func New

func New(base, max time.Duration, factor, jitter float64, randFloat func() float64) *Backoff

New returns a Backoff that starts at base, multiplies by factor per attempt, and is capped at max. jitter is the fraction of the computed delay that is randomly subtracted, in [0, 1]: 0.2 yields a delay in [0.8d, d].

randFloat supplies values in [0, 1) and may be nil, in which case the global source is used. Tests inject it to make delays deterministic.

func (*Backoff) Next

func (b *Backoff) Next() time.Duration

Next returns the delay for the current attempt and advances the sequence.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset returns the sequence to its first attempt. It is called whenever an operation succeeds.

Jump to

Keyboard shortcuts

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