Derived from project CrossMesh
Arbiter is tracer to manage goroutine lifecycles , preventing risk of goroutine leak. It also simplifies implement of Graceful Termination.
import arbit "github.com/sunmxt/utt/arbiter"
arbiter := arbit.New() // new arbiter
arbiter.Go(func(){
// ... do something ...
})
arbiter.Do(func(){
// ... do something ...
})
arbiter.Shutdown() // shutdown. Arbiter will send exit signal to all goroutines and executions.
arbiter.StopOSSignals(syscall.SIGTERM, syscall.SIGINT) // SIGTERM and SIGINT will tigger Shutdown().
Shutdown signal will be sent via a channel.
select {
case <-arbiter.Exit(): // watch for a shutdown signal.
// ...do cleanning...
case ...
case ...
}
Or you may periodically check arbiter.Shutdown(). For example:
for arbiter.ShouldRun() {
// ... do something ...
}
// ...do cleanning...
arbiter.Join() // blocked until all goroutines and executions exited.
arbiter.Arbit() // Let SIGTERM and SIGINT tigger Shutdown() than wait.
This is an shortcut of:
arbiter.StopOSSignals(syscall.SIGTERM, syscall.SIGINT)
arbiter.Join()
Create derived Arbiter.
child := arbit.NewWithParent(arbiter) // new child arbiter
Many derived arbiters forms a arbiter tree, which has following properties:
- Derived arbiters will be automatically shut down when the parent does.
- Arbiter.Join() waits for all goroutines and executions on the arbiter tree (i.e childrens' included ) to exit