retry

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 9 Imported by: 0

README

retry - retry strategies and utilities

Go Reference

This package provides configurable retry strategies and utilities.

Inspired by go-retry.

Documentation

Overview

Package retry provides configurable retry strategies and utilities.

Index

Constants

This section is empty.

Variables

View Source
var ErrDispatcherClosed = errors.New("dispatcher is closed")

ErrDispatcherClosed is returned when attempting to dispatch a task through a closed Dispatcher.

Functions

func Abort added in v0.3.1

func Abort(err error) error

Abort marks an error as non-retryable.

func Do added in v0.3.1

func Do(ctx context.Context, backoff Backoff, fn func(ctx context.Context) error) error

Do executes a function with retries according to the provided backoff strategy. Retries the function on failure until it succeeds, the context is cancelled, the backoff strategy indicates stopping or a non-retryable error is returned by the function. Returns the last error if all retries fail, or nil on success.

Types

type Backoff

type Backoff interface {
	// Next returns the next wait duration and whether to retry.
	Next() (delay time.Duration, ok bool)
	// Attempt returns the number of retries attempted so far.
	Attempt() int
	// Reset restarts the backoff counter to the initial state.
	Reset()
}

Backoff implements a retry strategy with increasing delays.

var NoBackoff Backoff = &noRetry{}

NoBackoff is a Backoff that immediately stops retrying. Useful for disabling retries or as a terminal condition.

func Constant

func Constant(delay time.Duration) Backoff

Constant creates a backoff with fixed delay.

func Exponential

func Exponential(initial time.Duration) Backoff

Exponential creates a backoff with doubling delays.

func Fibonacci

func Fibonacci(initial time.Duration) Backoff

Fibonacci creates a backoff with Fibonacci sequence delays.

func Wrap

func Wrap(b Backoff, wrappers ...Wrapper) Backoff

Wrap applies a series of wrappers to a backoff. Equivalent to Chain(wrappers...)(b).

type Dispatcher added in v0.3.1

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

Dispatcher creates and dispatches tasks with retries according to backoff strategies.

func NewDispatcher added in v0.3.1

func NewDispatcher(tasks chan<- func()) *Dispatcher

NewDispatcher creates a new dispatcher. Dispatcher will send tasks for execution to the specified channel.

func (*Dispatcher) Close added in v0.3.1

func (d *Dispatcher) Close()

Close closes the dispatcher and stops all retries.

func (*Dispatcher) Do added in v0.3.1

func (d *Dispatcher) Do(ctx context.Context, backoff Backoff, fn func() error) error

Do creates and dispatches a task. If fn returns a retryable error, the task is dispatched again according to backoff.

func (*Dispatcher) Drain added in v0.3.1

func (d *Dispatcher) Drain()

Drain closes the dispatcher and waits for retries to complete at most once.

type Wrapper

type Wrapper func(next Backoff) Backoff

Wrapper is a function that decorates a Backoff with additional behavior.

func Chain

func Chain(wrappers ...Wrapper) Wrapper

Chain composes multiple wrappers into a single wrapper. Wrappers are applied in reverse order: first wrapper is outermost.

func Jitter

func Jitter(j time.Duration) Wrapper

Jitter randomizes delays from another backoff by ±j. The delay can never be less than 0.

func MaxDelay

func MaxDelay(delay time.Duration) Wrapper

MaxDelay caps delays from another backoff.

func MaxDuration

func MaxDuration(dur time.Duration) Wrapper

MaxDuration limits the cumulative delay time from a backoff. It's best-effort, and should not be used to guarantee an exact amount of time.

func MaxRetries

func MaxRetries(n int) Wrapper

MaxRetries caps the number of retries for another backoff.

func WithTimer

func WithTimer(timer *time.Timer) Wrapper

WithTimer attaches a timer to a backoff. Each call to Next resets the timer to the returned delay duration. Calling Reset stops the timer.

Jump to

Keyboard shortcuts

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