retrycontext

package
v0.0.0-...-5044e41 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: MIT Imports: 5 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Settings Settings

	Tries     int
	LastError error
}

Context stores state related to an operation that should be retried.

func New

func New(settings Settings) *Context

New returns a new retry context with specific settings.

func NewDefault

func NewDefault() *Context

NewDefault returns a new retry context with default settings.

func (*Context) Retry

func (rc *Context) Retry(err error)

Retry records an error that was retried (accessible in LastError) If a consumer was passed, it'll pause progress, and log the error. It's also in charge of sleeping (following exponential backoff)

func (*Context) ShouldTry

func (rc *Context) ShouldTry() bool

ShouldTry must be used in a loop, like so:

----------------------------------------

for rc.ShouldRetry() {
	 err := someOperation()
	 if err != nil {
		 if isRetriable(err) {
			 rc.Retry(err.Error())
			 continue
		 }
	 }

	 // succeeded!
	 return nil // escape from loop
}

// THIS IS IMPORTANT return errors.New("task: too many failures, giving up") ----------------------------------------

If you forget to return an error after the loop, if there are too many errors you'll just keep running.

type Settings

type Settings struct {
	MaxTries  int
	Consumer  *state.Consumer
	NoSleep   bool
	FakeSleep func(d time.Duration)
}

Settings configures a retry context, allowing to specify a maximum number of tries, whether to sleep or not, and an optional consumer to log activity to.

Jump to

Keyboard shortcuts

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