backoff

package
v1.30.3 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRetriesExhausted is returned when the maximum number of retries has been reached.
	ErrRetriesExhausted = errors.New("retries exhausted")
	// ErrOperationCanceled is returned when the retry operation is canceled via context.
	ErrOperationCanceled = errors.New("operation canceled")
)
View Source
var (
	ErrPermanent = errors.New("permanent error")
)

Functions

func PermanentError added in v1.24.8

func PermanentError(err error) error

func Retry

func Retry(ctx context.Context, op Operation, policy RetryPolicy, isRetriable IsRetriableFunc) error

Retry executes the operation with retry logic based on the provided policy. If isRetriable is nil, all errors are considered retriable.

Types

type ConstantBackoffPolicy

type ConstantBackoffPolicy struct {
	// Interval is the constant interval between retries.
	Interval time.Duration `json:"interval,omitempty"`
	// MaxRetries is the maximum number of retries allowed. 0 means unlimited retries.
	MaxRetries int `json:"maxRetries,omitempty"`
}

ConstantBackoffPolicy is a retry policy that uses a constant interval between retries.

func NewConstantBackoffPolicy

func NewConstantBackoffPolicy(interval time.Duration) *ConstantBackoffPolicy

NewConstantBackoffPolicy creates a new ConstantBackoffPolicy with the specified interval.

func (*ConstantBackoffPolicy) ComputeNextInterval

func (p *ConstantBackoffPolicy) ComputeNextInterval(retryCount int, _ time.Duration, _ error) (time.Duration, error)

ComputeNextInterval returns a constant interval for each retry.

type ExponentialBackoffPolicy

type ExponentialBackoffPolicy struct {
	// InitialInterval is the initial interval before the first retry.
	InitialInterval time.Duration `json:"initialInterval,omitempty"`
	// BackoffFactor is the factor by which the interval increases after each retry.
	BackoffFactor float64 `json:"backoffFactor,omitempty"`
	// MaxInterval is the maximum interval cap for exponential backoff.
	MaxInterval time.Duration `json:"maxInterval,omitempty"`
	// MaxRetries is the maximum number of retries allowed. 0 means unlimited retries.
	MaxRetries int `json:"maxRetries,omitempty"`
}

ExponentialBackoffPolicy is a retry policy that implements exponential backoff.

func NewExponentialBackoffPolicy

func NewExponentialBackoffPolicy(initialInterval time.Duration) *ExponentialBackoffPolicy

NewExponentialBackoffPolicy creates a new ExponentialBackoffPolicy with the specified parameters.

func (*ExponentialBackoffPolicy) ComputeNextInterval

func (p *ExponentialBackoffPolicy) ComputeNextInterval(retryCount int, _ time.Duration, _ error) (time.Duration, error)

ComputeNextInterval computes the next retry interval using exponential backoff.

type IsRetriableFunc

type IsRetriableFunc func(err error) bool

IsRetriableFunc defines a function that checks if an error is retriable.

type JitterFunc

type JitterFunc func(interval time.Duration) time.Duration

JitterFunc defines a function that applies jitter to a backoff interval.

func NewJitterFunc

func NewJitterFunc(jitterType JitterType) JitterFunc

NewJitterFunc creates a new jitter function with the specified type.

type JitterType

type JitterType int

JitterType defines the type of jitter to apply to backoff intervals.

const (
	// NoJitter applies no jitter to the interval.
	NoJitter JitterType = iota
	// FullJitter applies jitter between 0 and the interval.
	FullJitter
	// Jitter applies jitter of ±50% of the interval.
	Jitter
)

type LinearBackoffPolicy

type LinearBackoffPolicy struct {
	// InitialInterval is the initial interval before the first retry.
	InitialInterval time.Duration `json:"initialInterval,omitempty"`
	// Increment is the amount by which the interval increases after each retry.
	Increment time.Duration `json:"increment,omitempty"`
	// MaxInterval is the maximum interval cap.
	MaxInterval time.Duration `json:"maxInterval,omitempty"`
	// MaxRetries is the maximum number of retries allowed. 0 means unlimited retries.
	MaxRetries int `json:"maxRetries,omitempty"`
}

LinearBackoffPolicy is a retry policy that increases the interval linearly.

func NewLinearBackoffPolicy

func NewLinearBackoffPolicy(initialInterval, increment time.Duration) *LinearBackoffPolicy

NewLinearBackoffPolicy creates a new LinearBackoffPolicy with the specified parameters.

func (*LinearBackoffPolicy) ComputeNextInterval

func (p *LinearBackoffPolicy) ComputeNextInterval(retryCount int, _ time.Duration, _ error) (time.Duration, error)

ComputeNextInterval computes the next retry interval using linear backoff.

type Operation

type Operation func(ctx context.Context) error

Operation to retry

type Retrier

type Retrier interface {
	// Next computes the next retry interval and updates internal state.
	// Returns the interval to wait and any error (e.g., retries exhausted).
	Next(err error) (time.Duration, error)
	// Reset resets the retrier to its initial state.
	Reset()
}

Retrier manages the state of retry operations.

func NewRetrier

func NewRetrier(retryPolicy RetryPolicy) Retrier

NewRetrier creates a new Retrier instance with the specified retry policy.

type RetryPolicy

type RetryPolicy interface {
	// ComputeNextInterval computes the next interval based on the retry policy.
	// Returns the duration to wait before the next retry, or an error if no more retries should be attempted.
	ComputeNextInterval(retryCount int, elapsedTime time.Duration, err error) (time.Duration, error)
}

RetryPolicy defines the interface for retry policies.

func WithJitter

func WithJitter(basePolicy RetryPolicy, jitterType JitterType) RetryPolicy

WithJitter returns a new RetryPolicy that applies jitter to the base policy's intervals.

Jump to

Keyboard shortcuts

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