Documentation
¶
Overview ¶
Package retry provides a generic retry mechanism with configurable backoff strategies.
Index ¶
- func Do(ctx context.Context, cfg Config, fn func(ctx context.Context) error) error
- func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
- func Exponential(ctx context.Context, maxAttempts int, initDelay time.Duration, ...) error
- func Simple(ctx context.Context, maxAttempts int, delay time.Duration, ...) error
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do retries fn until it succeeds, max attempts are exhausted, or ctx is cancelled. Returns the last error if all attempts fail.
func DoWithResult ¶
func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
DoWithResult retries fn until it succeeds and returns both the result and error.
Types ¶
type Config ¶
type Config struct {
MaxAttempts int
InitDelay time.Duration
MaxDelay time.Duration
Multiplier float64 // backoff multiplier (1.0 = constant, 2.0 = exponential)
Jitter bool // add random jitter to delay
RetryIf func(error) bool // only retry if this returns true; nil means always retry
}
Config configures the retry behavior.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a config with 3 attempts, exponential backoff starting at 100ms.
Click to show internal directories.
Click to hide internal directories.