retry

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 6 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 Delay

func Delay(policy Policy, attempt int) time.Duration

Delay returns the jittered exponential backoff delay for the attempt number.

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 Policy

type Policy struct {
	MaxAttempts int
	BaseDelay   time.Duration
	MaxDelay    time.Duration
	JitterRatio float64
	RandFloat64 func() float64
	Sleep       func(context.Context, time.Duration) error
	OnRetry     func(Attempt)
}

Policy configures retry attempts and jittered exponential backoff.

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