Documentation ¶
Index ¶
- Constants
- Variables
- func Do(ctx context.Context, cfg Config, fn func(ctx context.Context) error) error
- func Do1[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
- func Restartable(err error) error
- func Retriable(err error) error
- type Config
- type ErrRestart
- type ErrRetry
Constants ¶
View Source
const NoJitter = -1
NoJitter is a jitter value that disables jitter
Variables ¶
View Source
var NoLog = slog.New(discardHandler{})
NoLog is a logger that discards all log messages, to be removed once in stdlib
Functions ¶
func Do ¶
Do runs fn with retries controlled by config
fn triggers a retry by returning ErrRetry or ErrRestart. Any other return value ends the retry and is returned to the caller.
Context passed to fn is valid only during one attempt, and may or may not be canceled afterwards.
func Do1 ¶
func Do1[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
Do1 is a version of Do with one return value
func Restartable ¶
Restartable wraps the error in ErrRestart if it is not nil
Typical usage is to wrap a potential error known to require a restart.
Types ¶
type Config ¶
type Config struct { // Delay is a delay between attempts. It is scaled by Scale for each // consecutive attempt until it reaches MaxDelay // // This field is required. Delay time.Duration // Scale is a exponential scale for delay. // // Defaults to 1 (no scaling, constant delay), can't be less than 1. Scale float64 // Jitter is the amount of jitter to add to the delay. // // Defaults to 0.125 (12.5%), and has to be within [0,1]. // To disable jitter, set this field to NoJitter. Jitter float64 // PreDelay is optional delay before first try. // // Defaults to 0. PreDelay time.Duration // MaxDelay is a cap on delay scaling. // // Defaults to no maximum. MaxDelay time.Duration // Timeout is a maximum total time to retry. // // If timeout is reached then the context passed to the called function // will be called, and retry won't be attempted when the function returns. // // Note that if called function should handle context cancellation // for aborting the operation by timeout. // // Defaults to no timeout. Timeout time.Duration // Logger is a logger for retries // // This package logs retriable errors returned by an invoked function. // It omits logging identical subsequent errors. // // Defaults to slog.Default. Set to NoLog to disable logging. Logger *slog.Logger // LogLevel is a log level for retries // // Defaults to slog.Debug. LogLevel slog.Level // contains filtered or unexported fields }
Config configures the retry
type ErrRestart ¶
type ErrRestart struct {
// contains filtered or unexported fields
}
ErrRestart signals the restart of retry attempts, resetting both delay and timeout
func (ErrRestart) Error ¶
func (e ErrRestart) Error() string
func (ErrRestart) Unwrap ¶
func (e ErrRestart) Unwrap() error
Click to show internal directories.
Click to hide internal directories.