backoff

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package backoff provides backoff mechanisms for retrying operations.

Index

Constants

View Source
const (
	MinDelay = 20 * time.Millisecond
	MaxDelay = 10 * time.Second

	ErrUnknownScale = consterr.Error("unknown scale")
	ErrInvalidScale = consterr.Error("invalid scale")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	RetryDelay(attempt int) time.Duration
}

Backoff represents a backoff mechanism that returns delay before next retry happens.

func New

func New(scale Scale, base, limit time.Duration) (Backoff, error)

New returns backoff implementation based on the Scale type and basic configuration. It returns an error when an invalid Scale is passed.

type Base

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

Base is a base entity suitable for most backoff implementations. It is expected to be embedded, not to be used directly.

func NewBase

func NewBase(base, limit time.Duration, options ...Option) Base

NewBase returns a configured Base. base – starting value of delay between retries. limit – maximum value of delay between retries.

func (Base) Jitter

func (b Base) Jitter(base float64) float64

func (Base) Limit

func (b Base) Limit(delay float64) time.Duration

type ExponentialJitter

type ExponentialJitter struct {
	Base
}

ExponentialJitter is an implementation of Backoff interface that provides backoff delays with jitter. Delay grows exponentially.

func NewExponentialJitter

func NewExponentialJitter(base, limit time.Duration, options ...Option) ExponentialJitter

NewExponentialJitter returns a configured ExponentialJitter. Arguments are the same as for NewBase.

func (ExponentialJitter) RetryDelay

func (b ExponentialJitter) RetryDelay(attempt int) time.Duration

RetryDelay returns delay before next retry based on the attempt number. Formula is: min(exponentialBase^attempt * baseDelay * (1 + [0.0..1.0)/2), maxDelay).

type Func

type Func func(int) time.Duration

Func is an implementation of Backoff interface suitable for simple scenarios.

func NewFixed

func NewFixed(delay time.Duration) Func

NewFixed returns a Func that returns fixed delay for every attempt.

func (Func) RetryDelay

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

type LinearJitter

type LinearJitter struct {
	Base
}

LinearJitter is an implementation of Backoff interface that provides backoff delays with jitter. Delay grows linearly.

func NewLinearJitter

func NewLinearJitter(base, limit time.Duration, options ...Option) LinearJitter

NewLinearJitter returns a configured LinearJitter. Arguments are the same as for NewBase.

func (LinearJitter) RetryDelay

func (b LinearJitter) RetryDelay(attempt int) time.Duration

RetryDelay returns delay before next retry based on the attempt number. Formula is: min((attempt + 1) * baseDelay * (1 + [0.0..1.0)/2), maxDelay).

type Option

type Option func(*Base)

Option is a functional option type that extends Base functionality

func WithRand

func WithRand(rand func() float64) Option

WithRand returns an option that overrides default randomizer function

type ProgressiveJitter

type ProgressiveJitter struct {
	Base
}

ProgressiveJitter is an implementation of Backoff interface that provides backoff delays with jitter. Delay grows in progression.

func NewProgressiveJitter

func NewProgressiveJitter(base, limit time.Duration, options ...Option) ProgressiveJitter

NewProgressiveJitter returns a configured ProgressiveJitter. Arguments are the same as for NewBase. Using base value of 1ms is not recommended, see RetryDelay.

func (ProgressiveJitter) RetryDelay

func (b ProgressiveJitter) RetryDelay(attempt int) time.Duration

RetryDelay returns delay before next retry based on the attempt number. Formula is: min((baseDelay/ms)^(attempt + 1) * (1 + [0.0..1.0)/2) * ms, maxDelay).

type Scale

type Scale int

Scale defines timeout increase formula. Fixed – fixed, timeout doesn't change. Linear – t, 2t, 3t, ..., n*t. Progressive – t, t^2, t^3, ..., t^n. Exponential – t, t*2, t*2^2, ..., t*2^n.

const (
	ScaleInvalid Scale = iota - 1
	ScaleFixed
	ScaleLinear
	ScaleProgressive
	ScaleExponential
)

Jump to

Keyboard shortcuts

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