retry

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package retry provides exponential backoff with jitter for paper-board service gRPC call sites. It is the inner tier of the two-layer retry strategy (D9.4): sdk/retry handles transient blips at the call site; the outbox publish retry (sdk/outbox) is the outer tier.

Usage

ctx = retry.WithTarget(ctx, "vaults.GetCredential")
err := retry.Do(ctx, retry.DefaultPolicy, func(ctx context.Context) error {
    resp, err = vaultsClient.GetCredential(ctx, req)
    return err
})

Retry decision

Per attempt:

  • IsRetryable(err) → schedule retry (subject to MaxAttempts)
  • IsPermanent(err) → return immediately, no retry
  • ClassificationUnknown → treated as Permanent (fail-loud, no silent retries)

Jitter

Mandatory ±20% jitter (D9.4) prevents thundering herd when many services retry against the same downstream.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPolicy = Policy{
	MaxAttempts: 3,
	BaseDelay:   200 * time.Millisecond,
	MaxDelay:    2 * time.Second,
	Factor:      2.0,
	Jitter:      0.2,
}

DefaultPolicy is the D9.4 inner-tier configuration.

Functions

func Do

func Do(ctx context.Context, p Policy, fn func(context.Context) error) error

Do runs fn under the policy and returns the last error if all attempts fail. Cancellation via ctx.Done() aborts the loop and returns ctx.Err().

func WithTarget

func WithTarget(ctx context.Context, target string) context.Context

WithTarget attaches a target label (e.g. "vaults.GetCredential") to ctx so Do can include it in retry log lines.

Types

type Policy

type Policy struct {
	// MaxAttempts is the total number of attempts (first + retries).
	// Default: 3 (D9.4 inner tier).
	MaxAttempts int
	// BaseDelay is the delay before the second attempt.
	// Default: 200ms.
	BaseDelay time.Duration
	// MaxDelay caps the computed delay.
	// Default: 2s.
	MaxDelay time.Duration
	// Factor is the exponential growth multiplier.
	// Default: 2.0.
	Factor float64
	// Jitter is the ±fraction applied to each delay.
	// Default: 0.2 (±20%, mandatory per D9.4).
	Jitter float64
	// Logger emits one INFO line per attempt with retry context.
	// Uses slog.Default() if nil.
	Logger *slog.Logger
}

Policy controls retry behaviour.

Jump to

Keyboard shortcuts

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