retry

package
v4.12.4 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Cancelled         = cancelReason("context cancelled")
	Stopped           = cancelReason("retry stopped")
	AttemptsExhausted = cancelReason("attempts exhausted")
)

Variables

This section is empty.

Functions

func Stop

func Stop(err error) error

Stop forces the retry to cancel with the provided error and retry.Err.Reason == retry.Stopped

func Until

func Until(ctx context.Context, backOff BackOff, f Func) error

Until will retry the provided `retry.Func` until it returns nil or the context is cancelled. Optionally users may use `retry.Stop()` to force the retry to terminate with an error. Returns a `retry.Err` with the included Reason and Attempts

Types

type Async

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

func NewRetryAsync

func NewRetryAsync() *Async

Given a function that takes a context, run the provided function; if it fails, retry the function asynchronously and return Async{}. Subsequent calls to with the same 'key' will return Async{} if the function is still retrying, this continues until the retry period has exhausted or the context expires or is cancelled. Then the final error returned by f() is returned to the caller on the final call with the same 'key'

The code assumes the caller will continue to call `Async()` until either the retries have exhausted or an Async{Retrying: false} is returned.

func (*Async) Async

func (s *Async) Async(key interface{}, ctx context.Context, bo BackOff,
	f func(context.Context, int) error) *AsyncItem

func (*Async) Errs

func (s *Async) Errs() map[interface{}]AsyncItem

Return errors from failed asyncs and clean up the internal async map

func (*Async) Len

func (s *Async) Len() int

Return the number of active async retries

func (*Async) Stop

func (s *Async) Stop()

Stop forces stop of all running async retries

func (*Async) Wait

func (s *Async) Wait()

Wait waits for all running async retries to complete

type AsyncItem

type AsyncItem struct {
	Retrying bool
	Attempts int
	Err      error
}

func (*AsyncItem) Error

func (s *AsyncItem) Error() string

type AttemptsBackOff

type AttemptsBackOff struct {
	Interval time.Duration
	Attempts int64
	// contains filtered or unexported fields
}

AttemptsBackOff retry for `attempts` number of retries sleeping for `interval` between each retry

func Attempts

func Attempts(a int, t time.Duration) *AttemptsBackOff

func (*AttemptsBackOff) CalcDuration added in v4.9.0

func (b *AttemptsBackOff) CalcDuration(retries int64) time.Duration

func (*AttemptsBackOff) New

func (b *AttemptsBackOff) New() BackOff

func (*AttemptsBackOff) Next

func (b *AttemptsBackOff) Next() (time.Duration, bool)

func (*AttemptsBackOff) NextIteration added in v4.9.0

func (b *AttemptsBackOff) NextIteration() time.Duration

func (*AttemptsBackOff) NumRetries

func (b *AttemptsBackOff) NumRetries() int

func (*AttemptsBackOff) Reset

func (b *AttemptsBackOff) Reset()

type BackOff

type BackOff interface {
	New() BackOff
	Next() (time.Duration, bool)
	NumRetries() int
	NextIteration() time.Duration
	CalcDuration(int64) time.Duration
	Reset()
}

type ConstBackOff

type ConstBackOff struct {
	Interval time.Duration
	// contains filtered or unexported fields
}

ConstBackOff indefinitely retry, returns `interval` for each call to Next()

func Interval

func Interval(t time.Duration) *ConstBackOff

func (*ConstBackOff) CalcDuration added in v4.9.0

func (b *ConstBackOff) CalcDuration(retries int64) time.Duration

func (*ConstBackOff) New

func (b *ConstBackOff) New() BackOff

func (*ConstBackOff) Next

func (b *ConstBackOff) Next() (time.Duration, bool)

func (*ConstBackOff) NextIteration added in v4.9.0

func (b *ConstBackOff) NextIteration() time.Duration

func (*ConstBackOff) NumRetries

func (b *ConstBackOff) NumRetries() int

func (*ConstBackOff) Reset

func (b *ConstBackOff) Reset()

type Err

type Err struct {
	Err      error
	Reason   cancelReason
	Attempts int
}

func (*Err) Cause

func (e *Err) Cause() error

func (*Err) Error

func (e *Err) Error() string

func (*Err) Is

func (e *Err) Is(target error) bool

type ExponentialBackOff

type ExponentialBackOff struct {
	Min, Max time.Duration
	Factor   float64
	Attempts int64
	// contains filtered or unexported fields
}

func (*ExponentialBackOff) CalcDuration added in v4.9.0

func (b *ExponentialBackOff) CalcDuration(retries int64) time.Duration

CalcDuration returns the next duration to sleep given the number of retries.

This function is useful when keeping track of the attempts and attempt resets is external to our code.

func (*ExponentialBackOff) New

func (b *ExponentialBackOff) New() BackOff

New returns a copy of the current backoff

func (*ExponentialBackOff) Next

func (b *ExponentialBackOff) Next() (time.Duration, bool)

func (*ExponentialBackOff) NextIteration added in v4.9.0

func (b *ExponentialBackOff) NextIteration() time.Duration

NextIteration returns the next backoff duration. Is identical to Next() but never indicates if we have reached our max attempts

func (*ExponentialBackOff) NumRetries

func (b *ExponentialBackOff) NumRetries() int

func (*ExponentialBackOff) Reset

func (b *ExponentialBackOff) Reset()

type Func

type Func func(context.Context, int) error

Jump to

Keyboard shortcuts

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