Documentation
¶
Overview ¶
Package retrier provides a generic retry mechanism with exponential backoff and jitter.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BackoffParam ¶
type BackoffParam struct {
// contains filtered or unexported fields
}
BackoffParam holds the parameters for exponential backoff calculation. Example usage:
params := NewBackoffParam(
1 * time.Second, // Start with 1s
2.0, // Double each time
30 * time.Second, // Cap at 30s
)
func NewBackoffParam ¶
func NewBackoffParam( initialDuration time.Duration, multiplier float64, maxDuration time.Duration, ) BackoffParam
NewBackoffParam creates a new BackoffParam with the given settings.
func (*BackoffParam) InitialDuration ¶
func (b *BackoffParam) InitialDuration() time.Duration
InitialDuration returns the initial backoff duration.
func (*BackoffParam) MaxDuration ¶
func (b *BackoffParam) MaxDuration() time.Duration
MaxDuration returns the maximum backoff duration.
func (*BackoffParam) Multiplier ¶
func (b *BackoffParam) Multiplier() float64
Multiplier returns the backoff multiplier.
type DebugLogger ¶
type DebugLogger interface {
// Enabled returns true if debug logging is enabled.
// When false, the retry handler will skip logging entirely for efficiency.
Enabled() bool
// LogRetry logs retry attempts with backoff information.
// Parameters:
// - ctx: context for the retry operation
// - attempt: current attempt number (1-based)
// - maxAttempts: maximum number of attempts allowed
// - backoff: the delay before the next retry (0 for success/exhausted)
// - err: the error that triggered the retry (nil on success)
LogRetry(ctx context.Context, attempt int, maxAttempts int, backoff time.Duration, err error)
}
DebugLogger provides structured debug logging capabilities for the retry package. Users can implement this interface to provide custom logging behavior. When debug mode is disabled, use NoOpLogger for zero overhead.
type NoOpLogger ¶
type NoOpLogger struct{}
NoOpLogger is a no-operation implementation of DebugLogger. It provides zero overhead when debug mode is disabled. All methods are empty and Enabled() always returns false.
func NewNoOpLogger ¶
func NewNoOpLogger() *NoOpLogger
NewNoOpLogger creates a new NoOpLogger instance.
func (*NoOpLogger) Enabled ¶
func (n *NoOpLogger) Enabled() bool
Enabled returns false - debug logging is disabled.
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result encapsulates the immutable outcome of a retry operation. It holds either a successful value or an error, along with metadata about the execution.
func NewFailureResult ¶
func NewFailureResult[T any](err RetryableError, attempts int) Result[T]
NewFailureResult creates a Result representing a failed retry operation.
func NewSuccessResult ¶
NewSuccessResult creates a Result representing a successful retry operation.
func Retry ¶
func Retry[T any](retryParam RetryParam, logger DebugLogger, fn func() (T, RetryableError)) Result[T]
Retry executes the provided function with retry logic. It will retry the function up to MaxAttempts times, applying exponential backoff with jitter between attempts. Only retryable errors will trigger a retry.
Type parameter T represents the return type of the function being retried. Returns a Result containing the value (if successful), error (if failed), and the number of attempts made.
The logger parameter provides debug logging capabilities. When debug mode is disabled (NoOpLogger), there is zero overhead from logging.
func (Result[T]) Err ¶
func (r Result[T]) Err() RetryableError
Err returns the error if the operation failed, or nil if successful.
type RetryError ¶
type RetryError struct {
Message string
Cause RetryErrorCause
// contains filtered or unexported fields
}
RetryError represents an error that occurred during retry attempts. It stores the original error for debugging and implements the RetryableError interface.
func NewRetryError ¶
func NewRetryError(cause RetryErrorCause, message string, policy RetryPolicy, wrapped error) *RetryError
NewRetryError creates a new RetryError with explicit classification. Parameters:
- cause: The error cause (ErrZeroAttempt or ErrExhaustedAttempts)
- message: Human-readable error message
- policy: RetryPolicyAuto, RetryPolicyManual, or RetryPolicyNever
- wrapped: The original error that caused the retry failure (may be nil)
func (*RetryError) Error ¶
func (e *RetryError) Error() string
Error returns the error message implementing the error interface.
func (*RetryError) Is ¶
func (e *RetryError) Is(target error) bool
Is allows errors.Is to match RetryError types.
func (*RetryError) RetryPolicy ¶
func (e *RetryError) RetryPolicy() RetryPolicy
RetryPolicy returns the automatic retry behavior for this error. When RetryError is returned (exhausted attempts), it returns the cached policy.
func (*RetryError) Unwrap ¶
func (e *RetryError) Unwrap() error
Unwrap returns the wrapped error for error chain support.
type RetryErrorCause ¶
type RetryErrorCause string
RetryErrorCause represents the cause of a retry error.
const ( // ErrZeroAttempt indicates that MaxAttempts was set to 0. ErrZeroAttempt RetryErrorCause = "zero attempt" // ErrExhaustedAttempts indicates that all retry attempts were exhausted. ErrExhaustedAttempts RetryErrorCause = "exhausted attempt" )
type RetryParam ¶
type RetryParam struct {
BaseDelay time.Duration
Jitter time.Duration
RandomSeed int64
MaxAttempts int
BackoffParam BackoffParam
}
RetryParam holds the parameters for retry logic. These parameters are passed from outside (e.g., config) and should not be known by the retry handler internally.
func NewRetryParam ¶
func NewRetryParam( baseDelay time.Duration, jitter time.Duration, randomSeed int64, maxAttempts int, backoffParam BackoffParam, ) RetryParam
NewRetryParam creates a new RetryParam with the given settings.
type RetryPolicy ¶
type RetryPolicy int
RetryPolicy defines automatic retry behavior. This controls whether Retry() will attempt exponential backoff.
const ( // RetryPolicyAuto indicates the error should be retried automatically // with exponential backoff. RetryPolicyAuto RetryPolicy = iota // RetryPolicyManual indicates the error should not be auto-retried, // but is eligible for manual retry. RetryPolicyManual // RetryPolicyNever indicates a permanent failure that should not be retried. RetryPolicyNever )
type RetryableError ¶
type RetryableError interface {
error
// RetryPolicy controls automatic retry behavior.
// Used by the retry handler to decide whether to retry.
RetryPolicy() RetryPolicy
}
RetryableError is an interface that errors must implement to be handled by the retry mechanism. Users should implement this interface on their custom error types to control retry behavior.