retry

package
v1.13.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

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

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

func DefaultRetryIf(err error) bool

DefaultRetryIf is a retry condition that retries all errors except permanent ones

func Do

func Do(ctx context.Context, operation Operation, opts ...Option) error

Do executes the operation with retry logic

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

func IsPermanent(err error) bool

IsPermanent checks if an error is marked as permanent (non-retryable)

func IsRetryable

func IsRetryable(err error) bool

IsRetryable checks if an error implements RetryableError and is retryable

func MarkPermanent

func MarkPermanent(err error) error

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 applies decorrelated jitter (prevents synchronized retries)
	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 Operation

type Operation func() error

Operation is a function that can be retried

type OperationWithResult

type OperationWithResult[T any] func() (T, error)

OperationWithResult is a function that returns a result and can be retried

type Option

type Option func(*config)

Option configures retry behavior

func WithDelay

func WithDelay(d time.Duration) Option

WithDelay sets the delay between retries

func WithExponentialBackoff

func WithExponentialBackoff(initialDelay time.Duration, multiplier float64) Option

WithExponentialBackoff enables exponential backoff with the given multiplier

func WithJitter

func WithJitter(factor float64) Option

WithJitter adds randomness to retry delays (0.0 to 1.0)

func WithMaxDelay

func WithMaxDelay(d time.Duration) Option

WithMaxDelay sets the maximum delay between retries

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retry attempts

func WithOnRetry

func WithOnRetry(fn func(n int, err error)) Option

WithOnRetry sets a callback function called before each retry

func WithRetryIf

func WithRetryIf(fn func(error) bool) Option

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

func (Permanent) Error

func (e Permanent) Error() string

func (Permanent) Unwrap

func (e Permanent) Unwrap() error

type RetryableError

type RetryableError interface {
	error
	Retryable() bool
}

RetryableError interface for errors that can indicate if they're retryable

Jump to

Keyboard shortcuts

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