retry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package retry provides retry logic with exponential backoff for transient failures.

This package wraps github.com/cenkalti/backoff/v5 and integrates it with the CQI error package to provide intelligent retry policies based on error types. It provides exponential backoff with jitter to prevent thundering herd problems when multiple clients retry simultaneously.

Example usage:

cfg := retry.Config{
	MaxAttempts:    5,
	InitialDelay:   100 * time.Millisecond,
	MaxDelay:       5 * time.Second,
	Multiplier:     2.0,
	Policy:         retry.PolicyTemporary,
}

err := retry.Do(ctx, cfg, func() error {
	return someOperation()
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, cfg Config, fn func() error) error

Do executes the provided function with retry logic based on the configuration. It respects context cancellation and applies exponential backoff between retries.

The function will retry on errors according to the configured policy:

  • PolicyTemporary: Only retry errors.Temporary errors
  • PolicyAll: Retry all errors
  • PolicyNone: Never retry (execute once)
  • Custom policy functions can be provided via Config.PolicyFunc

Returns the error from the last attempt if all retries are exhausted.

func DoWithData

func DoWithData[T any](ctx context.Context, cfg Config, fn func() (T, error)) (T, error)

DoWithData executes the provided function with retry logic and returns a value. It works the same as Do but supports functions that return both a value and an error.

Example:

data, err := retry.DoWithData(ctx, cfg, func() (string, error) {
	return fetchData()
})

Types

type Config

type Config struct {
	// MaxAttempts is the maximum number of attempts (initial attempt + retries).
	// 0 means unlimited attempts. Default is 10.
	MaxAttempts uint

	// InitialDelay is the initial backoff delay. Default is 100ms.
	InitialDelay time.Duration

	// MaxDelay is the maximum backoff delay. Default is 5 seconds.
	MaxDelay time.Duration

	// Multiplier is the backoff multiplier. Default is 2.0.
	Multiplier float64

	// Jitter is the randomization factor (0.0 to 1.0). Default is 0.25 (±25%).
	Jitter float64

	// MaxElapsedTime is the maximum total time for all retry attempts.
	// 0 means no time limit. Default is 0.
	MaxElapsedTime time.Duration

	// Policy determines which errors should be retried.
	// Default is PolicyTemporary.
	Policy Policy

	// PolicyFunc is a custom policy function. If set, it takes precedence over Policy.
	PolicyFunc PolicyFunc
}

Config holds the retry configuration.

type Policy

type Policy int

Policy defines when a function should be retried.

const (
	// PolicyTemporary retries only errors.Temporary errors.
	PolicyTemporary Policy = iota
	// PolicyAll retries all errors.
	PolicyAll
	// PolicyNone never retries (executes once).
	PolicyNone
)

type PolicyFunc

type PolicyFunc func(error) bool

PolicyFunc is a custom function that determines if an error should be retried.

Jump to

Keyboard shortcuts

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