cockroach: Index | Files

package retry

import ""


Package Files


func ForDuration Uses

func ForDuration(duration time.Duration, fn func() error) error

ForDuration will retry the given function until it either returns without error, or the given duration has elapsed. The function is invoked immediately at first and then successively with an exponential backoff starting at 1ns and ending at the specified duration.

This function is DEPRECATED! Please use one of the other functions in this package that takes context cancellation into account.

TODO(benesch): remove this function and port its callers to a context- sensitive API.

func WithMaxAttempts Uses

func WithMaxAttempts(ctx context.Context, opts Options, n int, fn func() error) error

WithMaxAttempts is a helper that runs fn N times and collects the last err. The function will terminate early if the provided context is canceled, but it guarantees that fn will run at least once.

type Options Uses

type Options struct {
    InitialBackoff      time.Duration   // Default retry backoff interval
    MaxBackoff          time.Duration   // Maximum retry backoff interval
    Multiplier          float64         // Default backoff constant
    MaxRetries          int             // Maximum number of attempts (0 for infinite)
    RandomizationFactor float64         // Randomize the backoff interval by constant
    Closer              <-chan struct{} // Optionally end retry loop channel close

Options provides reusable configuration of Retry objects.

type Retry Uses

type Retry struct {
    // contains filtered or unexported fields

Retry implements the public methods necessary to control an exponential- backoff retry loop.

func Start Uses

func Start(opts Options) Retry

Start returns a new Retry initialized to some default values. The Retry can then be used in an exponential-backoff retry loop.

func StartWithCtx Uses

func StartWithCtx(ctx context.Context, opts Options) Retry

StartWithCtx returns a new Retry initialized to some default values. The Retry can then be used in an exponential-backoff retry loop. If the provided context is canceled (see Context.Done), the retry loop ends early, but will always run at least once.

func (*Retry) Next Uses

func (r *Retry) Next() bool

Next returns whether the retry loop should continue, and blocks for the appropriate length of time before yielding back to the caller.

Next is guaranteed to return true on its first call. As such, a retry loop can be thought of as implementing do-while semantics (i.e. always running at least once). Otherwide, if a context and/or closer is present, Next will return false if the context is canceled and/or the closer fires while the method is waiting.

func (*Retry) NextCh Uses

func (r *Retry) NextCh() <-chan time.Time

NextCh returns a channel which will receive when the next retry interval has expired.

func (*Retry) Reset Uses

func (r *Retry) Reset()

Reset resets the Retry to its initial state, meaning that the next call to Next will return true immediately and subsequent calls will behave as if they had followed the very first attempt (i.e. their backoffs will be short). The exception to this is if the provided Closer has fired or context has been canceled, in which case subsequent calls to Next will still return false immediately.

Package retry imports 7 packages (graph) and is imported by 501 packages. Updated 2020-08-03. Refresh now. Tools for package owners.