retry

package
v0.0.0-...-507ffb6 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package retry implements retry logic helpers to execute arbitrary functions with defined policy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, policy Policy, f func() error) error

Run executes and retries the function on failure based on policy defined and returns nil-error on success.

func RunWithResponse

func RunWithResponse[T any](ctx context.Context, policy Policy, f func() (T, error)) (T, error)

RunWithResponse executes and retries the function on failure based on policy defined and returns response on success.

Types

type IsRetriable

type IsRetriable func(error) bool

IsRetriable is method signature for implementing to override default logic of retrying each error.

type Policy

type Policy struct {
	// MaxAttempts represents the maximum number of retry attempts. If set to 0
	// then retry will be infinite.
	MaxAttempts int
	// BackoffFactor is the multiplier by which retry interval (Jitter) increases
	// after each retry. For constant backoff set Backoff factor to 1.
	BackoffFactor float64
	// Jitter is the interval before the first retry.
	Jitter time.Duration
	// ShouldRetry is optional and the way to override default retry logic of
	// retry every error. If ShouldRetry is not provided/implemented every error
	// will be retried until all attempts are exhausted.
	ShouldRetry IsRetriable
	// MaximumBackoff is the maximum backoff time between retries. Backoff will be
	// capped to this value. If not set, then backoff will keep increasing based
	// on BackoffFactor and Jitter.
	MaximumBackoff time.Duration
}

Policy represents the struct to configure the retry behavior.

Jump to

Keyboard shortcuts

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