retry

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package retry calls an operation again when it fails on a connection-level error, waiting longer each time, until it succeeds, fails some other way, runs out of budget, or its context ends. kcp uses it on the calls a connection storm breaks: a fence drains a gateway's connections, thousands of clients reconnect at once, and for a while the clusters reset connections, kcp's among them.

Index

Constants

View Source
const (
	FirstWait = 500 * time.Millisecond
	MaxWait   = 10 * time.Second
	Budget    = 2 * time.Minute
)

The schedule every Do uses: wait FirstWait after the first failure and double the wait after each later one, up to MaxWait. Do gives up rather than start a wait that would end more than Budget after the first failure; the attempts' own time counts against the budget. There is no jitter: one kcp process makes these calls.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, operation string, fn func(ctx context.Context) error, opts ...Option) error

Do calls fn until it returns nil or an error the classifier (Retryable by default) rejects, the budget runs out, or ctx ends. A rejected error is returned unchanged, even once ctx has ended: it is the answer, and no retry was cut short. When ctx ends during an attempt that failed with an error the classifier accepts, or during the wait after one, Do returns ctx's error joined with fn's last error. Running out of budget returns a *GaveUpError wrapping the last error.

Each retry adds a retry.attempt event to the span current in ctx, waits inside a wait.retry_backoff span, and logs a warning. A call that needed retries sets kcp.retry.attempts on that span however it ends, including when ctx ends during a retry, and kcp.retry.gave_up when it ran out of budget. A call that succeeds first time records nothing. Under WithoutRetries Do calls fn once and returns its error unchanged.

func Retryable

func Retryable(err error) bool

Retryable reports whether err is a connection-level failure worth trying again: a reset, dropped, refused or timed-out connection, or an HTTP 429, 502, 503 or 504. A Kafka error code, any other HTTP status, a failed login, a TLS error, a closed client, a cancellation and a *GaveUpError are not. sarama's ErrOutOfBrokers is judged by the errors it wraps, so a failed login stays a failure. Do checks its own context before retrying an error this accepts, so a caller's deadline is never retried even though a deadline is a timeout.

func SetScheduleForTest

func SetScheduleForTest(s Schedule) (restore func())

SetScheduleForTest replaces the schedule every Do uses and returns the function that restores it. Only tests call it, through retrytest.Fast.

func WithoutRetries

func WithoutRetries(ctx context.Context) context.Context

WithoutRetries returns a context under which Do calls fn once and returns its error unchanged, recording nothing: for callers that show their own errors and must not be held up or have warnings written over their output, such as a full-screen view.

Types

type GaveUpError

type GaveUpError struct {
	Operation string
	Attempts  int
	Elapsed   time.Duration
	Err       error
}

GaveUpError is Do's error once the budget runs out. Retryable never retries it, so retries nested inside each other stop together.

func (*GaveUpError) Error

func (e *GaveUpError) Error() string

func (*GaveUpError) Unwrap

func (e *GaveUpError) Unwrap() error

type Option

type Option func(*options)

Option configures one Do call.

func WithAttributes

func WithAttributes(attrs ...attribute.KeyValue) Option

WithAttributes adds attrs to every retry.attempt event. They must not name topics, groups or pods.

func WithBeforeRetry

func WithBeforeRetry(f func()) Option

WithBeforeRetry runs f before each retry, after the wait. Kafka callers close the broker connection the failed attempt used, so the retry opens a fresh one.

func WithRetryable

func WithRetryable(f func(error) bool) Option

WithRetryable replaces Retryable as the test of which errors are retried.

type Schedule

type Schedule struct {
	FirstWait time.Duration
	MaxWait   time.Duration
	Budget    time.Duration
}

Schedule is a backoff schedule. Do uses FirstWait, MaxWait and Budget unless a test replaces it.

Directories

Path Synopsis
Package retrytest shortens the retry schedule for one test, so a test that drives a retry does not wait the production backoff.
Package retrytest shortens the retry schedule for one test, so a test that drives a retry does not wait the production backoff.

Jump to

Keyboard shortcuts

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