Documentation ¶
Overview ¶
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:
http://blog.labix.org/2011/10/09/death-of-goroutines-under-control
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrStillAlive = errors.New("tomb: still alive") ErrDying = errors.New("tomb: dying") )
Functions ¶
This section is empty.
Types ¶
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 (*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 ¶
Err returns the death reason, or ErrStillAlive if the tomb is not in a dying or dead state.
func (*Tomb) Go ¶
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 ¶
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.