retry

package
v0.0.0-...-f05e396 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package retry provides exponential backoff with jitter for retrying operations that may fail transiently.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateDelay

func CalculateDelay(attempt int, cfg RetryConfig) time.Duration

CalculateDelay computes the backoff delay for the given attempt number. The delay is min(baseDelay * 2^attempt, maxDelay), with optional equal jitter that guarantees at least 50% of the calculated delay.

func Do

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

Do executes fn with retry logic according to cfg. It returns nil on success, or the last error if all retries are exhausted. It respects context cancellation between retries.

func IsRetryable

func IsRetryable(statusCode int) bool

IsRetryable returns true if the given HTTP status code indicates a retryable error: 408, 429, 500, 502, 503, 504.

Types

type RetryConfig

type RetryConfig struct {
	MaxRetries int
	BaseDelay  time.Duration
	MaxDelay   time.Duration
	Jitter     bool
}

RetryConfig holds configuration for retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns a RetryConfig with sensible defaults: 3 retries, 1s base delay, 30s max delay, jitter enabled.

type RetryableError

type RetryableError struct {
	Err        error
	StatusCode int
	RetryAfter time.Duration
}

RetryableError represents an error that may be retried, optionally including an HTTP status code and a Retry-After duration.

func (*RetryableError) Error

func (e *RetryableError) Error() string

func (*RetryableError) Unwrap

func (e *RetryableError) Unwrap() error

Jump to

Keyboard shortcuts

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