Package workgroup
is a Go client library providing a wrapper for implementing a sync.WaitGroup. It attempts to eliminate all the boilerplate often required when writing sync.WaitGroup workers.
go get -v github.com/Spatially/go-workgroup
See GoDoc or Go Walker for automatically generated documentation.
This is a Worker function. The workgroup will start however many of these you specify. In this example, it will start one for each CPU (see 3 below).
workhorse := func(worker int, work workgroup.Work) {
log.Printf("%d %+v Done : +%v", worker, work, time.Now())
}
This is a Work-Generator. It simply feeds work to each workhorse
goroutine as each is ready for Work. Although the Workers are goroutines, a workgroup uses sync.WaitGroup internally so this goroutine will block the out channel until a Worker reads from the channel. The completion of this signals the workgroup's cleanup process (all the Workers will complete their work).
workUnits := workgroup.Generator(func(out chan<- workgroup.Work) {
for i := 0; i < 100; i++ {
out <- i
}
})
This configures and initates the workgroup.
FanOut
specifies how many Workers to start. 8 in this case.Drain
specifies the Generator function.With
provides the Worker function.
workgroup.FanOut(8).Drain(workUnits).With(workhorse).Go()