retry

package
v3.78.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	DefaultDelay    time.Duration = 100 * time.Millisecond // by default, delay by 100ms
	DefaultBackoff  float64       = 1.5                    // by default, backoff by 1.5x
	DefaultMaxDelay time.Duration = 5 * time.Second        // by default, no more than 5 seconds
)

Variables

This section is empty.

Functions

func Until

func Until(ctx context.Context, acceptor Acceptor) (bool, interface{}, error)

Until waits until the acceptor accepts the current condition, or the context expires, whichever comes first. A return boolean of true means the acceptor eventually accepted; a non-nil error means the acceptor returned an error. If an acceptor accepts a condition after the context has expired, we ignore the expiration and return the condition.

This uses Retryer with the default settings.

func UntilDeadline

func UntilDeadline(ctx context.Context, acceptor Acceptor, deadline time.Time) (bool, interface{}, error)

UntilDeadline creates a child context with the given deadline, and then invokes the above Until function.

func UntilTimeout

func UntilTimeout(ctx context.Context, acceptor Acceptor, timeout time.Duration) (bool, interface{}, error)

UntilTimeout creates a child context with the given timeout, and then invokes the above Until function.

Types

type Acceptance

type Acceptance func(try int, nextRetryTime time.Duration) (success bool, result interface{}, err error)

Acceptance is meant to accept a condition. It returns true when this condition has succeeded, and false otherwise (to which we respond by waiting and retrying after a certain period of time). If a non-nil error is returned, retrying halts. The interface{} data may be used to return final values to the caller.

Try specifies the attempt number, zero indicating that this is the first attempt with no retries.

type Acceptor

type Acceptor struct {
	Accept   Acceptance     // a function that determines when to proceed.
	Delay    *time.Duration // an optional delay duration.
	Backoff  *float64       // an optional backoff multiplier.
	MaxDelay *time.Duration // an optional maximum delay duration.
}

type Retryer added in v3.60.0

type Retryer struct {
	// Returns a channel that will send the time after the duration elapses.
	//
	// Defaults to time.After.
	After func(time.Duration) <-chan time.Time
}

Retryer provides the ability to run and retry a fallible operation with exponential backoff.

func (*Retryer) Until added in v3.60.0

func (r *Retryer) Until(ctx context.Context, acceptor Acceptor) (bool, interface{}, error)

Until runs the provided acceptor until one of the following conditions is met:

  • the operation succeeds: returns true and the result
  • the context expires: returns false and no result or errors
  • the operation returns an error: returns an error

Note that the number of attempts is not limited. The Acceptance function is responsible for determining when to stop retrying.

Jump to

Keyboard shortcuts

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