backoff

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

backoff

The package provides a simple exponential backoff with optional symmetric jitter. The backoff grows from a minimum duration, is capped by a maximum duration, and applies random jitter to avoid synchronized retries.

// min, max, factor, jitter
b := backoff.New(time.Second, time.Minute, 3, 0.1)

for {
    err := doSomething()
    if err == nil {
        // ...
        return
    }
    
    select {
    case <-ctx.Done():
        return err
    case <-b.After(): // or <-time.After(b.Duration())
    }
}

See GoDoc for reference

Documentation

Overview

Package backoff provides a simple exponential backoff implementation with optional symmetric jitter.

The backoff grows exponentially from a minimum duration, is capped by a maximum duration (if specified), and can apply random jitter to avoid synchronized retries (thundering herd).

Backoff is not thread-safe.

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 implements an exponential backoff with optional jitter.

Each call to Duration advances the internal state. The base delay grows exponentially by multiplying the previous value by factor, starting from min and capped at max (if max > 0).

Jitter, if enabled, is applied only to the returned duration and does not affect the underlying exponential progression.

func New

func New(minDur, maxDur time.Duration, factor, jitter float64) *Backoff

New returns a new exponential Backoff.

The backoff starts at minDur and grows exponentially by the given factor on each call to Duration, up to maxDur if maxDur is positive. If jitter is non-zero, a symmetric random jitter is applied to the returned duration to reduce retry synchronization.

The returned backoff starts in a reset state; the first call to Duration returns minDur (with jitter applied, if enabled).

func (*Backoff) After

func (b *Backoff) After() <-chan time.Time

After returns a channel that will receive current time after the duration has elapsed. Each call advances the backoff state in the same way as Duration.

func (*Backoff) Duration

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

Duration returns the next backoff duration.

On the first call, it returns the minimum duration. On subsequent calls, the duration grows exponentially by the configured factor and is capped by the maximum duration if specified.

If jitter is enabled, the returned value is randomly adjusted within +/-jitter of the base duration.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset resets the backoff to its initial state. The next call to Duration will return the minimum duration.

func (*Backoff) Wait

func (b *Backoff) Wait()

Wait blocks for the duration returned by Duration. Each call advances the backoff state.

Jump to

Keyboard shortcuts

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