asyncutils

package
v0.16.4 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PollWithBackoff added in v0.16.4

func PollWithBackoff[T any](ctx context.Context, cfg BackoffConfig, fn PollFunc[T]) (T, error)

PollWithBackoff calls fn immediately, then retries with exponential backoff (per cfg) until fn reports done, fn returns an error, ctx is cancelled, or a configured bound (MaxAttempts / MaxElapsed) is reached.

On ctx cancellation while waiting between attempts, PollWithBackoff returns the last result along with ctx.Err(). When a bound is reached without fn ever reporting done, it returns the last result with a nil error — callers that need retries-exhausted to be an error should check the result themselves (e.g. a not-found sentinel), mirroring how the original per-call-site loops behaved.

func WaitForStateTransition

func WaitForStateTransition(ctx context.Context, resourceType, resourceID string, stateChecker StateChecker, opts ...Option) error

WaitForStateTransition waits for a resource to reach the desired state by polling its current state. The default poll interval is two seconds; pass WithPollInterval to customize it.

Types

type BackoffConfig added in v0.16.4

type BackoffConfig struct {
	// Initial is the delay before the second attempt. The first attempt
	// always runs immediately.
	Initial time.Duration
	// Max caps the backoff delay after it doubles on each retry. Leave zero
	// to keep the delay flat at Initial (no exponential growth).
	Max time.Duration
	// MaxAttempts bounds the number of attempts. Zero means unbounded (rely
	// on MaxElapsed and/or ctx cancellation to stop retrying).
	MaxAttempts int
	// MaxElapsed bounds the total wall-clock time spent retrying, measured
	// from the first attempt. Zero means unbounded (rely on MaxAttempts
	// and/or ctx cancellation to stop retrying).
	MaxElapsed time.Duration
}

BackoffConfig bounds the retry policy used by PollWithBackoff. It generalizes the wall-clock-bound, attempt-count-bound, and flat-delay policies that call sites previously hand-rolled independently.

type Option added in v0.16.1

type Option func(*waitConfig)

Option customizes WaitForStateTransition behavior.

func WithPollInterval added in v0.16.1

func WithPollInterval(d time.Duration) Option

WithPollInterval overrides the default polling interval. Useful for long-running resources (e.g. connector sync jobs) where a slower cadence is more appropriate than the default snappy 2-second tick.

type PollFunc added in v0.16.4

type PollFunc[T any] func(ctx context.Context, attempt int) (result T, done bool, err error)

PollFunc performs one poll attempt, 1-indexed by attempt. It returns the current result, whether polling should stop (done), and any error. A non-nil error stops polling immediately, same as done=true.

type StateChecker

type StateChecker func(ctx context.Context) (isDesiredState bool, err error)

StateChecker is a function that checks if a resource is in the desired state. It should return true if the resource is in the desired state, false otherwise, and any error that occurred during the check.

Jump to

Keyboard shortcuts

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