Documentation
¶
Index ¶
- Variables
- type Tomb
- func (t *Tomb) Alive() bool
- func (t *Tomb) Context(parent context.Context) context.Context
- func (t *Tomb) Dead() <-chan struct{}
- func (t *Tomb) Dying() <-chan struct{}
- func (t *Tomb) Err() (reason error)
- func (t *Tomb) Go(f func() error) error
- func (t *Tomb) Kill(reason error) error
- func (t *Tomb) Killf(f string, a ...interface{}) error
- func (t *Tomb) Wait() error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrStillAlive is a sentinel error to identify when a tomb is still alive. ErrStillAlive = errors.New("still alive") // ErrDying is a sentinel error to identify when a tomb is dying. ErrDying = errors.New("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 WithContext ¶
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) 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 ¶
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.
Although 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.