Documentation
¶
Overview ¶
Package retry provides retry functionality with various jitter algorithms and backoff strategies. It includes implementations of full jitter, equal jitter, decorrelated jitter, and exponential backoff to prevent thundering herd problems and improve system resilience.
Index ¶
- func CalculateJitteredDelay(jitterType JitterType, baseDelay, maxDelay time.Duration, attempt int) time.Duration
- func DefaultRetryIf(err error) bool
- func Do(ctx context.Context, operation Operation, opts ...Option) error
- func DoWithResult[T any](ctx context.Context, operation OperationWithResult[T], opts ...Option) (T, error)
- func ExecuteWithJitter(retryFunc func() error, config *JitterConfig, maxAttempts int) error
- func IsPermanent(err error) bool
- func IsRetryable(err error) bool
- func MarkPermanent(err error) error
- type JitterConfig
- type JitterOption
- type JitterType
- type JitteredRetryFunc
- type Operation
- type OperationWithResult
- type Option
- func WithDelay(d time.Duration) Option
- func WithExponentialBackoff(initialDelay time.Duration, multiplier float64) Option
- func WithJitter(factor float64) Option
- func WithMaxDelay(d time.Duration) Option
- func WithMaxRetries(n int) Option
- func WithOnRetry(fn func(n int, err error)) Option
- func WithRetryIf(fn func(error) bool) Option
- type Permanent
- type RetryableError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateJitteredDelay ¶
func CalculateJitteredDelay(jitterType JitterType, baseDelay, maxDelay time.Duration, attempt int) time.Duration
CalculateJitteredDelay is a convenience function for calculating jittered delays
func DefaultRetryIf ¶
DefaultRetryIf is a retry condition that retries all errors except permanent ones
func DoWithResult ¶
func DoWithResult[T any](ctx context.Context, operation OperationWithResult[T], opts ...Option) (T, error)
DoWithResult executes an operation that returns a result with retry logic
func ExecuteWithJitter ¶
func ExecuteWithJitter(retryFunc func() error, config *JitterConfig, maxAttempts int) error
ExecuteWithJitter wraps a retry function with jitter configuration
func IsPermanent ¶
IsPermanent checks if an error is marked as permanent (non-retryable)
func IsRetryable ¶
IsRetryable checks if an error implements RetryableError and is retryable
func MarkPermanent ¶
MarkPermanent marks an error as permanent (non-retryable)
Types ¶
type JitterConfig ¶
type JitterConfig struct {
Type JitterType
BaseDelay time.Duration
MaxDelay time.Duration
Multiplier float64
RandomSeed int64
// contains filtered or unexported fields
}
JitterConfig holds configuration for jitter calculations
func DefaultJitterConfig ¶
func DefaultJitterConfig() *JitterConfig
DefaultJitterConfig returns a sensible default jitter configuration
func NewJitterConfig ¶
func NewJitterConfig(jitterType JitterType, baseDelay, maxDelay time.Duration) *JitterConfig
NewJitterConfig creates a new jitter configuration
func NewJitterConfigWithOptions ¶
func NewJitterConfigWithOptions(opts ...JitterOption) *JitterConfig
NewJitterConfigWithOptions creates a jitter configuration with options
func (*JitterConfig) CalculateDelay ¶
func (jc *JitterConfig) CalculateDelay(attempt int) time.Duration
CalculateDelay calculates the delay for a given attempt with jitter
type JitterOption ¶
type JitterOption func(*JitterConfig)
JitterOption allows customization of jitter configuration
func WithDelays ¶
func WithDelays(baseDelay, maxDelay time.Duration) JitterOption
WithDelays sets the base and max delays
func WithJitterType ¶
func WithJitterType(jitterType JitterType) JitterOption
WithJitterType sets the jitter type
func WithMultiplier ¶
func WithMultiplier(multiplier float64) JitterOption
WithMultiplier sets the exponential backoff multiplier
func WithRandomSeed ¶
func WithRandomSeed(seed int64) JitterOption
WithRandomSeed sets a specific random seed (useful for testing)
type JitterType ¶
type JitterType int
JitterType defines different types of jitter algorithms
const ( // NoJitter applies no jitter to the delay NoJitter JitterType = iota // FullJitter applies full jitter (0 to base_delay) FullJitter // EqualJitter applies equal jitter (base_delay/2 + random(base_delay/2)) EqualJitter DecorrelatedJitter // ExponentialJitter applies exponential backoff with jitter ExponentialJitter )
type JitteredRetryFunc ¶
type JitteredRetryFunc func(attempt int, config *JitterConfig) time.Duration
JitteredRetryFunc represents a function that implements retry logic with jitter
type OperationWithResult ¶
OperationWithResult is a function that returns a result and can be retried
type Option ¶
type Option func(*config)
Option configures retry behavior
func WithExponentialBackoff ¶
WithExponentialBackoff enables exponential backoff with the given multiplier
func WithJitter ¶
WithJitter adds randomness to retry delays (0.0 to 1.0)
func WithMaxDelay ¶
WithMaxDelay sets the maximum delay between retries
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retry attempts
func WithOnRetry ¶
WithOnRetry sets a callback function called before each retry
func WithRetryIf ¶
WithRetryIf sets a custom function to determine if an error is retryable
type Permanent ¶
type Permanent struct {
Err error
}
Permanent wraps an error to indicate it should not be retried
type RetryableError ¶
RetryableError interface for errors that can indicate if they're retryable