Documentation
¶
Overview ¶
Package wait provides a generic context-aware polling loop with exponential backoff, full jitter, and an overall timeout. It is the engine behind `--wait` flags.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrTimeout = errors.New("timed out waiting")
ErrTimeout is returned when Options.Timeout elapses before fn reports done.
Functions ¶
func Poll ¶
func Poll(ctx context.Context, opts Options, fn func(ctx context.Context) (done bool, err error)) error
Poll calls fn immediately and then on a backoff schedule until fn reports done, fn returns an error, ctx is canceled, or the Timeout budget runs out (ErrTimeout). When less than one full delay remains in the budget the sleep is clamped so a final poll runs at the deadline.
Types ¶
type Options ¶
type Options struct {
Initial time.Duration // first backoff delay (default 2s)
Factor float64 // backoff multiplier (default 1.5)
Cap time.Duration // backoff ceiling (default 30s)
Timeout time.Duration // overall budget; 0 = poll forever
// Jitter maps the scheduled delay to the actual sleep. nil = full jitter:
// a uniform draw from (0, d].
Jitter func(d time.Duration) time.Duration
// Sleep blocks for d or until ctx is done (returning ctx.Err()).
// nil = timer-based sleep.
Sleep func(ctx context.Context, d time.Duration) error
// Now is the clock used for the Timeout budget. nil = time.Now.
Now func() time.Time
}
Options tunes a Poll loop. Zero values select the defaults; the function fields exist so tests can inject a deterministic jitter, sleeper, and clock.
Click to show internal directories.
Click to hide internal directories.