Documentation
¶
Overview ¶
This package provides a facility to run a heterogeneous pool of workers.
Each worker is defined by an interface, and the pool execute's each worker's Run method repeatedly concurrently. The worker may exit early by returning the Done error. Each worker's Run method accepts a context.Context which is passed to it through the pool. If this context is cancelled, it may cancel workers and will always cancel the pool.
Each worker is guaranteed to start immediately when the pool's Run method is called and not any sooner.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Done error = errors.New("pool worker is done")
Done is used to signal to the pool that the worker has no more useful work to do.
Functions ¶
This section is empty.
Types ¶
type P ¶
type P struct {
// contains filtered or unexported fields
}
P implements a heterogeneous pool of Workers.
func New ¶
New creates a new pool of the given workers.
The provided context will be passed to all workers' run methods.
func (*P) Run ¶
Run signals all the workers to begin and waits for all of them to complete.
Each Worker's Run method is called in a loop until the worker returns an error or the context passed to New is cancelled. If the error is Done, then it does not propagate to Run and instead the worker stops looping.
If the context is cancelled for any reason no error is returned. Check the context for any errors in that case.
Always cleans up the pool's workers by calling Close before returning.
Returns the first error encountered from any worker that failed and cancels the rest immediately.
type Worker ¶
type Worker interface { // Run executes a task once, returning an error on failure. Run(context.Context) error // Close releases any resources associated with the Worker. Close() error }
Worker represents a stateful task which is executed repeatedly by calling its Run method. Any resources associated with the Worker may be freed with Close.