retry

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 7 Imported by: 15

Documentation

Overview

Package retry contains utilities for implementing retry logic.

Index

Constants

View Source
const MaxBackoffMax = time.Duration(maxInt64Convertible)

MaxBackoffMax is the maximum value that can be passed as max to Backoff.

Variables

This section is empty.

Functions

func Wait

func Wait(ctx context.Context, policy Policy, retry int) error

Wait queries the provided policy at the provided retry number and sleeps until the next try should be attempted. Wait returns an error if the policy prohibits further tries or if the context was canceled, or if its deadline would run out while waiting for the next try.

func WaitForFn added in v0.0.11

func WaitForFn(ctx context.Context, policy Policy, fn interface{}, params ...interface{}) (result []reflect.Value)

WaitForFn uses the above Wait function taking the same policy and retry number and generalizes it for a use of a function. Just like Wait it errors in the cases of extra tries, context cancel, or if its deadline runs out waiting for the next try

Types

type Policy

type Policy interface {
	// Retry tells whether the a new retry should be attempted,
	// and after how long.
	Retry(retry int) (bool, time.Duration)
}

A Policy is an interface that abstracts retry policies. Typically users will not call methods directly on a Policy but rather use the package function retry.Wait.

func Backoff

func Backoff(initial, max time.Duration, factor float64) Policy

Backoff returns a Policy that initially waits for the amount of time specified by parameter initial; on each try this value is multiplied by the provided factor, up to the max duration.

func BackoffWithTimeout added in v0.0.11

func BackoffWithTimeout(initial, max time.Duration, factor float64) Policy

BackoffWithTimeout returns a Policy that initially waits for the amount of time specified by parameter initial; on each try this value is multiplied by the provided factor, up to the max duration. After the max duration, the Policy will timeout and return an error.

func Jitter

func Jitter(policy Policy, frac float64) Policy

Jitter returns a policy that jitters 'frac' fraction of the wait times returned by the provided policy. For example, setting frac to 1.0 and 0.5 will implement "full jitter" and "equal jitter" approaches respectively. These approaches are describer here: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/

func MaxRetries added in v0.0.11

func MaxRetries(policy Policy, n int) Policy

MaxRetries returns a policy that enforces a maximum number of attempts. The provided policy is invoked when the current number of tries is within the permissible limit. If policy is nil, the returned policy will permit an immediate retry when the number of tries is within the allowable limits.

Jump to

Keyboard shortcuts

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