This is a simple local execution framework for Go, enabling the interchangeable use of synchronous and asynchronous execution strategies.
The core interface is Strategy which has a simple signature:
type Strategy interface {
Do(task func())
}
There are 4 implementations of the Strategy interface:
Boundeduses a semaphore to limit the maximum number of goroutinesDirectruns every task directly on the caller's goroutinePooluses a fixed-size pool of goroutines to run queued tasksUnboundedstarts a new goroutine for each task
Except for Direct, every implementation provides a Wait method which
blocks until all running tasks are done (although you have to call Stop
first on a Pool).
Bounded and Pool also provide additional methods for running tasks:
Trywon't block at allTryUntilwon't block past a timeout duration
There are unit tests with good coverage on all of the functions and methods, but they are timing sensitive. For accurate results, the tests should not be run on machines under heavy load.
The semaphore implementation backing Bounded is taken from
Go Language Patterns.