Help us shape the future of Go! Take the 2026 Go Developer Survey

workerpool

package
v1.27.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package workerpool provides a generic buffered worker pool: items submitted via Submit are fanned out across a fixed number of worker goroutines, each running the handler.

Index

Constants

This section is empty.

Variables

View Source
var ErrStopped = errors.New("workerpool: stopped")

ErrStopped is returned by Submit when the pool is stopped.

Functions

This section is empty.

Types

type Option

type Option func(*options)

func WithCapacity

func WithCapacity(n int) Option

WithCapacity sets the number of items buffered before Submit blocks.

func WithRateLimit

func WithRateLimit(limit rate.Limit, burst int) Option

WithRateLimit caps how fast items are handled across all workers.

func WithWorkers

func WithWorkers(n int) Option

WithWorkers sets the number of goroutines running the handler.

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](
	handler func(context.Context, T),
	opts ...Option,
) *Pool[T]

New creates a Pool, panics on invalid arguments.

func (*Pool[T]) Stop

func (p *Pool[T]) Stop(ctx context.Context) error

Stop rejects new submissions, handles buffered items and waits for the workers to finish. If ctx is done first, its error is returned and workers may still be running; calling Stop again waits again.

func (*Pool[T]) Submit

func (p *Pool[T]) Submit(ctx context.Context, value T) error

Submit enqueues value, blocking while the buffer is full. ctx's values reach the handler but its cancellation doesn't, so processing isn't aborted when the caller's ctx ends. It returns ErrStopped if the pool is stopped, or ctx's error if ctx is done before value is enqueued.

func (*Pool[T]) TrySubmit

func (p *Pool[T]) TrySubmit(ctx context.Context, value T) bool

TrySubmit is like Submit but doesn't block: it returns false if the buffer is full or the pool is stopped.

Jump to

Keyboard shortcuts

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