pool

package
v0.0.0-...-66e2823 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

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

View Source
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

func New(ctx context.Context, workers []Worker) *P

New creates a new pool of the given workers.

The provided context will be passed to all workers' run methods.

func (*P) Run

func (p *P) Run() error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳