func (catacomb *Catacomb) add(w worker.Worker) {
	// We must wait for _both_ goroutines to exit in
	// arbitrary order depending on the order of the worker
	// and the catacomb shutting down.
	workerDone := make(chan struct{})
	catacomb.wg.Add(2)
	go func() {
		defer catacomb.wg.Done()
		defer close(workerDone)
		if err := w.Wait(); err != nil {
			catacomb.Kill(err)
		}
	}()
	go func() {
		defer catacomb.wg.Done()
		select {
		case <-catacomb.tomb.Dying():
			worker.Stop(w)
		case <-workerDone:
			// Exit the go routine to release the worker's memory.
		}
	}()
}
// add starts two goroutines that (1) kill the catacomb's tomb with any
// error encountered by the worker; and (2) kill the worker when the
// catacomb starts dying.
add starts two goroutines that (1) kill the catacomb's tomb with any
error encountered by the worker; and (2) kill the worker when the
catacomb starts dying.
// add starts two goroutines that ( 1 ) kill the catacomb ' s tomb with any // error encountered by the worker ; and ( 2 ) kill the worker when the // catacomb starts dying .
func (catacomb *Catacomb) add(w worker.Worker) {
	workerDone := make(chan struct{})
	catacomb.wg.Add(2)
	go func() {
		defer catacomb.wg.Done()
		defer close(workerDone)
		if err := w.Wait(); err != nil {
			catacomb.Kill(err)
		}
	}()
	go func() {
		defer catacomb.wg.Done()
		select {
		case <-catacomb.tomb.Dying():
			worker.Stop(w)
		case <-workerDone:
		}
	}()
}
func ( catacomb * Catacomb ) add ( w worker . Worker ) { workerDone := make ( chan struct { } ) catacomb . wg . Add ( 2 ) go func ( ) { defer catacomb . wg . Done ( ) defer close ( workerDone ) if err := w . Wait ( ) ; err != nil { catacomb . Kill ( err ) } } ( ) go func ( ) { defer catacomb . wg . Done ( ) select { case <- catacomb . tomb . Dying ( ) : worker . Stop ( w ) case <- workerDone : } } ( ) }
add starts two goroutines that (1) kill the catacomb's tomb with any
error encountered by the worker; and (2) kill the worker when the
catacomb starts dying.
add starts two goroutines that ( 1 ) kill the catacomb ' s tomb with any error encountered by the worker ; and ( 2 ) kill the worker when the catacomb starts dying .
// We must wait for _both_ goroutines to exit in // arbitrary order depending on the order of the worker // and the catacomb shutting down. // Exit the go routine to release the worker's memory.