ops

package
v0.0.0-...-bd33f34 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package ops provides utilities to execute operations with retry, timeout, and rate limiting.

This package draws inspiration from the AWS and GCP SDKs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, op func(context.Context) error, opts ...Option) error

Execute executes operation op with the given options.

func ExecuteWithResult

func ExecuteWithResult[T any](ctx context.Context, op func(context.Context) (T, error), opts ...Option) (T, error)

ExecuteWithResult returns the result of calling op with the given options. It is a convenience wrapper around Execute for operations that return a value. In case of error, the zero value of T is returned.

Types

type BackoffPolicy

type BackoffPolicy struct {
	// Initial delay to be used for the first retry; defaults to 1 second.
	Initial time.Duration

	// Maximum value for the retry delay; defaults to 60 seconds.
	Maximum time.Duration

	// Factor by which the delay is multiplied after each retry. The value
	// must be greater or equal to 1. If not, it defaults to 2.
	Factor float64
	// contains filtered or unexported fields
}

BackoffPolicy implements an exponential backoff policy. The delay betwen retries is randomly computed between 0 and the "exponential delay" as recommended in Exponential Backoff And Jitter. The retry delay start from Initial and grows exponentially by Factor at every retry. The maximum retry delay is capped by Maximum.

There is no parameter to limit the number of retries. This is intended as such logic should be implemented upstream (e.g. in a Retrier).

Important: BackoffPolicies cannot conveniently be reset. This is intentional and client should rather create a new BackoffPolicy instead of resetting an existing one.

func (*BackoffPolicy) Delay

func (bp *BackoffPolicy) Delay() time.Duration

type Limiter

type Limiter interface {
	Wait(context.Context) error
}

Limiter is anything that can wait. It is typically used to implement client-side rate limiting. Implementation of this interface must be thread-safe.

type Option

type Option interface {
	Apply(*Options) error
}

Option is an option used by Execute to control the behavior of an operation. An Option essentially acts as a convenient way to configure Options.

func WithDisableRetry

func WithDisableRetry() Option

WithDisableRetry is a convenience option to disable retries.

func WithLimiter

func WithLimiter(l Limiter) Option

WithLimiter configures to use the given Limiter. If no limiter is provided, the operation is not rate limited.

func WithRetrier

func WithRetrier(provider func() Retrier) Option

WithRetrier configures to use the given Retrier provider. If no retrier is provided, the operation is not retried.

The provider function must be thread-safe.

func WithTimeout

func WithTimeout(t time.Duration) Option

WithTimeout sets the timeout duration for the operation. If the context already has a deadline, it is updated to the minimum of the context's deadline and the timeout.

The timeout covers the entire execution, including retries.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options controls the behavior of an operation.

type Retrier

type Retrier interface {
	// IsRetriable returns whether an error is retriable and how long the
	// caller should wait before retrying. Implementation should assume that
	// the given error is never nil.
	IsRetriable(err error) (time.Duration, bool)
}

Retrier defines a retry behavior.

func RetryIf

func RetryIf(bp BackoffPolicy, isRetriable func(error) bool) Retrier

RetryIf returns a Retrier that retries whenever predicate returns true.

Important: the retrier has its own copy of the backoff policy which cannot be trivially reset by design. Users who need to reset the backoff policy should rather create a new retrier.

Jump to

Keyboard shortcuts

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