workerpool

package module
v0.0.0-...-0b5cdef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

workerpool

Go worker pool library.

This library makes it easier to create multiple workers to execute identical tasks over multiple input items in parallel. Items are sent through an input channel, processed with a provided worker function, and the results are sent to an output channel.

The library allows arbitrary task interruption and timeouts using a context.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Errors.
	ErrInvalidNumWorkers = errors.New("number of workers must be positive")
)

Functions

This section is empty.

Types

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

WorkerPool implements a worker pool for executing the same task over many items in parallel.

func New

func New(workerFunc worker.WorkerFunc, numWorkers int) (*WorkerPool, error)

New returns a new WorkerPool instance that will use the given workerFunc to process input items and will have numWorkers workers.

func (*WorkerPool) GetOutputChannel

func (wp *WorkerPool) GetOutputChannel() (<-chan interface{}, error)

GetOutputChannel returns the channel where the result of processing input items will be sent to. The channel will be internally allocated the first time this is called. This must be called at least once before Start().

func (*WorkerPool) SetInputChannel

func (wp *WorkerPool) SetInputChannel(inputChannel chan interface{}) error

SetInputChannel sets the channel where the Workers will read items from. This must be called before Start().

func (*WorkerPool) Start

func (wp *WorkerPool) Start(ctx context.Context) error

Start starts the WorkerPool with the given context. The context can be used to stop the WorkerPool with an explicit cancelation or with a deadline and it can also be used to pass required data to the workerFunc.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL