Example #1
0
func (w *Worker) Start() {

	killable.Defer(w, func() {
		fmt.Printf("worker: %s: all processes complete, cleaning up\n", w.name)
	})

	killable.Go(w, func() error {
		w.startProducer()
		return w.consumer()
	})
}
Example #2
0
func (w *Worker) startProducer() {

	// producer (non-blocking)
	killable.Go(w, func() error {
		defer close(w.ch)
		var i int64

		for {
			select {
			case w.ch <- i:
				i++
			case <-w.Dying():
				return killable.ErrDying
			}

			if i > 100 {
				return fmt.Errorf("worker: %s: limit reached", w.name)
			}
		}
		return nil
	})
}