retry

package
Version: v0.0.0-...-e90f7c6 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultAttempts      = uint(10)
	DefaultDelay         = 100 * time.Millisecond
	DefaultMaxJitter     = 100 * time.Millisecond
	DefaultOnRetry       = func(n uint, err error) {}
	DefaultRetryIf       = IsRecoverable
	DefaultDelayType     = CombineDelay(BackOffDelay, RandomDelay)
	DefaultLastErrorOnly = false
	DefaultContext       = context.Background()
)
View Source
var ConflictError = errors.New("Conflict")
View Source
var DefaultBackoff = wait.Backoff{
	Steps:    4,
	Duration: 10 * time.Millisecond,
	Factor:   5.0,
	Jitter:   0.1,
}

DefaultBackoff is the recommended backoff for a conflict where a client may be attempting to make an unrelated modification to a resource under active management by one or more controllers.

View Source
var DefaultRetry = wait.Backoff{
	Steps:    5,
	Duration: 10 * time.Millisecond,
	Factor:   1.0,
	Jitter:   0.1,
}

DefaultRetry is the recommended retry for a conflict where multiple clients are making changes to the same resource.

Functions

func BackOffDelay

func BackOffDelay(n uint, _ error, config *Config) time.Duration

BackOffDelay is a DelayType which increases delay between consecutive retries

func Do

func Do(retryableFunc RetryableFunc, opts ...Option) error

func FixedDelay

func FixedDelay(_ uint, _ error, config *Config) time.Duration

FixedDelay is a DelayType which keeps delay the same through all iterations

func IsRecoverable

func IsRecoverable(err error) bool

IsRecoverable checks if error is an instance of `unrecoverableError`

func OnError

func OnError(backoff wait.Backoff, retriable func(error) bool, fn func() error) error

OnError allows the caller to retry fn in case the error returned by fn is retriable according to the provided function. backoff defines the maximum retries and the wait interval between two retries.

func RandomDelay

func RandomDelay(_ uint, _ error, config *Config) time.Duration

RandomDelay is a DelayType which picks a random delay up to config.maxJitter

func RetryOnConflict

func RetryOnConflict(backoff wait.Backoff, fn func() error) error

RetryOnConflict is used to make an update to a resource when you have to worry about conflicts caused by other code making unrelated updates to the resource at the same time. fn should fetch the resource to be modified, make appropriate changes to it, try to update it, and return (unmodified) the error from the update function. On a successful update, RetryOnConflict will return nil. If the update function returns a "Conflict" error, RetryOnConflict will wait some amount of time as described by backoff, and then try again. On a non-"Conflict" error, or if it retries too many times and gives up, RetryOnConflict will return an error to the caller.

err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
    // Fetch the resource here; you need to refetch it on every try, since
    // if you got a conflict on the last update attempt then you need to get
    // the current version before making your own changes.
    pod, err := c.Pods("mynamespace").Get(name, metav1.GetOptions{})
    if err ! nil {
        return err
    }

    // Make whatever updates to the resource are needed
    pod.Status.Phase = v1.PodFailed

    // Try to update
    _, err = c.Pods("mynamespace").UpdateStatus(pod)
    // You have to return err itself here (not wrapped inside another error)
    // so that RetryOnConflict can identify it correctly.
    return err
})
if err != nil {
    // May be conflict if max retries were hit, or may be something unrelated
    // like permissions or a network error
    return err
}
...

TODO: Make Backoff an interface?

func Unrecoverable

func Unrecoverable(err error) error

Unrecoverable wraps an error in `unrecoverableError` struct

Types

type Config

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

type DelayTypeFunc

type DelayTypeFunc func(n uint, err error, config *Config) time.Duration

DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts.

func CombineDelay

func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc

CombineDelay is a DelayType the combines all of the specified delays into a new DelayTypeFunc

type Error

type Error []error

Error type represents list of errors in retry

func (Error) Error

func (e Error) Error() string

Error method return string representation of Error It is an implementation of error interface

func (Error) WrappedErrors

func (e Error) WrappedErrors() []error

WrappedErrors returns the list of errors that this Error is wrapping. It is an implementation of the `errwrap.Wrapper` interface in package [errwrap](https://github.com/hashicorp/errwrap) so that `retry.Error` can be used with that library.

type OnRetryFunc

type OnRetryFunc func(n uint, err error)

Function signature of OnRetry function n = count of attempts

type Option

type Option func(*Config)

Option represents an option for retry.

func Attempts

func Attempts(attempts uint) Option

Attempts set count of retry default is 10

func Context

func Context(ctx context.Context) Option

Context allow to set context of retry default are Background context

example of immediately cancellation (maybe it isn't the best example, but it describes behavior enough; I hope)

ctx, cancel := context.WithCancel(context.Background())
cancel()

retry.Do(
	func() error {
		...
	},
	retry.Context(ctx),
)

func Delay

func Delay(delay time.Duration) Option

Delay set delay between retry default is 100ms

func DelayType

func DelayType(delayType DelayTypeFunc) Option

DelayType set type of the delay between retries default is BackOff

func LastErrorOnly

func LastErrorOnly(lastErrorOnly bool) Option

return the direct last error that came from the retried function default is false (return wrapped errors with everything)

func MaxDelay

func MaxDelay(maxDelay time.Duration) Option

MaxDelay set maximum delay between retry does not apply by default

func MaxJitter

func MaxJitter(maxJitter time.Duration) Option

MaxJitter sets the maximum random Jitter between retries for RandomDelay

func OnRetry

func OnRetry(onRetry OnRetryFunc) Option

OnRetry function callback are called each retry

log each retry example:

retry.Do(
	func() error {
		return errors.New("some error")
	},
	retry.OnRetry(func(n uint, err error) {
		log.Printf("#%d: %s\n", n, err)
	}),
)

func RetryIf

func RetryIf(retryIf RetryIfFunc) Option

RetryIf controls whether a retry should be attempted after an error (assuming there are any retry attempts remaining)

skip retry if special error example:

retry.Do(
	func() error {
		return errors.New("special error")
	},
	retry.RetryIf(func(err error) bool {
		if err.Error() == "special error" {
			return false
		}
		return true
	})
)

By default RetryIf stops execution if the error is wrapped using `retry.Unrecoverable`, so above example may also be shortened to:

retry.Do(
	func() error {
		return retry.Unrecoverable(errors.New("special error"))
	}
)

type RetryIfFunc

type RetryIfFunc func(error) bool

Function signature of retry if function

type RetryableFunc

type RetryableFunc func() error

Function signature of retryable function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL