backoff

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: GPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package backoff provides an exponential backoff schedule with optional additive jitter and a configurable cap. It is designed to drive reconnect loops (APRS-IS client) and restart supervisors (modembridge child process), which previously each carried their own nearly-identical implementation.

A Backoff is not safe for concurrent use; each caller owns one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

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

Backoff is the stateful schedule. The zero value is not usable; call New.

func New

func New(cfg Config) *Backoff

New builds a Backoff from cfg. Panics on an invalid configuration so callers discover the problem at startup rather than after a failure already occurred.

func (*Backoff) Next

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

Next returns the current delay, then advances the schedule. The first call returns Initial (plus jitter); each subsequent call multiplies the un-jittered delay by Factor, capped at Max.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset clears the backoff so the next Next call returns Initial again. Use after a successful operation so the next failure starts over from the bottom of the schedule.

type Config

type Config struct {
	// Initial is the first delay returned by Next. Required; a
	// non-positive value panics in New.
	Initial time.Duration
	// Max caps the schedule. Once the growing delay reaches Max it
	// stays there. Zero means "unbounded" (capped at math.MaxInt64).
	Max time.Duration
	// Factor is the growth multiplier applied after each Next call.
	// Zero defaults to 2.0 (classic "double each time").
	Factor float64
	// JitterFrac is the upper bound of additive jitter as a fraction
	// of the current delay: the returned value is current +
	// Uniform[0, current*JitterFrac). Zero disables jitter entirely.
	// Must be in [0, 1); values outside that range panic.
	JitterFrac float64
	// Rand is the random source used for jitter. Ignored when
	// JitterFrac is 0. When nil and JitterFrac > 0, a time-seeded
	// source is created in New; tests that want deterministic jitter
	// should supply their own source.
	Rand *rand.Rand
}

Config describes one Backoff schedule.

Jump to

Keyboard shortcuts

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