Skip to content

Latest commit

 

History

History
119 lines (73 loc) · 2.16 KB

README.md

File metadata and controls

119 lines (73 loc) · 2.16 KB

arbiter

Derived from project CrossMesh

golang Build Status codecov

Intro

Arbiter is tracer to manage goroutine lifecycles , preventing risk of goroutine leak. It also simplifies implement of Graceful Termination.

Getting Started

import arbit "github.com/sunmxt/utt/arbiter"
arbiter := arbit.New() // new arbiter

Spawn a goroutine (like "go" keyword)

arbiter.Go(func(){
  // ... do something ...
})

Trace an execution

arbiter.Do(func(){
  // ... do something ...
})

Shutdown

arbiter.Shutdown() // shutdown. Arbiter will send exit signal to all goroutines and executions. 

Intergrate Shutdown() with OS signals

arbiter.StopOSSignals(syscall.SIGTERM, syscall.SIGINT) // SIGTERM and SIGINT will tigger Shutdown().

Watch a 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...

Join (Wait)

arbiter.Join() // blocked until all goroutines and executions exited.

Arbit

arbiter.Arbit() // Let SIGTERM and SIGINT tigger Shutdown() than wait.

This is an shortcut of:

arbiter.StopOSSignals(syscall.SIGTERM, syscall.SIGINT)
arbiter.Join()

Arbiter tree

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