tomb

package
v0.0.0-...-d2438c5 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 New

func New() *Tomb

New will create a Tomb with sane defaults

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) 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) error

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.

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