Documentation
¶
Overview ¶
Package koi is a generic goroutine and worker manager. A Pond owns a set of named Workers, routes requests to them, and shuts them down gracefully.
Index ¶
- Variables
- type NoReturn
- type Pond
- func (p *Pond[T, E]) AddWork(workerID string, request T) (<-chan E, error)
- func (p *Pond[T, E]) Close()
- func (p *Pond[T, E]) MapResults[U any](workerID string, fn func(E) U) <-chan U
- func (p *Pond[T, E]) MustRegisterWorker(id string, worker *Worker[T, E])
- func (p *Pond[T, E]) RegisterWorker(id string, worker *Worker[T, E]) error
- func (p *Pond[T, E]) ResultChan(workerID string) <-chan E
- type Worker
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWorkerNotFound is returned when no worker is registered under the given id. ErrWorkerNotFound = errors.New("worker not found") // ErrMinConcurrentCount is returned when a worker would run with fewer than one goroutine. ErrMinConcurrentCount = errors.New("concurrent count must be at least 1") // ErrPondClosed is returned by operations on a pond that is already closed. ErrPondClosed = errors.New("pond is closed") )
Functions ¶
This section is empty.
Types ¶
type NoReturn ¶
type NoReturn int
NoReturn is the result type for fire-and-forget workers that produce no meaningful output. Use it as the E type parameter together with None.
const None NoReturn = 0
None is the canonical NoReturn value returned by workers without a result.
type Pond ¶
Pond owns a set of named workers and routes work to them. It is safe for concurrent use.
func (*Pond[T, E]) AddWork ¶
AddWork enqueues request for the worker registered under workerID and returns that worker's result channel.
func (*Pond[T, E]) Close ¶
func (p *Pond[T, E]) Close()
Close stops every worker, waits for in-flight work to finish, and closes each worker's result channel. After Close returns, AddWork and RegisterWorker fail with ErrPondClosed. Close is idempotent.
func (*Pond[T, E]) MapResults ¶
MapResults returns a channel that yields fn applied to each result produced by the worker registered under workerID, or nil if no such worker exists.
The result type U is chosen per call and is independent of the pond's own result type E: MapResults is a Go 1.27 generic method, so U lives in the method's scope rather than the package's. The returned channel is closed once the worker's result channel drains, i.e. after Close.
MapResults consumes from the worker's result channel, so a given worker's results should be read either through MapResults or through ResultChan, not both.
func (*Pond[T, E]) MustRegisterWorker ¶
MustRegisterWorker is like RegisterWorker but panics on error.
func (*Pond[T, E]) RegisterWorker ¶
RegisterWorker validates and starts the worker, making it addressable by id.
func (*Pond[T, E]) ResultChan ¶
ResultChan returns the result channel of the worker registered under workerID, or nil if no such worker exists.
type Worker ¶
type Worker[T any, E any] struct { QueueSize uint ConcurrentCount int Work func(T) E ResultChan chan E RequestChan chan T // contains filtered or unexported fields }
Worker runs Work concurrently over requests received on RequestChan and, unless its result type is NoReturn, publishes results on ResultChan.
func MustNewWorker ¶
MustNewWorker is like NewWorker but panics on a validation error.
func NewWorker ¶
func NewWorker[T any, E any](work func(T) E, queueSize uint, concurrentCount int) (*Worker[T, E], error)
NewWorker creates and validates a Worker. queueSize sets the buffer of both the request and result channels; concurrentCount sets how many goroutines process requests in parallel and must be at least 1.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
printer
command
Package main shows a fire-and-forget koi pond: ten printer jobs are queued on a single worker and the pond is closed once they are done.
|
Package main shows a fire-and-forget koi pond: ten printer jobs are queued on a single worker and the pond is closed once they are done. |
|
squares
command
Package main shows a koi pond that returns results: squares are computed by ten goroutines and read back both raw and mapped to strings.
|
Package main shows a koi pond that returns results: squares are computed by ten goroutines and read back both raw and mapped to strings. |