retry

package
v2.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package retry provides context based retry functionality for functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPanic is returned when a panic is rescued.
	ErrPanic = errors.New("panic")
	// ErrAbort is returned when retrying an operation will not result in a
	// different outcome.
	ErrAbort = errors.New("operation can not be completed")
)

Functions

func Do

func Do(ctx context.Context, fn func() error, opts ...Option) error

Do runs the function until it returns nil or the context is done or canceled.

func DoFor

func DoFor(d time.Duration, fn func() error, opts ...Option) error

DoFor retries the function until it returns nil or the given duration has passed.

func DoForWithContext

func DoForWithContext(d time.Duration, fn func(context.Context) error, opts ...Option) error

DoForWithContext retries the function and passes the context to it until it returns nil or the given duration has passed.

func DoWithContext

func DoWithContext(ctx context.Context, fn func(context.Context) error, opts ...Option) error

DoWithContext runs the function and passes the context to it until it returns nil or the context is done or canceled.

func Get

func Get[T any](ctx context.Context, fn func() (T, error), opts ...Option) (T, error)

Get is a generic alternative of Do that returns the result of a function that returns a value and an error.

func GetWithContext

func GetWithContext[T any](ctx context.Context, fn func(context.Context) (T, error), opts ...Option) (T, error)

GetWithContext is a generic alternative of DoWithContext that returns the result of a function that returns a value and an error.

Types

type Option

type Option func(*Options)

Option is a functional option function for Options.

func Backoff

func Backoff(f float64) Option

Backoff is a functional option that sets the backoff factor. On each attempt the delay is multiplied by this factor. The default is 1.0.

func Delay

func Delay(d time.Duration) Option

Delay is a functional option that sets the delay between retries. The default is 2 seconds.

func If

func If(f func(error) bool) Option

If is a functional option that sets the function to determine if an error should continue the retry. If the function returns true, the retry will continue.

func MaxRetries

func MaxRetries(n int) Option

MaxRetries is a functional option that sets the maximum number of retries. The default is to retry indefinitely or until the context is done or canceled.

func RescuePanic

func RescuePanic() Option

RescuePanic is a functional option that controls if panics should be rescued.

type Options

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

Options for retry.

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions returns a new Options with the given options applied.

Jump to

Keyboard shortcuts

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