workerpool

package
v0.0.0-...-51a0f74 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Errorf(string, ...interface{})
	Debugf(string, ...interface{})
	Tracef(string, ...interface{})
}

Logger defines the logging methods that the worker pool uses.

type Task

type Task struct {
	// Type is a short, optional description of the task which will be
	// included in emitted log messages.
	Type string

	// Process encapsulates the logic for processing a task.
	Process func() error
}

Task represents a unit of work which should be executed by the pool workers.

type WorkerPool

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

WorkerPool implements a fixed-size worker pool for processing tasks in parallel. If any of the tasks fails, the pool workers will be shut down, and any detected errors (different tasks may fail concurrently) will be consolidated and reported when the pool's Close() method is invoked.

New tasks can be enqueued by writing to the channel returned by the pool's Queue() method while the pool's Done() method returns a channel which can be used by callers to detect when an error occurred and the pool is shutting down.

func NewWorkerPool

func NewWorkerPool(logger Logger, size int) *WorkerPool

NewWorkerPool returns a pool with the taskuested number of workers. Callers must ensure to call the pool's Close() method to avoid leaking goroutines.

func (*WorkerPool) Close

func (wp *WorkerPool) Close() error

Close the pool and return any queued errors. The method signals and waits for all workers to exit before draining the worker error channel and returning a combined error (if any errors were reported) value. After a call to Shutdown, no further provision tasks will be accepted by the pool.

func (*WorkerPool) Done

func (wp *WorkerPool) Done() <-chan struct{}

Done returns a channel which is closed if the pool has detected one or more errors and is shutting down. Callers must then invoke the pool's Close method to obtain any reported errors.

func (*WorkerPool) Queue

func (wp *WorkerPool) Queue() chan<- Task

Queue returns a channel for enqueueing processing tasks.

func (*WorkerPool) Size

func (wp *WorkerPool) Size() int

Size returns the number of workers in the pool.

Jump to

Keyboard shortcuts

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