Skip to content

Golang library to launch supervised goroutines and log panics properly

License

Notifications You must be signed in to change notification settings

orbs-network/govnr

Repository files navigation

govnr

one does not simply start a goroutine.

CI

Use govnr to launch supervised goroutines.

The package offers:

  • Once() launches a goroutine and logs uncaught panics.
  • Forever() launches a goroutine and in the event of a panic, log the error and re-launches, as long as the context has not been cancelled.
  • Recover() runs a function inline, in the currently running goroutine. panics are recovered, logged and ignored.

Docs are available but could probably be better. PRs will be appreciated!

Used extensively in Orbs Network's golang implementation to make sure all background processes play nicely.

Example usage:

type stdoutErrorer struct {}

func (s *stdoutErrorer) Error(err error) {
	fmt.Println(err.Error())
}

errorHandler := &stdoutErrorer{}
ctx, cancel := context.WithCancel(context.Background())

data := make(chan int)
handle := govnr.Forever(ctx, "an example process", errorHandler, func() {
	for {
		select {
		case i := <-data:
			fmt.Printf("goroutine got data: %d\n", i)
		case <-ctx.Done():
			return
		}
	}
})

supervisor := &govnr.TreeSupervisor{}
supervisor.Supervise(handle)

data <- 3
data <- 2
data <- 1
cancel()

shutdownCtx, cancel := context.WithTimeout(context.Background(), 1 * time.Second)
supervisor.WaitUntilShutdown(shutdownCtx)

About

Golang library to launch supervised goroutines and log panics properly

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

 
 
 

Contributors 4

  •  
  •  
  •  
  •