Skip to content

foozlevazquez/go-workgroup

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

workgroup - wraps sync.WaitGroup

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.

Status

Build Status

Installation

go get -v github.com/Spatially/go-workgroup

Documentation

See GoDoc or Go Walker for automatically generated documentation.

Usage

1. Define a Worker

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())
}

2. Define a Work-Generator function

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

3. Initiate the WorkGroup with 8 workers

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

About

go-workgroup - wraps sync.WaitGroup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%