try

package module
v0.0.0-...-b2edf0c Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 2 Imported by: 0

README

Idiomatic Go retry package. Thanks to @rowland for code review.

It is based on Mat Reyer's try package, modified to return multierror.

Usage

Just call try.Do with the function you want to retry in the event of an error:

  • Call try.Do that returns a bool indicating whether to retry or not, and an error
  • The attempt argument will start at 1 and count up
  • try.Do blocks until you return false, or a nil error
  • try.Do returns the last error or nil if it was successful
var value string
err := try.Do(func(attempt int) (bool, error) {
  var err error
  value, err = SomeFunction()
  return attempt < 5, err // try 5 times
})
if err != nil {
  log.Fatalln("error:", err)
}

In the above example the function will be called repeatedly until error is nil, while attempt < 5 (i.e. try 5 times)

Retrying panics

Try supports retrying in the event of a panic.

  • Use named return parameters
  • Set retry first
  • Defer the recovery code, and set err manually in the case of a panic
  • Use empty return statement at the end
var value string
err := try.Do(func(attempt int) (retry bool, err error) {
  retry = attempt < 5 // try 5 times
  defer func() {
    if r := recover(); r != nil {
      err = errors.New(fmt.Sprintf("panic: %v", r))
    }
  }()
  value, err = SomeFunction()
  return
})
if err != nil {
  log.Fatalln("error:", err)
}

Delay between retries

To introduce a delay between retries, just make a time.Sleep call before you return from the function if you are returning an error.

var value string
err := try.Do(func(attempt int) (bool, error) {
  var err error
  value, err = SomeFunction()
  if err != nil {
    time.Sleep(1 * time.Minute) // wait a minute
  }
  return attempt < 5, err
})
if err != nil {
  log.Fatalln("error:", err)
}

Maximum retry limit

To avoid infinite loops, Try will ensure it only makes try.MaxRetries attempts. By default, this value is 10, but you can change it:

try.MaxRetries = 20

To see if a Do operation failed due to reaching the limit, you can check the error with try.IsMaxRetries(err).

Documentation

Overview

Package try provides retry functionality.

var value string
err := try.Do(func(attempt int) (retry bool, err error) {
  var err error
  value, err = SomeFunction()
  return attempt < 5, err // try 5 times
})
if err != nil {
  log.Error(err, "somefunction failed")
}

This package was created by Mat Ryer and modified to support multierr, the original package is available at:

https://github.com/matryer/try

With the post introducing it at:

https://medium.com/@matryer/retrying-in-golang-quicktip-f688d00e650a#.3r2nbnjwu

Index

Constants

This section is empty.

Variables

View Source
var MaxRetries = 10

MaxRetries is the maximum number of retries before bailing.

Functions

func Do

func Do(fn Func) error

Do keeps trying the function until the second argument returns false, or no error is returned, attempt is started at 1.

A *multierror.Error combining all attempt errors on failure. If the function does not return true before MaxRetries then the combination of all errors that occurred will be returned which IsMaxRetries() will return true for.

func IsMaxRetries

func IsMaxRetries(err error) bool

IsMaxRetries checks whether the error is due to hitting the maximum number of retries or not.

Types

type Func

type Func func(attempt int) (retry bool, err error)

Func represents functions that can be retried.

Jump to

Keyboard shortcuts

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