Documentation
¶
Index ¶
- func Do(ctx context.Context, cfg Config, operation Operation) error
- func DoWithNotify(ctx context.Context, cfg Config, operation Operation, ...) error
- func Permanent(err error) error
- type Config
- func (c Config) WithInitialInterval(interval time.Duration) Config
- func (c Config) WithMaxInterval(interval time.Duration) Config
- func (c Config) WithMaxRetries(maxRetries uint64) Config
- func (c Config) WithMultiplier(multiplier float64) Config
- func (c Config) WithName(name string) Config
- func (c Config) WithOTel(otelConfig *pkgotel.Config) Config
- func (c Config) WithRandomizationFactor(factor float64) Config
- type Operation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do executes the operation with retry logic using exponential backoff. It returns nil if the operation succeeds, or the last error if all retries are exhausted.
Example:
cfg := retry.DefaultConfig().
WithName("database.connect").
WithMaxRetries(3).
WithOTel(otelConfig)
err := retry.Do(ctx, cfg, func(ctx context.Context) error {
return db.Ping()
})
func DoWithNotify ¶
func DoWithNotify( ctx context.Context, cfg Config, operation Operation, notifyFunc func(error, time.Duration), ) error
DoWithNotify is like Do but calls notifyFunc on each retry with the error and backoff duration. This is useful for custom logging or metrics.
Example:
err := retry.DoWithNotify(ctx, cfg, operation, func(err error, backoff time.Duration) {
log.Printf("Retry after error: %v (waiting %v)", err, backoff)
})
Types ¶
type Config ¶
type Config struct {
// MaxRetries is the maximum number of retries after the initial attempt
// (0 means unlimited retries). With MaxRetries = N the operation is called
// at most N+1 times: 1 initial attempt plus up to N retries.
// Default: 5
MaxRetries uint64
// InitialInterval is the initial retry interval.
// Default: 500ms
InitialInterval time.Duration
// MaxInterval caps the maximum retry interval.
// Default: 60s
MaxInterval time.Duration
// Multiplier is the exponential backoff multiplier. Must be > 1.
// Default: 2.0 (each retry waits 2x longer)
Multiplier float64
// RandomizationFactor adds jitter to backoff intervals to prevent thundering herd.
// Must be in [0, 1). 0.0 means no randomization, 0.5 means +/-50% jitter.
// Default: 0.5
RandomizationFactor float64
// OperationName is used for logging and tracing.
// Default: "retry.operation"
OperationName string
// OTelConfig enables OpenTelemetry tracing and logging.
// Optional: if nil, no OTel instrumentation.
OTelConfig *pkgotel.Config
}
Config holds retry configuration with exponential backoff.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults.
func (Config) WithInitialInterval ¶
WithInitialInterval sets the initial retry interval.
func (Config) WithMaxInterval ¶
WithMaxInterval sets the maximum retry interval.
func (Config) WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries after the initial attempt.
func (Config) WithMultiplier ¶
WithMultiplier sets the exponential backoff multiplier. Panics if multiplier <= 1.
func (Config) WithRandomizationFactor ¶ added in v2.7.11
WithRandomizationFactor sets the jitter factor for backoff intervals. A value of 0.5 means intervals will vary by +/-50%. Set to 0.0 to disable jitter. Panics if factor is not in [0, 1).