backoff

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package backoff provides a thread-safe, configurable exponential backoff implementation using atomic operations.

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 manages exponential backoff state and parameters safely across multiple goroutines. To instantiate a new instance, use the NewBackoff function.

func NewBackoff

func NewBackoff(opts ...BackoffOption) *Backoff

NewBackoff initializes and returns a new *Backoff instance with default values and applies any provided BackoffOption. If the configured minimum value exceeds the maximum value, the minimum will be clamped to the maximum value.

func (*Backoff) Attempts

func (b *Backoff) Attempts() uint64

Attempts returns the current number of backoff attempts made.

func (*Backoff) Duration

func (b *Backoff) Duration() int64

Duration calculates and returns the next backoff duration in nanoseconds. It increments the attempt counter atomically and applies exponential growth and jitter if configured.

func (*Backoff) GetFactor

func (b *Backoff) GetFactor() float64

GetFactor returns the current multiplication factor.

func (*Backoff) GetJitter

func (b *Backoff) GetJitter() float64

GetJitter returns the current jitter ratio.

func (*Backoff) GetMax

func (b *Backoff) GetMax() float64

GetMax returns the current maximum duration value.

func (*Backoff) GetMin

func (b *Backoff) GetMin() float64

GetMin returns the current minimum duration value.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset resets the internal attempt counter back to 0.

func (*Backoff) SetFactor

func (b *Backoff) SetFactor(val float64)

SetFactor updates the multiplication factor dynamically. The value must be greater than 1 and a valid finite number.

func (*Backoff) SetJitter

func (b *Backoff) SetJitter(val float64)

SetJitter updates the jitter ratio dynamically. The value must be between 0 and 1 inclusive.

func (*Backoff) SetMax

func (b *Backoff) SetMax(val float64)

SetMax updates the maximum duration value dynamically. The value is automatically clamped to the current minimum value.

func (*Backoff) SetMin

func (b *Backoff) SetMin(val float64)

SetMin updates the minimum duration value dynamically. The value is automatically clamped to the current maximum value.

type BackoffOption

type BackoffOption func(*Backoff)

BackoffOption defines a configuration function used to customize a *Backoff instance.

func WithFactor

func WithFactor(factor float64) BackoffOption

WithFactor returns a BackoffOption that configures the multiplication factor. The value must be greater than 1 and a valid finite number.

func WithJitter

func WithJitter(jitter float64) BackoffOption

WithJitter returns a BackoffOption that configures the jitter ratio. The value must be between 0 and 1 inclusive.

func WithMax

func WithMax(max float64) BackoffOption

WithMax returns a BackoffOption that configures the maximum duration. The value must be greater than 0 and a valid finite number.

func WithMin

func WithMin(min float64) BackoffOption

WithMin returns a BackoffOption that configures the minimum duration. The value must be greater than 0 and a valid finite number.

Jump to

Keyboard shortcuts

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