All Projects → orbs-network → govnr

orbs-network / govnr

Licence: MIT license
Golang library to launch supervised goroutines and log panics properly

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

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)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].