retry

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package retry provides retry strategies and mechanisms for handling transient failures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteWithRetry

func ExecuteWithRetry(ctx context.Context, strategy Strategy, operation func() error) error

ExecuteWithRetry is a convenience function that executes an operation with the specified strategy.

Types

type BackoffType

type BackoffType int

BackoffType represents the type of backoff strategy.

const (
	// ExponentialBackoff increases delay exponentially with each attempt.
	ExponentialBackoff BackoffType = iota

	// LinearBackoff increases delay linearly with each attempt.
	LinearBackoff

	// ConstantBackoff uses a constant delay between attempts.
	ConstantBackoff

	// CustomBackoff uses custom intervals provided by the user.
	CustomBackoff
)

type ConstantStrategy

type ConstantStrategy struct {
	MaxRetries   int
	Delay        time.Duration
	Jitter       bool
	JitterFactor float64
	RetryChecker func(error) bool
}

ConstantStrategy implements constant delay backoff.

func NewConstantStrategy

func NewConstantStrategy() *ConstantStrategy

NewConstantStrategy creates a new constant delay strategy.

func (*ConstantStrategy) MaxAttempts

func (s *ConstantStrategy) MaxAttempts() int

MaxAttempts returns the maximum number of retry attempts.

func (*ConstantStrategy) NextDelay

func (s *ConstantStrategy) NextDelay(attempt int) time.Duration

NextDelay returns the constant delay.

func (*ConstantStrategy) ShouldRetry

func (s *ConstantStrategy) ShouldRetry(err error, attempt int) bool

ShouldRetry determines if an operation should be retried.

func (*ConstantStrategy) WithDelay

func (s *ConstantStrategy) WithDelay(delay time.Duration) *ConstantStrategy

WithDelay sets the constant delay between retries.

func (*ConstantStrategy) WithJitter

func (s *ConstantStrategy) WithJitter(enabled bool, factor float64) *ConstantStrategy

WithJitter enables or disables jitter.

func (*ConstantStrategy) WithMaxRetries

func (s *ConstantStrategy) WithMaxRetries(maxRetries int) *ConstantStrategy

WithMaxRetries sets the maximum number of retry attempts.

func (*ConstantStrategy) WithRetryChecker

func (s *ConstantStrategy) WithRetryChecker(checker func(error) bool) *ConstantStrategy

WithRetryChecker sets a custom function to determine if an error should be retried.

type CustomStrategy

type CustomStrategy struct {
	Intervals    []time.Duration
	RetryChecker func(error) bool
}

CustomStrategy implements custom retry intervals.

func NewCustomStrategy

func NewCustomStrategy(intervals []time.Duration) *CustomStrategy

NewCustomStrategy creates a new custom interval strategy.

func (*CustomStrategy) MaxAttempts

func (s *CustomStrategy) MaxAttempts() int

MaxAttempts returns the maximum number of retry attempts.

func (*CustomStrategy) NextDelay

func (s *CustomStrategy) NextDelay(attempt int) time.Duration

NextDelay returns the delay for the specified attempt.

func (*CustomStrategy) ShouldRetry

func (s *CustomStrategy) ShouldRetry(err error, attempt int) bool

ShouldRetry determines if an operation should be retried.

func (*CustomStrategy) WithRetryChecker

func (s *CustomStrategy) WithRetryChecker(checker func(error) bool) *CustomStrategy

WithRetryChecker sets a custom function to determine if an error should be retried.

type ExponentialStrategy

type ExponentialStrategy struct {
	MaxRetries    int
	BaseDelay     time.Duration
	MaxDelay      time.Duration
	BackoffFactor float64
	Jitter        bool
	JitterFactor  float64 // How much randomness to add (0.0 to 1.0)
	RetryChecker  func(error) bool
}

ExponentialStrategy implements exponential backoff with optional jitter.

func NewExponentialStrategy

func NewExponentialStrategy() *ExponentialStrategy

NewExponentialStrategy creates a new exponential backoff strategy with sensible defaults.

func (*ExponentialStrategy) MaxAttempts

func (s *ExponentialStrategy) MaxAttempts() int

MaxAttempts returns the maximum number of retry attempts.

func (*ExponentialStrategy) NextDelay

func (s *ExponentialStrategy) NextDelay(attempt int) time.Duration

NextDelay calculates the delay for the next retry attempt using exponential backoff.

func (*ExponentialStrategy) ShouldRetry

func (s *ExponentialStrategy) ShouldRetry(err error, attempt int) bool

ShouldRetry determines if an operation should be retried.

func (*ExponentialStrategy) WithBackoffFactor

func (s *ExponentialStrategy) WithBackoffFactor(factor float64) *ExponentialStrategy

WithBackoffFactor sets the multiplier for exponential backoff.

func (*ExponentialStrategy) WithBaseDelay

func (s *ExponentialStrategy) WithBaseDelay(baseDelay time.Duration) *ExponentialStrategy

WithBaseDelay sets the base delay for the first retry.

func (*ExponentialStrategy) WithJitter

func (s *ExponentialStrategy) WithJitter(enabled bool, factor float64) *ExponentialStrategy

WithJitter enables or disables jitter and sets the jitter factor.

func (*ExponentialStrategy) WithMaxDelay

func (s *ExponentialStrategy) WithMaxDelay(maxDelay time.Duration) *ExponentialStrategy

WithMaxDelay sets the maximum delay between retries.

func (*ExponentialStrategy) WithMaxRetries

func (s *ExponentialStrategy) WithMaxRetries(maxRetries int) *ExponentialStrategy

WithMaxRetries sets the maximum number of retry attempts.

func (*ExponentialStrategy) WithRetryChecker

func (s *ExponentialStrategy) WithRetryChecker(checker func(error) bool) *ExponentialStrategy

WithRetryChecker sets a custom function to determine if an error should be retried.

type LinearStrategy

type LinearStrategy struct {
	MaxRetries   int
	BaseDelay    time.Duration
	MaxDelay     time.Duration
	Increment    time.Duration
	Jitter       bool
	JitterFactor float64
	RetryChecker func(error) bool
}

LinearStrategy implements linear backoff.

func NewLinearStrategy

func NewLinearStrategy() *LinearStrategy

NewLinearStrategy creates a new linear backoff strategy.

func (*LinearStrategy) MaxAttempts

func (s *LinearStrategy) MaxAttempts() int

MaxAttempts returns the maximum number of retry attempts.

func (*LinearStrategy) NextDelay

func (s *LinearStrategy) NextDelay(attempt int) time.Duration

NextDelay calculates the delay for the next retry attempt using linear backoff.

func (*LinearStrategy) ShouldRetry

func (s *LinearStrategy) ShouldRetry(err error, attempt int) bool

ShouldRetry determines if an operation should be retried.

func (*LinearStrategy) WithBaseDelay

func (s *LinearStrategy) WithBaseDelay(baseDelay time.Duration) *LinearStrategy

WithBaseDelay sets the base delay for the first retry.

func (*LinearStrategy) WithIncrement

func (s *LinearStrategy) WithIncrement(increment time.Duration) *LinearStrategy

WithIncrement sets the linear increment for each retry.

func (*LinearStrategy) WithJitter

func (s *LinearStrategy) WithJitter(enabled bool, factor float64) *LinearStrategy

WithJitter enables or disables jitter.

func (*LinearStrategy) WithMaxDelay

func (s *LinearStrategy) WithMaxDelay(maxDelay time.Duration) *LinearStrategy

WithMaxDelay sets the maximum delay between retries.

func (*LinearStrategy) WithMaxRetries

func (s *LinearStrategy) WithMaxRetries(maxRetries int) *LinearStrategy

WithMaxRetries sets the maximum number of retry attempts.

func (*LinearStrategy) WithRetryChecker

func (s *LinearStrategy) WithRetryChecker(checker func(error) bool) *LinearStrategy

WithRetryChecker sets a custom function to determine if an error should be retried.

type RetryManager

type RetryManager struct {
	MaxRetries    int           // Maximum number of retry attempts
	BaseDelay     time.Duration // Base delay for the first retry
	MaxDelay      time.Duration // Maximum delay between retries
	BackoffFactor float64       // Multiplier for exponential backoff
	Jitter        bool          // Whether to add jitter to delays
}

RetryManager manages retry attempts with configurable backoff strategies.

func AggressiveRetryManager

func AggressiveRetryManager() *RetryManager

AggressiveRetryManager returns a retry manager with more aggressive retry attempts.

func ConservativeRetryManager

func ConservativeRetryManager() *RetryManager

ConservativeRetryManager returns a retry manager with fewer, more spaced-out retry attempts.

func DefaultRetryManager

func DefaultRetryManager() *RetryManager

DefaultRetryManager returns a retry manager with sensible defaults for general use.

func FileSystemRetryManager

func FileSystemRetryManager() *RetryManager

FileSystemRetryManager returns a retry manager optimized for file system operations.

func NetworkRetryManager

func NetworkRetryManager() *RetryManager

NetworkRetryManager returns a retry manager optimized for network operations.

func NewRetryManager

func NewRetryManager() *RetryManager

NewRetryManager creates a new RetryManager with default settings.

func NewRetryManagerWithConfig

func NewRetryManagerWithConfig(
	maxRetries int,
	baseDelay, maxDelay time.Duration,
	backoffFactor float64,
	jitter bool,
) *RetryManager

NewRetryManagerWithConfig creates a new RetryManager with the specified configuration.

func (*RetryManager) ExecuteWithRetry

func (rm *RetryManager) ExecuteWithRetry(ctx context.Context, operation func() error) error

ExecuteWithRetry executes an operation with retry logic using the manager's configuration.

func (*RetryManager) ExecuteWithRetryAndStats

func (rm *RetryManager) ExecuteWithRetryAndStats(
	ctx context.Context,
	operation func() error,
) (*Stats, error)

ExecuteWithRetryAndStats executes an operation with retry logic and returns detailed statistics.

func (*RetryManager) ExecuteWithRetryCallback

func (rm *RetryManager) ExecuteWithRetryCallback(
	ctx context.Context,
	operation func() error,
	onRetry func(attempt int, err error, nextDelay time.Duration),
) error

ExecuteWithRetryCallback executes an operation with retry logic and calls a callback on each retry.

func (*RetryManager) NewRetryableOperation

func (rm *RetryManager) NewRetryableOperation(operation func() error) *RetryableOperation

NewRetryableOperation creates a new retryable operation.

func (*RetryManager) NextDelay

func (rm *RetryManager) NextDelay(attempt int) time.Duration

NextDelay calculates the delay for the next retry attempt using exponential backoff.

func (*RetryManager) ShouldRetry

func (rm *RetryManager) ShouldRetry(err error, attempt int) bool

ShouldRetry determines whether an error should be retried based on the error type and attempt number.

func (*RetryManager) WithBackoffFactor

func (rm *RetryManager) WithBackoffFactor(factor float64) *RetryManager

WithBackoffFactor returns a new RetryManager with the specified backoff factor.

func (*RetryManager) WithBaseDelay

func (rm *RetryManager) WithBaseDelay(baseDelay time.Duration) *RetryManager

WithBaseDelay returns a new RetryManager with the specified base delay.

func (*RetryManager) WithJitter

func (rm *RetryManager) WithJitter(enabled bool) *RetryManager

WithJitter returns a new RetryManager with jitter enabled or disabled.

func (*RetryManager) WithMaxDelay

func (rm *RetryManager) WithMaxDelay(maxDelay time.Duration) *RetryManager

WithMaxDelay returns a new RetryManager with the specified maximum delay.

func (*RetryManager) WithMaxRetries

func (rm *RetryManager) WithMaxRetries(maxRetries int) *RetryManager

WithMaxRetries returns a new RetryManager with the specified maximum retries.

type RetryableOperation

type RetryableOperation struct {
	// contains filtered or unexported fields
}

RetryableOperation wraps an operation to make it retryable with the manager.

func (*RetryableOperation) Execute

func (ro *RetryableOperation) Execute(ctx context.Context) error

Execute runs the retryable operation.

func (*RetryableOperation) ExecuteWithCallback

func (ro *RetryableOperation) ExecuteWithCallback(
	ctx context.Context,
	onRetry func(attempt int, err error, nextDelay time.Duration),
) error

ExecuteWithCallback runs the retryable operation with a retry callback.

func (*RetryableOperation) ExecuteWithStats

func (ro *RetryableOperation) ExecuteWithStats(ctx context.Context) (*Stats, error)

ExecuteWithStats runs the retryable operation and returns statistics.

type Stats

type Stats struct {
	TotalAttempts int           // Total number of attempts made
	TotalDelay    time.Duration // Total time spent waiting between retries
	LastError     error         // The last error encountered
	Succeeded     bool          // Whether the operation ultimately succeeded
}

Stats holds statistics about retry operations.

type Strategy

type Strategy interface {
	// ShouldRetry determines if an operation should be retried based on the error and attempt number
	ShouldRetry(err error, attempt int) bool

	// NextDelay calculates the delay before the next retry attempt
	NextDelay(attempt int) time.Duration

	// MaxAttempts returns the maximum number of retry attempts allowed
	MaxAttempts() int
}

Strategy defines the interface for retry strategies.

func AggressiveStrategy

func AggressiveStrategy() Strategy

AggressiveStrategy returns a strategy with shorter delays for time-sensitive operations.

func ConservativeStrategy

func ConservativeStrategy() Strategy

ConservativeStrategy returns a strategy with longer delays for less time-sensitive operations.

func DefaultStrategy

func DefaultStrategy() Strategy

DefaultStrategy returns a default exponential backoff strategy suitable for most use cases.

func FileSystemStrategy

func FileSystemStrategy() Strategy

FileSystemStrategy returns a strategy optimized for file system operations.

func NetworkStrategy

func NetworkStrategy() Strategy

NetworkStrategy returns a strategy optimized for network operations.

type StrategyBuilder

type StrategyBuilder struct {
	// contains filtered or unexported fields
}

StrategyBuilder provides a fluent interface for building retry strategies.

func NewStrategyBuilder

func NewStrategyBuilder() *StrategyBuilder

NewStrategyBuilder creates a new strategy builder.

func (*StrategyBuilder) Build

func (b *StrategyBuilder) Build() Strategy

Build creates the configured retry strategy.

func (*StrategyBuilder) WithBackoffFactor

func (b *StrategyBuilder) WithBackoffFactor(factor float64) *StrategyBuilder

WithBackoffFactor sets the backoff factor for exponential backoff.

func (*StrategyBuilder) WithBackoffType

func (b *StrategyBuilder) WithBackoffType(backoffType BackoffType) *StrategyBuilder

WithBackoffType sets the backoff type.

func (*StrategyBuilder) WithBaseDelay

func (b *StrategyBuilder) WithBaseDelay(baseDelay time.Duration) *StrategyBuilder

WithBaseDelay sets the base delay.

func (*StrategyBuilder) WithCustomIntervals

func (b *StrategyBuilder) WithCustomIntervals(intervals []time.Duration) *StrategyBuilder

WithCustomIntervals sets custom intervals for custom backoff.

func (*StrategyBuilder) WithIncrement

func (b *StrategyBuilder) WithIncrement(increment time.Duration) *StrategyBuilder

WithIncrement sets the increment for linear backoff.

func (*StrategyBuilder) WithJitter

func (b *StrategyBuilder) WithJitter(enabled bool, factor float64) *StrategyBuilder

WithJitter enables jitter with the specified factor.

func (*StrategyBuilder) WithMaxDelay

func (b *StrategyBuilder) WithMaxDelay(maxDelay time.Duration) *StrategyBuilder

WithMaxDelay sets the maximum delay.

func (*StrategyBuilder) WithMaxRetries

func (b *StrategyBuilder) WithMaxRetries(maxRetries int) *StrategyBuilder

WithMaxRetries sets the maximum number of retries.

func (*StrategyBuilder) WithRetryChecker

func (b *StrategyBuilder) WithRetryChecker(checker func(error) bool) *StrategyBuilder

WithRetryChecker sets a custom retry checker function.

Jump to

Keyboard shortcuts

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