Coder Social home page Coder Social logo

go-retro's Introduction

Retro - Retryable errors in Golang

⚠️ Note: This library is no longer maintained ⚠️

Codeship Status for codeship/go-retro

Retry allows you to wrap failure prone sections of code in retry blocks, and conditionally retry different types of errors. To retry an error within a block, simply return a custom error type matching the provided interface. By default, wrapping code in a retro block will have no effect.

Usage

// Wrap your code block with retro
finalErr := retro.DoWithRetry(func() error{
    // Hit an external API
    return DoMyFunc(args)
})

Any errors matching the RetryableError interface bubbled up will automatically retry the code block.

For more detailed examples see the examples folder.

Independent sections of code within a chain can be wrapped conditionally in retro blocks as needed. You should always try and keep retry blocks as small as possible to reduce code and requests being re-run.

Retrying

You can use two types of provided error retry patterns, or create your own:

Static retryable errors

These errors retry X times every Y seconds. To create a static retryable error use retro.NewStaticRetryableError(err error, maxAttempts, waitSeconds int). For generic errors you can reference a shared variable, for dynamic errors you can create an error with the relevant base error message each time. Error retrying state is stored in the retro loop, not the RetryableError.

Backoff retryable errors

These errors use quadrilateral equation based on Sidekiq retries to increasingly space out subsequent attempts. To create a backoff retryable error use retro.NewBackoffRetryableError(err error, maxAttempts int). For generic errors you can reference a shared variable, for dynamic errors you can create an error with the relevant base error message each time. Error retrying state is stored in the retro loop, not the RetryableError.

Retry attempts

The retro retry loop will keep track of how many times it has looped. Any time it gets an error it compares the state against the allowed retry conditions for the latest error. This means that should the loop initially retry with an error allowing ten retries, if the second error indicates only two retries or fewer are allowed, then the loop will no longer retry since it has already retried twice.

go-retro's People

Contributors

bfosberry avatar jipiboily avatar

Stargazers

nodojo avatar xiyun-aws avatar Le Van avatar Gilles Barsotti avatar  avatar Michal Matczuk avatar Asaduzzaman Pavel avatar Alexey Tsverov avatar  avatar André Mar avatar Chris McKee avatar Tristan Eastburn avatar swh avatar Xiaohan Song avatar Spiros Gerokostas avatar Henson Lu avatar Javed Khan avatar Felipe Oliveira avatar  avatar Nathan Jordan avatar Taras avatar Señor Gus avatar Felix  avatar Murat Gurel avatar Rafael Jesus avatar Benjamin Edwards avatar Anirudh Aithal avatar Timesking avatar Joshua Rubin avatar Dario Castañé avatar Igor Dubinskiy avatar Nicolas M. avatar Ibrahim AshShohail avatar Phil Kates avatar Alfonso Cabrera avatar Raul Martinez Jr avatar Lucas Bremgartner avatar Marc Boeker avatar Steven Maude avatar Dmitry Chusovitin avatar Nick Tan avatar BadTimo avatar Chris Dillon avatar Miguel Eduardo Gil Biraud avatar Pietro De Caro avatar Artem Nistratov avatar Pana avatar carlos L avatar Magicshui avatar Sadlil Rhythom avatar Johnny Boursiquot avatar Drew LeSueur avatar Uwe Dauernheim avatar Paul Seiffert avatar 爱可可-爱生活 avatar Ivan K. avatar Ethan Yu avatar Michael S. Manley avatar Anthony HAMON avatar George Erickson avatar Miki Oracle avatar Jim Schley avatar Matt Perkinson avatar Piotr Kubisa avatar Mateusz "Serafin" Gajewski avatar Jonathan Gautheron avatar Damon Zhao avatar Todd Siegel avatar  avatar Stefan Haacker avatar Takeshi Yamashita avatar  avatar Jason avatar Konstantin Burykin avatar Manu Chambon avatar Bjørn Erik Pedersen avatar Gergely Radics avatar Daniel Lohse avatar Lei Cao avatar Harold Miao avatar Gaurav Agarwal avatar Chirayu Patel avatar Ilsun Park avatar Valentin Hăloiu avatar Chris Hart avatar Suguru Namura avatar Caesar Wang avatar donnol avatar Kyle Truscott avatar Tanabe Ken-ichi avatar Lyric avatar Eric Yu avatar Jorge Araya Navarro avatar Raffaele Sena avatar Roman Shakhov avatar David Sauer avatar Hervé Gouchet avatar David Smith avatar Sindre Røkenes Myren avatar Yannick Heinrich avatar

Watchers

Rakhmad Azhari avatar Raul Martinez Jr avatar Lars Fronius avatar James Cloos avatar Joe Siewert avatar  avatar Gilles Barsotti avatar Jim Schley avatar  avatar  avatar  avatar

go-retro's Issues

Update Go-retro to support background workers, and goroutine handoff

Currently this works in the case where we have a synchronous set of goroutines processing work, such as with a web handler. With a limited worker pool the sleep methodology of this library makes it inefficient. A better approach would be to support some kind of handoff and a future execution time determine by the error type returned, e.g.

// Wrap your code block with retro
finalErr, retryAt := retro.DoWithAsyncRetry(func() error{
    // Hit an external API
    return DoMyFunc(args)
})

if finalErr != nil {
  logger.Error("my stuff failed :(")
  return finalErr
}

// code did not complete, need to retry
if retryAt != nil {
  // if Im using a worker queue I can specify that this task should not be handled until this time
  time.Sleep(*retryAt - time.Now())
  // try again, but in a more reusable way than this
  finalErr, retryAt := retro.DoWithAsyncRetry(func() error{
    // Hit an external API
    return DoMyFunc(args)
  })
} else {
  // error was nil, no retry needed
  return nil
}

infinite retries

I am suggesting add infinite retries feature.
For example if we use -1 in NewStaticRetryableError.

ErrNetwork = retro.NewStaticRetryableError(errors.New("error: Max retries attempts reached"), -1, 2)

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.