retry

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 4 Imported by: 0

README

Retry

The retry package provides a generic, context-aware way to retry fallible operations with configurable backoff strategies and timeout handling.

Options

  • MaxAttempts uint: Total number of times the operation will be tried before giving up.
  • TotalTimeout time.Duration: Maximum time allowed across all attempts, including backoff delays.
  • Backoff func(attempt uint) time.Duration: Returns how long to wait before the next attempt. attempt is zero-indexed.
  • ShouldRetry func(err error) bool: Reports whether the given error is retryable. Return false to abort immediately.

Functions

  • Do[T any](ctx context.Context, opts Options, fn RetryFunc[T]) (T, error):
    Calls fn repeatedly until it succeeds, ShouldRetry returns false, MaxAttempts is reached, or TotalTimeout elapses.

  • DoVoid(ctx context.Context, opts Options, fn func(ctx context.Context) error) error:
    Convenience wrapper around Do for operations that return no value.

Backoff Strategies

  • FixedBackoff(d time.Duration) func(attempt uint) time.Duration:
    Waits exactly d between every attempt.

  • LinearBackoff(d time.Duration) func(attempt uint) time.Duration:
    Waits d * attempt. Grows linearly: 0, d, 2d, 3d, …

  • ExponentialBackoff(d time.Duration) func(attempt uint) time.Duration:
    Waits min(d * 2^attempt, 2^63 - 1). Doubles on each failure: d, 2d, 4d, 8d, …

Notes

  • TotalTimeout is enforced via a derived context passed to every attempt. If the deadline is exceeded mid-backoff, Do returns context.DeadlineExceeded immediately. A value of 0 means no timeout is applied.
  • A non-retryable error returned from ShouldRetry is returned as-is, without wrapping.
  • When MaxAttempts is exhausted, Do returns an error wrapping the last error from fn: "retry: max attempts reached: <last error>". Use errors.Is/errors.As to inspect the underlying cause.
  • Backoff and ShouldRetry are optional. If nil, Backoff defaults to no delay and ShouldRetry defaults to always retry.
  • LinearBackoff produces a zero-length first pause (attempt 0). Prefer FixedBackoff if an immediate first retry is undesirable.

Examples:

For examples of each function, please check out EXAMPLES.md


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do[T any](ctx context.Context, opts Options, fn RetryFunc[T]) (T, error)

Do calls fn repeatedly until fn succeeds, ShouldRetry returns false, MaxAttempts is reached, or TotalTimeout elapses.

func DoVoid

func DoVoid(ctx context.Context, opts Options, fn func(ctx context.Context) error) error

DoVoid is a convenience wrapper around Do for operations that return no value.

func ExponentialBackoff

func ExponentialBackoff(d time.Duration) func(attempt uint) time.Duration

ExponentialBackoff returns a backoff that waits min(d * 2^attempt, 2^63 - 1).

func FixedBackoff

func FixedBackoff(d time.Duration) func(attempt uint) time.Duration

FixedBackoff returns a backoff that always waits exactly d.

func LinearBackoff

func LinearBackoff(d time.Duration) func(attempt uint) time.Duration

LinearBackoff returns a backoff that waits d * attempt.

Types

type Options

type Options struct {
	MaxAttempts  uint
	TotalTimeout time.Duration
	// Backoff returns how long to wait before the next attempt.
	// attempt is zero-indexed (0 = pause after the first failure).
	Backoff func(attempt uint) time.Duration

	// ShouldRetry reports whether the given error is retryable.
	// Return false to abort immediately.
	ShouldRetry func(err error) bool
}

type RetryFunc

type RetryFunc[T any] func(ctx context.Context) (T, error)

Jump to

Keyboard shortcuts

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