Documentation
¶
Index ¶
- func Do[T any](ctx context.Context, action func(context.Context) (T, error), opts ...Option) (T, error)
- func DoErr(ctx context.Context, action func(context.Context) error, opts ...Option) error
- func DoErrState(ctx context.Context, action func(context.Context, RetryState) error, ...) error
- func DoState[T any](ctx context.Context, action func(context.Context, RetryState) (T, error), ...) (T, error)
- func FatalError(err error) error
- func WithTestingBypass(ctx context.Context) context.Context
- type Backoff
- type Config
- type Instrumenter
- type Option
- func WithBackoff(backoff Backoff) Option
- func WithBaseDelay(delay time.Duration) Option
- func WithCircuitBreaker(cb *circuit.Breaker) Option
- func WithInstrumenter(instr Instrumenter) Option
- func WithMaxAttempts(attempts uint) Option
- func WithMaxDelay(delay time.Duration) Option
- func WithName(name string) Option
- func WithRetryIf(target error) Option
- func WithRetryIfFunc(f func(error) bool) Option
- type RetryAfterError
- type RetryState
- type Retryer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
func Do[T any](ctx context.Context, action func(context.Context) (T, error), opts ...Option) (T, error)
Do executes an action with retry logic using the provided options. This generic function handles functions returning (T, error).
func DoErr ¶
DoErr executes an action with retry logic using the provided options. This function handles functions returning only error.
func DoErrState ¶
func DoErrState(ctx context.Context, action func(context.Context, RetryState) error, opts ...Option) error
DoErrState executes a stateful action with retry logic using the provided options.
func DoState ¶
func DoState[T any](ctx context.Context, action func(context.Context, RetryState) (T, error), opts ...Option) (T, error)
DoState executes a stateful action with retry logic using the provided options. The RetryState is passed to the closure, allowing it to adapt to failure history.
func FatalError ¶
FatalError wraps an error to indicate that the retry loop should terminate immediately.
Types ¶
type Backoff ¶
type Backoff interface {
// Next calculates the duration to wait before the specified attempt.
// The attempt parameter is 0-indexed.
Next(attempt uint) time.Duration
}
Backoff defines the interface for temporal distribution of retries.
func NewFullJitter ¶
NewFullJitter returns a Backoff implementation using the AWS Full Jitter algorithm. The base duration dictates the initial delay, and the cap defines the absolute maximum.
type Config ¶
type Config struct {
Name string
MaxAttempts uint
BaseDelay time.Duration
MaxDelay time.Duration
Backoff Backoff
Policy *retryPolicy
Instrumenter Instrumenter
CircuitBreaker *circuit.Breaker
}
Config represents the configuration for the retry execution.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a reasonable production-grade configuration.
type Instrumenter ¶
type Instrumenter interface {
// BeforeAttempt is called before each execution attempt.
// It can return a new context (e.g., to inject trace spans).
BeforeAttempt(ctx context.Context, state RetryState) context.Context
// AfterAttempt is called after each execution attempt.
AfterAttempt(ctx context.Context, state RetryState)
}
Instrumenter defines the lifecycle hooks for monitoring retry executions. It is a zero-dependency interface to allow custom implementations for logging, metrics, and tracing.
type Option ¶
type Option func(*Config)
Option defines a functional option for configuring a retry execution.
func WithBackoff ¶
WithBackoff sets a custom backoff algorithm.
func WithBaseDelay ¶
WithBaseDelay sets the initial delay for the backoff algorithm.
func WithCircuitBreaker ¶
WithCircuitBreaker integrates a circuit breaker into the retry execution.
func WithInstrumenter ¶
func WithInstrumenter(instr Instrumenter) Option
WithInstrumenter sets a telemetry instrumenter.
func WithMaxAttempts ¶
WithMaxAttempts sets the maximum number of execution attempts.
func WithMaxDelay ¶
WithMaxDelay sets the maximum delay for the backoff algorithm.
func WithRetryIf ¶
WithRetryIf sets a specific error to trigger a retry.
func WithRetryIfFunc ¶
WithRetryIfFunc sets a custom function to determine if an error should be retried.
type RetryAfterError ¶
RetryAfterError is implemented by errors that specify how long to wait before retrying. This is commonly used with HTTP 429 (Too Many Requests) or 503 (Service Unavailable) to respect Retry-After headers.
type RetryState ¶
type RetryState struct {
// Name is the optional identifier for the operation being retried.
Name string
// Attempt is the current 0-indexed retry iteration.
Attempt uint
// MaxAttempts is the maximum number of attempts allowed.
MaxAttempts uint
// LastError is the error encountered in the previous attempt.
LastError error
// TotalDuration is the cumulative time spent across all attempts and sleeps.
TotalDuration time.Duration
// NextDelay is the duration to be slept before the next attempt.
NextDelay time.Duration
}
RetryState encapsulates the current state of a retry execution.
type Retryer ¶
type Retryer interface {
// Do executes a function that returns a value and an error.
Do(ctx context.Context, action func(context.Context) (any, error)) (any, error)
// DoErr executes a function that returns only an error.
DoErr(ctx context.Context, action func(context.Context) error) error
}
Retryer defines the interface for executing actions with resilience.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
circuitbreaker
command
|
|
|
http
command
|
|
|
stateful
command
|
|
|
telemetry
|
|