Git Product home page Git Product logo

try's Introduction

try GoDoc Go Report Card

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

go get gopkg.in/matryer/try.v1
or
drop gopkg.in/matryer/try.v1
  • Learn more about Drop

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).

try's People

Contributors

geoffreybauduin avatar matryer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

try's Issues

Concurrent retries

Hi Thanks for sharing this. What are your thoughts on implementing concurrent retries. We could make it go to a buffered channel on success and break/return as soon as that channel receives something.

Package is not stateless, not concurrency-safe

It looks like it is not possible to use this package in several places with different values for MaxRetries. Also it is not concurrency safe to mutate this value if package is used from multiple goroutines - e.g. one might be setting MaxRetries value and another one will be reading without any synchronization.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.