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 ¶
- func Retry(operation func() error, opts ...Option) error
- func RetryWithContext(ctx context.Context, operation func() error, opts ...Option) error
- func RetryWithData[T any](operation func() (T, error), opts ...Option) (T, error)
- func RetryWithDataAndContext[T any](ctx context.Context, operation func() (T, error), opts ...Option) (T, error)
- type Option
- func WithConstantPolicy(duration time.Duration) Option
- func WithExponentialPolicy() Option
- func WithInitialInterval(interval time.Duration) Option
- func WithMaxElapsedTime(elapsedTime time.Duration) Option
- func WithMaxInterval(interval time.Duration) Option
- func WithMaxRetries(maxRetries int64) Option
- func WithMultiplier(multiplier float32) Option
- func WithRandomizationFactor(factor float32) Option
- type PolicyType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Retry ¶
Retry executes operation repeatedly until it succeeds or the backoff stops. In v5, this uses the new generic Retry function.
func RetryWithContext ¶
RetryWithContext executes operation repeatedly with context until it succeeds or the backoff stops.
func RetryWithData ¶
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 ¶
WithConstantPolicy configures constant backoff policy with the specified duration.
func WithExponentialPolicy ¶
func WithExponentialPolicy() Option
WithExponentialPolicy configures exponential backoff policy.
func WithInitialInterval ¶
WithInitialInterval sets the initial interval for exponential backoff.
func WithMaxElapsedTime ¶
WithMaxElapsedTime sets the maximum elapsed time for exponential backoff.
func WithMaxInterval ¶
WithMaxInterval sets the maximum interval for exponential backoff.
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries.
func WithMultiplier ¶
WithMultiplier sets the multiplier for exponential backoff.
func WithRandomizationFactor ¶
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 )