retry

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package retry provides exponential backoff retry mechanisms for high availability.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxAttemptsExceeded = errors.New("maximum retry attempts exceeded")
	ErrContextCanceled     = errors.New("context canceled")
)

Retryable errors

View Source
var Retryable = func(err error) bool {

	return isTemporary(err) || isTimeout(err)
}

Retryable wraps common errors that should be retried.

Functions

func Do

func Do(fn func() error) error

Do is a convenience method that creates a retrier with default config and background context.

func DoWithContext

func DoWithContext(ctx context.Context, fn func() error) error

DoWithContext is a convenience method that creates a retrier with default config.

Types

type Attempt

type Attempt struct {
	AttemptNumber int
	Delay         time.Duration
	Error         error
}

Attempt represents a single retry attempt.

type Config

type Config struct {
	// MaxAttempts is the maximum number of retry attempts.
	// 0 means unlimited retries (use with caution).
	// Default: 3
	MaxAttempts int

	// InitialDelay is the initial delay before first retry.
	// Default: 100ms
	InitialDelay time.Duration

	// MaxDelay is the maximum delay between retries.
	// Default: 30s
	MaxDelay time.Duration

	// Multiplier is the factor by which delay is multiplied after each attempt.
	// Default: 2.0
	Multiplier float64

	// Jitter adds randomness to prevent thundering herd.
	// 0 means no jitter, 1 means full jitter.
	// Default: 0.1
	Jitter float64

	// RetryableError determines if an error is retryable.
	// If nil, all errors are retryable.
	RetryableError func(error) bool
}

Config holds retry configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Retrier

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

Retrier implements retry with exponential backoff.

func NewRetrier

func NewRetrier(config Config) *Retrier

NewRetrier creates a new retrier with the given configuration.

func WithDelay

func WithDelay(delay time.Duration) *Retrier

WithDelay creates a new retrier with the specified initial delay.

func WithMaxAttempts

func WithMaxAttempts(maxAttempts int) *Retrier

WithMaxAttempts creates a new retrier with the specified max attempts.

func WithRetryableError

func WithRetryableError(fn func(error) bool) *Retrier

WithRetryableError creates a new retrier with a custom retryable error function.

func (*Retrier) Do

func (r *Retrier) Do(ctx context.Context, fn func() error) error

Do executes the given function with retry logic.

func (*Retrier) DoWithCallback

func (r *Retrier) DoWithCallback(ctx context.Context, fn func() error, callback func(Attempt)) error

DoWithCallback executes the given function with retry logic and calls the callback after each attempt.

func (*Retrier) DoWithResult

func (r *Retrier) DoWithResult(ctx context.Context, fn func() (interface{}, error)) (interface{}, error)

DoWithResult executes the given function with retry logic and returns a result.

Jump to

Keyboard shortcuts

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