Sunmxt / arbiter

Trace goroutine lifecycles.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Trace goroutine lifecycles.


Languages

Language:Go 87.6%Language:Makefile 12.4%