Giter Club home page Giter Club logo

go-reroutine's Introduction

Reroutine

codecov

Easily restart go-routines when they panic. This package provides an easy way to restart a go-routine when it panics but to ignore a restart if it returned normally. It behaves identically to go func() but re-calls the provided function if the function panicked. This is useful for long-running worker routines that don't maintain their own state.

Use cases

This library is not a replacement for proper error handling, nor is it a replacement for a service lifecycle manager like Kubernetes or Nomad. This package was specifically designed to keep long-running scheduler and worker go-routines running in uncontrolled and unmanaged edge/IoT environments.

Installation

go get github.com/clarkmcc/go-reroutine

Example

Basic

Calling reroutine.Go behaves exactly like calling a function inside a regular go-routine (go func()) where the operation is non-blocking. In the following example, the provided function will be restarted on panic until the stop channel is closed.

stop := make(chan struct{})
reroutine.Go(stop, func() {
  // Do something here that could panic and should be resumed on panic
})

Full

The following example illustrates how a go-routine can panic and restart to continue its process of incrementing i. In this case, the go-routine closes the stop channel when its incremented i three times, and panics on every iteration.

// Create a counter. Once this gets to 3 we want to stop restarting. Until then, we want to
// panic on every increment to prove that we're restarting the go-routine through panics and
// continuing the work.
i := int64(0)

reroutine.Go(stop, func() {
  for {
    // Increment until we get to 3, then stop restarting
    if atomic.AddInt64(&i, 1) == 3 {
      close(stop)
    }
		
    // Panic on every iteration
    panic("panicked")
  }
})

// Make sure that the incrementation was performed
if atomic.LoadInt64(&i) != 3 {
  panic("expected three iterations")
}

Blocking Restart

Sometimes it might be useful to block until the panicking go-routine is able to successfully complete.

stop := make(chan struct{})
reroutine.BlockingGo(stop, func() {
  // Do something that we should wait for
})

go-reroutine's People

Contributors

clarkmcc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.