retry

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package retry provides shared retry, backoff, and rate-limiting logic.

Index

Constants

View Source
const (
	// DefaultMaxRetries is the default number of retry attempts.
	DefaultMaxRetries = 3

	// DefaultBaseDelay is the default initial backoff delay.
	DefaultBaseDelay = time.Second

	// DefaultMaxDelay is the default maximum backoff delay.
	DefaultMaxDelay = 30 * time.Second
)

Variables

This section is empty.

Functions

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true for common transient network errors. Protocol-specific checks (SMTP 4xx, IMAP auth) should be combined with this in a wrapper that also checks protocol-specific conditions.

func WaitForToken

func WaitForToken(ctx context.Context, lim *Limiter) error

WaitForToken blocks until a rate limit token is available or ctx is canceled.

func WithRetry

func WithRetry(
	ctx context.Context,
	cfg Config,
	lim *Limiter,
	isRetryable IsRetryableFunc,
	op func() error,
) error

WithRetry executes op with rate limiting, exponential backoff, and jitter. The isRetryable function determines whether a failed attempt should be retried. If lim is non-nil, a rate limit token is acquired before each attempt.

Types

type Config

type Config struct {
	MaxRetries int
	BaseDelay  time.Duration
	MaxDelay   time.Duration
	RateLimit  int           // tokens per window
	RateWindow time.Duration // window duration
}

Config controls retry behavior.

type IsRetryableFunc

type IsRetryableFunc func(error) bool

IsRetryableFunc classifies whether an error is transient.

type Limiter

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

Limiter implements a token-bucket rate limiter with a sliding window.

func NewLimiter

func NewLimiter(limit int, window time.Duration) *Limiter

NewLimiter creates a rate limiter with the given token limit and window duration.

func (*Limiter) Info

func (l *Limiter) Info() (int, int, time.Time)

Info returns current rate limit state: remaining tokens, total limit, and next reset time.

type PermanentError

type PermanentError struct {
	Err error
}

PermanentError wraps an error to signal that it should not be retried, regardless of what the IsRetryableFunc says. Use this for errors like failed reconnections that should immediately abort the retry loop.

func (*PermanentError) Error

func (e *PermanentError) Error() string

func (*PermanentError) Unwrap

func (e *PermanentError) Unwrap() error

Jump to

Keyboard shortcuts

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