Documentation
¶
Overview ¶
Package concurrency provides convenience functions for working with concurrency in Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WorkerPool ¶
func WorkerPool[T, U any]( ctx context.Context, workers int, jobData []T, workerFn WorkerFunc[T, U], ) chan WorkerPoolResult[T, U]
WorkerPool executes the worker function for a fixed amount of threads (workers). Each worker function processes one element of the jobData array. The returned result channel gets closed once all jobs have been processed. The channel must be read until it is closed, otherwise the goroutine will leak.
Types ¶
type PanicError ¶ added in v1.2.1
PanicError is a custom error that contains information about a panic.
func (*PanicError) Error ¶ added in v1.2.1
func (e *PanicError) Error() string
type WorkerFunc ¶
WorkerFunc is a function that processes a job of type T and returns a result of type U. The error return value should be nil if the job was processed successfully.
type WorkerPoolResult ¶
type WorkerPoolResult[T, U any] struct { // Job is the original job that was processed. // This can be useful for correlating results with their input data. Job T // JobIndex is the index of the job in the original jobData slice. // It can be used to maintain the order of results if needed. JobIndex int // Result is the output of the worker function for the given job. // It will be of type U, as defined by the WorkerFunc. Result U // Error is the error returned by the worker function for the given job. // It will be nil if the job was processed successfully. Error error }
WorkerPoolResult is used to return worker pool results via the worker pool channel.
Source Files
¶
- errors.go
- workerpool.go