retrier

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

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

View Source
const (
	DefaultRetryDelay   = 100 * time.Millisecond
	DefaultJitterFactor = goldenRatioValue - 1
)

Variables

View Source
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")
)
View Source
var ErrDuplicateOption = errors.New("duplicate option")

ErrDuplicateOption represent an error where ....

View Source
var ErrInvalidConfiguration = errors.New("invalid configuration")

ErrInvalidConfiguration represent an error where ....

View Source
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 MaxRetryDelay time.Duration

type MaxRetryOpt

type MaxRetryOpt int

type MaxTotalDurationOpt

type MaxTotalDurationOpt time.Duration

type MustRetryFuncOpt

type MustRetryFuncOpt func(error) bool

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithBackoffFactor

func WithBackoffFactor(backoffFactor float64) Option

func WithJitterFactor

func WithJitterFactor(jitterFactor float64) Option

func WithMaxRetry

func WithMaxRetry(maxRetry int) Option

func WithMaxRetryDelay

func WithMaxRetryDelay(maxDuration time.Duration) Option

func WithMaxTotalDuration

func WithMaxTotalDuration(maxDuration time.Duration) Option

func WithMustRetryFunc

func WithMustRetryFunc(mustRetryFunc func(error) bool) Option

func WithNoJitter

func WithNoJitter() Option

func WithRetryDelay

func WithRetryDelay(retryDelay time.Duration) Option

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.

func New

func New(options ...Option) (*Retrier, error)

New creates a new Retrier instance with the given configuration options. It returns a pointer to the Retrier and an error if configuration fails.

type RetryDelayOpt

type RetryDelayOpt time.Duration

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL