retry

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExceededAttempts is an error that is returned for a task that was run
	// too many times, without being successful.
	ErrExceededAttempts = errors.New("maximum attempts exceeded")

	// ErrExceededTime is an error that is returned for a task that ran too
	// long, without being successful.
	ErrExceededTime = errors.New("maximum time exceeded")
)

Functions

func Retry

func Retry(spec Spec, task Task) error

Retry will repeatedly run the given task, until it is successful. The given spec is used for determining what exactly is considered "successful", and how to handle timing of the potentially multiple task invocations.

Types

type ExecTask

type ExecTask struct {
	Name  string
	Args  []string
	Quiet bool
}

func (ExecTask) Run

func (t ExecTask) Run(ctx context.Context) error

type HTTPTask

type HTTPTask struct {
	URL string
}

func (HTTPTask) Run

func (t HTTPTask) Run(ctx context.Context) error

type Spec

type Spec struct {
	// Attempts is the maximum number of total times a task is run. A value of
	// zero removes this restriction, and a task will be run indefinitely.
	Attempts int

	// Backoff is for enabling exponential backoff between task invocations.
	// The time between tasks will double each time there is a failure, but
	// will reset if there is a subsequent success.
	Backoff bool

	// Consecutive is the number of successful task runs that must happen in a
	// row in order for the task to be considered successful overall.
	Consecutive int

	// InitialDelay is the duration to pause before initially starting task
	// invocations.
	InitialDelay time.Duration

	// Invert is used to indicate that the task success status should be
	// reversed. Failed tasks count as successful, and vice versa.
	Invert bool

	// Jitter is the duration range to randomly add to the Sleep time.
	// Sleep + [0, Jitter)
	Jitter time.Duration

	// Sleep is the duration to pause between individual task invocations.
	Sleep time.Duration

	// TaskTime is the maximum time that an individual task invocation is
	// allowed to take.
	TaskTime time.Duration

	// TotalTime is the maximum time that all combined task invocations are
	// allowed to take.
	TotalTime time.Duration
}

Spec represents the various behavior parameters for retrying a task.

type Task

type Task interface {
	Run(context.Context) error
}

Jump to

Keyboard shortcuts

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