retry

package
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package retry provides small context-aware retry and backoff primitives.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, policy Policy, shouldRetry ShouldRetry, fn func(context.Context) error) error

Do retries fn until it succeeds, shouldRetry rejects the error, attempts are exhausted, or ctx is canceled.

func DoValue

func DoValue[T any](
	ctx context.Context,
	policy Policy,
	shouldRetry ShouldRetry,
	fn func(context.Context) (T, error),
) (T, error)

DoValue is Do for operations that return a value.

func Wait

func Wait(ctx context.Context, delay time.Duration) error

Wait blocks for delay or returns early when ctx is canceled.

Types

type Attempt

type Attempt struct {
	Number      int
	MaxAttempts int
	Err         error
	Delay       time.Duration
}

Attempt describes one failed attempt that will be retried.

type DecorrelatedJitterConfig

type DecorrelatedJitterConfig struct {
	BaseDelay   time.Duration
	MaxDelay    time.Duration
	RandFloat64 func() float64
}

DecorrelatedJitterConfig bounds a decorrelated-jitter delay sequence.

type DelayFunc

type DelayFunc func(DelayInput) time.Duration

DelayFunc computes the delay before the next attempt.

func DecorrelatedJitter

func DecorrelatedJitter(config DecorrelatedJitterConfig) DelayFunc

DecorrelatedJitter returns a per-operation delay strategy whose next value is sampled between the base delay and three times the previous delay.

func Exponential

func Exponential(config ExponentialConfig) DelayFunc

Exponential returns a delay strategy whose first delay is BaseDelay and whose later delays double without exceeding MaxDelay.

type DelayInput

type DelayInput struct {
	Attempt  int
	Previous time.Duration
	Err      error
}

DelayInput describes the failed attempt for which a retry delay is needed.

type ExponentialConfig

type ExponentialConfig struct {
	BaseDelay time.Duration
	MaxDelay  time.Duration
}

ExponentialConfig bounds a deterministic doubling delay sequence.

type Policy

type Policy struct {
	MaxAttempts int
	Delay       DelayFunc
	Sleep       func(context.Context, time.Duration) error
	OnRetry     func(context.Context, Attempt) error
}

Policy configures retry attempts and their delay strategy.

type ShouldRetry

type ShouldRetry func(error) bool

ShouldRetry reports whether an operation error should be retried.

Jump to

Keyboard shortcuts

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