retry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package retry provides a functional mechanism to retry operations with configurable backoff policies.

It supports constant and exponential backoff strategies, context cancellation, and extensive configuration options such as max retries, max elapsed time, and randomization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(operation func() error, opts ...Option) error

Retry executes operation repeatedly until it succeeds or the backoff stops. In v5, this uses the new generic Retry function.

func RetryWithContext

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

RetryWithContext executes operation repeatedly with context until it succeeds or the backoff stops.

func RetryWithData

func RetryWithData[T any](operation func() (T, error), opts ...Option) (T, error)

RetryWithData executes operation repeatedly until it succeeds or the backoff stops. Returns the result of the operation and any error.

func RetryWithDataAndContext

func RetryWithDataAndContext[T any](ctx context.Context, operation func() (T, error), opts ...Option) (T, error)

RetryWithDataAndContext executes operation repeatedly with context until it succeeds or the backoff stops. Returns the result of the operation and any error.

Types

type Option

type Option func(*config)

Option represents a functional option for configuring retry behavior.

func WithConstantPolicy

func WithConstantPolicy(duration time.Duration) Option

WithConstantPolicy configures constant backoff policy with the specified duration.

func WithExponentialPolicy

func WithExponentialPolicy() Option

WithExponentialPolicy configures exponential backoff policy.

func WithInitialInterval

func WithInitialInterval(interval time.Duration) Option

WithInitialInterval sets the initial interval for exponential backoff.

func WithMaxElapsedTime

func WithMaxElapsedTime(elapsedTime time.Duration) Option

WithMaxElapsedTime sets the maximum elapsed time for exponential backoff.

func WithMaxInterval

func WithMaxInterval(interval time.Duration) Option

WithMaxInterval sets the maximum interval for exponential backoff.

func WithMaxRetries

func WithMaxRetries(maxRetries int64) Option

WithMaxRetries sets the maximum number of retries.

func WithMultiplier

func WithMultiplier(multiplier float32) Option

WithMultiplier sets the multiplier for exponential backoff.

func WithRandomizationFactor

func WithRandomizationFactor(factor float32) Option

WithRandomizationFactor sets the randomization factor for exponential backoff.

type PolicyType

type PolicyType int

PolicyType denotes if the back off delay should be constant or exponential.

const (
	// PolicyConstant is a backoff policy that always returns the same backoff delay.
	PolicyConstant PolicyType = iota
	// PolicyExponential is a backoff implementation that increases the backoff period
	// for each retry attempt using a randomization function that grows exponentially.
	PolicyExponential
)

Jump to

Keyboard shortcuts

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