package module
v2.0.0-...-d5d1b58 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2016 License: BSD-3-Clause Imports: 4 Imported by: 1,495


Installation and usage

See for documentation and usage details.



The tomb package handles clean goroutine tracking and termination.

The zero value of a Tomb is ready to handle the creation of a tracked goroutine via its Go method, and then any tracked goroutine may call the Go method again to create additional tracked goroutines at any point.

If any of the tracked goroutines returns a non-nil error, or the Kill or Killf method is called by any goroutine in the system (tracked or not), the tomb Err is set, Alive is set to false, and the Dying channel is closed to flag that all tracked goroutines are supposed to willingly terminate as soon as possible.

Once all tracked goroutines terminate, the Dead channel is closed, and Wait unblocks and returns the first non-nil error presented to the tomb via a result or an explicit Kill or Killf method call, or nil if there were no errors.

It is okay to create further goroutines via the Go method while the tomb is in a dying state. The final dead state is only reached once all tracked goroutines terminate, at which point calling the Go method again will cause a runtime panic.

Tracked functions and methods that are still running while the tomb is in dying state may choose to return ErrDying as their error value. This preserves the well established non-nil error convention, but is understood by the tomb as a clean termination. The Err and Wait methods will still return nil if all observed errors were either nil or ErrDying.

For background and a detailed example, see the following blog post:



This section is empty.


View Source
var (
	ErrStillAlive = errors.New("tomb: still alive")
	ErrDying      = errors.New("tomb: dying")


This section is empty.


type Tomb

type Tomb struct {
	// contains filtered or unexported fields

A Tomb tracks the lifecycle of one or more goroutines as alive, dying or dead, and the reason for their death.

See the package documentation for details.

func WithContext

func WithContext(parent context.Context) (*Tomb, context.Context)

WithContext returns a new tomb that is killed when the provided parent context is canceled, and a copy of parent with a replaced Done channel that is closed when either the tomb is dying or the parent is canceled. The returned context may also be obtained via the tomb's Context method.

func (*Tomb) Alive

func (t *Tomb) Alive() bool

Alive returns true if the tomb is not in a dying or dead state.

func (*Tomb) Context

func (t *Tomb) Context(parent context.Context) context.Context

Context returns a context that is a copy of the provided parent context with a replaced Done channel that is closed when either the tomb is dying or the parent is cancelled.

If parent is nil, it defaults to the parent provided via WithContext, or an empty background parent if the tomb wasn't created via WithContext.

func (*Tomb) Dead

func (t *Tomb) Dead() <-chan struct{}

Dead returns the channel that can be used to wait until all goroutines have finished running.

func (*Tomb) Dying

func (t *Tomb) Dying() <-chan struct{}

Dying returns the channel that can be used to wait until t.Kill is called.

func (*Tomb) Err

func (t *Tomb) Err() (reason error)

Err returns the death reason, or ErrStillAlive if the tomb is not in a dying or dead state.

func (*Tomb) Go

func (t *Tomb) Go(f func() error)

Go runs f in a new goroutine and tracks its termination.

If f returns a non-nil error, t.Kill is called with that error as the death reason parameter.

It is f's responsibility to monitor the tomb and return appropriately once it is in a dying state.

It is safe for the f function to call the Go method again to create additional tracked goroutines. Once all tracked goroutines return, the Dead channel is closed and the Wait method unblocks and returns the death reason.

Calling the Go method after all tracked goroutines return causes a runtime panic. For that reason, calling the Go method a second time out of a tracked goroutine is unsafe.

func (*Tomb) Kill

func (t *Tomb) Kill(reason error)

Kill puts the tomb in a dying state for the given reason, closes the Dying channel, and sets Alive to false.

Althoguh Kill may be called multiple times, only the first non-nil error is recorded as the death reason.

If reason is ErrDying, the previous reason isn't replaced even if nil. It's a runtime error to call Kill with ErrDying if t is not in a dying state.

func (*Tomb) Killf

func (t *Tomb) Killf(f string, a ...interface{}) error

Killf calls the Kill method with an error built providing the received parameters to fmt.Errorf. The generated error is also returned.

func (*Tomb) Wait

func (t *Tomb) Wait() error

Wait blocks until all goroutines have finished running, and then returns the reason for their death.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL