Documentation
¶
Overview ¶
Package retrier provides retry mechanisms for stream processing applications. It offers configurable retry policies with exponential backoff and jitter to handle transient failures gracefully.
Goals: - Provide configurable retry policies for failed operations. - Support exponential backoff with jitter for retry intervals. - Enable integration with stream processors for resilient processing.
Design Philosophy: The retrier package focuses on graceful handling of transient failures through configurable retry strategies. It employs exponential backoff with jitter to prevent thundering herd problems and allows fine-tuning of retry behavior based on application requirements.
Usage Patterns: - Wrap processing functions with retry logic for fault tolerance. - Configure retry policies with maximum attempts and duration limits. - Use RetryableError to indicate errors that should trigger retry attempts.
Example:
package main
import (
"context" "fmt" "github.com/byte4ever/conch/retrier"
)
func main() {
ctx := context.Background()
// Create a retrier with maximum 3 attempts
r, err := retrier.New(
retrier.WithMaxRetry(3),
retrier.WithBackoffFactor(2.0),
)
if err != nil {
fmt.Println("Error creating retrier:", err)
return
}
// Wrap a processor function with retry logic
processor := retrier.WrapProcessorFunc(r, func(ctx context.Context, elem *int) {
fmt.Println("Processing:", *elem)
})
elem := 1
processor(ctx, &elem)
}
Index ¶
- Constants
- Variables
- func WrapProcessorFunc[T domain.Processable[Param, Result], Param any, Result any](retrier *Retrier, f domain.ProcessorFunc[T, Param, Result]) domain.ProcessorFunc[T, Param, Result]
- type BackoffFactorOpt
- type JitterFactorOpt
- type MaxRetryDelay
- type MaxRetryOpt
- type MaxTotalDurationOpt
- type MustRetryFuncOpt
- type Option
- func WithBackoffFactor(backoffFactor float64) Option
- func WithJitterFactor(jitterFactor float64) Option
- func WithMaxRetry(maxRetry int) Option
- func WithMaxRetryDelay(maxDuration time.Duration) Option
- func WithMaxTotalDuration(maxDuration time.Duration) Option
- func WithMustRetryFunc(mustRetryFunc func(error) bool) Option
- func WithNoJitter() Option
- func WithRetryDelay(retryDelay time.Duration) Option
- type Retrier
- type RetryDelayOpt
- type RetryableError
Constants ¶
const ( DefaultRetryDelay = 100 * time.Millisecond DefaultJitterFactor = goldenRatioValue - 1 )
Variables ¶
var ( // ErrRetryable represents an error that indicates a retryable condition. ErrRetryable = errors.New("retryable error") // ErrReachMaxRetry indicates the maximum number of retry attempts was reached. ErrReachMaxRetry = errors.New("reach max retry") // ErrReachMaxDuration indicates the maximum allowable duration was reached. ErrReachMaxDuration = errors.New("reach max duration") )
var ErrDuplicateOption = errors.New("duplicate option")
ErrDuplicateOption represent an error where ....
var ErrInvalidConfiguration = errors.New("invalid configuration")
ErrInvalidConfiguration represent an error where ....
var ErrInvalidValue = errors.New("invalid value")
ErrInvalidValue represent an error where ....
Functions ¶
func WrapProcessorFunc ¶
func WrapProcessorFunc[ T domain.Processable[Param, Result], Param any, Result any]( retrier *Retrier, f domain.ProcessorFunc[T, Param, Result], ) domain.ProcessorFunc[T, Param, Result]
WrapProcessorFunc wraps a ProcessorFunc with retry logic using a Retrier. retrier defines the retry configuration and policies. f is the original ProcessorFunc that will be wrapped with retry handling. Returns a wrapped ProcessorFunc with integrated retry behavior.
Types ¶
type BackoffFactorOpt ¶
type BackoffFactorOpt float64
type JitterFactorOpt ¶
type JitterFactorOpt float64
type MaxRetryDelay ¶
type MaxRetryOpt ¶
type MaxRetryOpt int
type MaxTotalDurationOpt ¶
type MustRetryFuncOpt ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithBackoffFactor ¶
func WithJitterFactor ¶
func WithMaxRetry ¶
func WithMaxRetryDelay ¶
func WithMaxTotalDuration ¶
func WithMustRetryFunc ¶
func WithNoJitter ¶
func WithNoJitter() Option
func WithRetryDelay ¶
type Retrier ¶
type Retrier struct {
// contains filtered or unexported fields
}
Retrier is a generic type for handling retryable operations. It encapsulates retry logic, configuration, and coefficient calculations.
type RetryDelayOpt ¶
type RetryableError ¶
type RetryableError struct {
Err error // The underlying error wrapped by RetryableError
}
RetryableError wraps an error to indicate it is a retryable condition. It implements the error interface and provides unwrapping functionality.
func WrapToRetryableError ¶
func WrapToRetryableError(e error) *RetryableError
WrapToRetryableError wraps the given error into a RetryableError instance.
func (*RetryableError) Error ¶
func (re *RetryableError) Error() string
Error returns the string representation of the RetryableError.
func (*RetryableError) Is ¶
func (re *RetryableError) Is(e error) bool
Is checks if the given error matches the ErrRetryable error type.
func (*RetryableError) Unwrap ¶
func (re *RetryableError) Unwrap() error
Unwrap returns the underlying error contained in the RetryableError.