retry

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package retry provides generic retry logic with exponential backoff for iter.Seq2-based operations such as LLM provider calls.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRetryable

func IsRetryable(err error) bool

IsRetryable reports whether err represents a transient failure that warrants a retry. The following are considered retryable:

  • HTTP 429 Too Many Requests (rate limit)
  • HTTP 5xx Server Errors (500–599)
  • Common network-level errors (connection refused/reset, timeout, EOF)

Context cancellation and deadline exceeded are explicitly non-retryable.

func Seq2

func Seq2[V any](
	ctx context.Context,
	cfg Config,
	fn func() iter.Seq2[V, error],
	isPartial func(V) bool,
) iter.Seq2[V, error]

Seq2 wraps fn with retry logic for iter.Seq2-based operations.

On each attempt fn is called to obtain a fresh iterator. Values are forwarded to the caller via yield. If the iterator produces an error and the error is retryable (see IsRetryable), Seq2 waits the appropriate backoff duration and calls fn again, up to cfg.MaxAttempts times total.

The isPartial predicate is called for every successfully yielded value. Once a partial/streaming value has been forwarded to the caller, retry is disabled for the remainder of that call to prevent delivering duplicate content.

If cfg.MaxAttempts ≤ 1, fn() is returned directly with no wrapping overhead.

Types

type Config

type Config struct {
	// MaxAttempts is the total number of attempts (including the first).
	// A value ≤ 1 disables retries; only one attempt is made.
	MaxAttempts int

	// InitialDelay is the wait time before the first retry.
	InitialDelay time.Duration

	// MaxDelay caps the exponential backoff delay.
	MaxDelay time.Duration

	// Multiplier is the factor by which the delay grows on each retry.
	Multiplier float64

	// Jitter adds ±25 % random noise to the delay to avoid thundering-herd
	// problems when many callers retry simultaneously.
	Jitter bool
}

Config holds configuration for automatic retry with exponential backoff.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a sensible default Config. It retries up to 3 times with exponential backoff starting at 1 s, capped at 60 s.

Jump to

Keyboard shortcuts

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