retry

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExponentialBackoffDefault = ExponentialBackoffWithJitter{
	Base:         100 * time.Millisecond,
	Max:          30 * time.Second,
	JitterFactor: 0.2,
}

ExponentialBackoffDefault is a sensible default exponential backoff

View Source
var FixedBackoffDefault = FixedBackoff(1 * time.Second)

FixedBackoffDefault is a sensible default fixed backoff

View Source
var LinearBackoffDefault = LinearBackoff{
	Base:      100 * time.Millisecond,
	Increment: 100 * time.Millisecond,
	Max:       30 * time.Second,
}

LinearBackoffDefault is a sensible default linear backoff

Functions

func Do

func Do(ctx context.Context, config *RetryConfig, op func() error) error

Do executes the operation with retry logic

func RetryOnError

func RetryOnError(err error) bool

RetryOnError creates a condition that retries on all errors

func RetryOnTemporary

func RetryOnTemporary(err error) bool

RetryOnTemporary creates a condition that retries on temporary errors

func RetryOnTimeout

func RetryOnTimeout(err error) bool

RetryOnTimeout creates a condition that retries only on timeout errors

Types

type BackoffStrategy

type BackoffStrategy interface {
	// NextDelay returns the delay for the next retry attempt
	// attempt starts at 1 for the first retry
	NextDelay(attempt int) time.Duration
}

BackoffStrategy defines the interface for backoff strategies

type ExponentialBackoff

type ExponentialBackoff struct {
	Base time.Duration
	Max  time.Duration
}

ExponentialBackoff increases delay exponentially with each attempt

func (ExponentialBackoff) NextDelay

func (e ExponentialBackoff) NextDelay(attempt int) time.Duration

NextDelay implements BackoffStrategy

type ExponentialBackoffWithJitter

type ExponentialBackoffWithJitter struct {
	Base         time.Duration
	Max          time.Duration
	JitterFactor float64 // 0-1, percentage of jitter
}

ExponentialBackoffWithJitter adds jitter to exponential backoff

func (ExponentialBackoffWithJitter) NextDelay

func (e ExponentialBackoffWithJitter) NextDelay(attempt int) time.Duration

NextDelay implements BackoffStrategy

type FibonacciBackoff

type FibonacciBackoff struct {
	Base time.Duration
	Max  time.Duration
}

FibonacciBackoff increases delay following the Fibonacci sequence

func (FibonacciBackoff) NextDelay

func (f FibonacciBackoff) NextDelay(attempt int) time.Duration

NextDelay implements BackoffStrategy

type FixedBackoff

type FixedBackoff time.Duration

FixedBackoff is a backoff strategy with fixed intervals

func (FixedBackoff) NextDelay

func (f FixedBackoff) NextDelay(attempt int) time.Duration

NextDelay implements BackoffStrategy

type LinearBackoff

type LinearBackoff struct {
	Base      time.Duration
	Increment time.Duration
	Max       time.Duration
}

LinearBackoff increases delay linearly with each attempt

func (LinearBackoff) NextDelay

func (l LinearBackoff) NextDelay(attempt int) time.Duration

NextDelay implements BackoffStrategy

type RetryCondition

type RetryCondition func(err error) bool

RetryCondition defines when to retry

type RetryConfig

type RetryConfig struct {
	MaxAttempts int
	Backoff     BackoffStrategy
	RetryOn     []RetryCondition
	Timeout     time.Duration
}

RetryConfig contains retry configuration

func DefaultRetryConfig

func DefaultRetryConfig() *RetryConfig

DefaultRetryConfig returns a default retry configuration

func RetryConfigForDatabase

func RetryConfigForDatabase() *RetryConfig

RetryConfigForDatabase creates a retry config optimized for database operations

func RetryConfigForIO

func RetryConfigForIO() *RetryConfig

RetryConfigForIO creates a retry config optimized for I/O operations

func RetryConfigForNetwork

func RetryConfigForNetwork() *RetryConfig

RetryConfigForNetwork creates a retry config optimized for network operations

type RetryResult

type RetryResult struct {
	Value     interface{}
	Error     error
	Attempts  int
	TotalTime time.Duration
	LastDelay time.Duration
	Success   bool
}

RetryResult contains the result of a retry operation

func DoWithResult

func DoWithResult(ctx context.Context, config *RetryConfig, op func() (interface{}, error)) *RetryResult

DoWithResult executes the operation with retry logic and returns a result

type Retryer

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

Retryer is a retry helper with built-in state

func NewRetryer

func NewRetryer(config *RetryConfig) *Retryer

NewRetryer creates a new Retryer with the given config

func (*Retryer) Attempt

func (r *Retryer) Attempt(ctx context.Context, op func() error) error

Attempt executes a single retry attempt

func (*Retryer) Attempts

func (r *Retryer) Attempts() int

Attempts returns the number of attempts made

func (*Retryer) LastDelay

func (r *Retryer) LastDelay() time.Duration

LastDelay returns the last backoff delay

func (*Retryer) Reset

func (r *Retryer) Reset()

Reset resets the retryer state

func (*Retryer) TotalTime

func (r *Retryer) TotalTime() time.Duration

TotalTime returns the total time spent retrying

func (*Retryer) Wait

func (r *Retryer) Wait(ctx context.Context) error

Wait waits for the backoff delay

Jump to

Keyboard shortcuts

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