Documentation
¶
Overview ¶
Package workerpool provides a bounded goroutine pool for Go.
Index ¶
- Variables
- type Future
- type Pool
- func (p *Pool) Drain()
- func (p *Pool) Resize(n int)
- func (p *Pool) Running() int
- func (p *Pool) Stats() PoolStats
- func (p *Pool) Stop()
- func (p *Pool) Submit(fn func())
- func (p *Pool) SubmitCtx(ctx context.Context, fn func()) error
- func (p *Pool) SubmitTimeout(fn func(), d time.Duration) error
- func (p *Pool) Wait()
- type PoolStats
Constants ¶
This section is empty.
Variables ¶
var ErrSubmitTimeout = errors.New("workerpool: submit timed out")
ErrSubmitTimeout is returned when a task submission exceeds its timeout.
Functions ¶
This section is empty.
Types ¶
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future represents a value that will be available at some point in the future. It is safe to call Get and Done from multiple goroutines concurrently.
func Go ¶
Go submits a function that returns a value and an error to the pool, and returns a Future that can be used to retrieve the result. The function runs asynchronously in the pool. Use Get to block until the result is ready.
func GoTimeout ¶ added in v0.2.0
GoTimeout submits a function that returns a value and an error to the pool, waiting at most duration d for a worker slot. It returns a Future and nil error on success, or nil and ErrSubmitTimeout if no slot becomes available within the deadline.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a bounded goroutine pool that limits the number of concurrently running goroutines. It uses a buffered channel as a semaphore to enforce backpressure — when all worker slots are occupied, Submit blocks until a slot becomes available.
func New ¶
New creates a new Pool with the given concurrency limit. The concurrency parameter controls how many goroutines can run simultaneously. It panics if concurrency is less than 1.
func (*Pool) Drain ¶ added in v0.2.0
func (p *Pool) Drain()
Drain removes all pending tasks from the queue by draining and refilling the semaphore channel. Active tasks are not interrupted and will complete normally. Because Submit blocks on the semaphore channel, any goroutines blocked in Submit will eventually proceed once Drain returns.
func (*Pool) Resize ¶ added in v0.2.0
Resize adjusts the pool's maximum concurrency to n. If n is greater than the current capacity, new slots are made available immediately. If n is smaller, excess workers are allowed to drain naturally — no goroutines are interrupted. Resize panics if n is less than 1.
func (*Pool) Running ¶
Running returns the approximate number of goroutines currently executing work. Because goroutines start and finish concurrently, the value is an approximation.
func (*Pool) Stats ¶ added in v0.2.0
Stats returns a snapshot of the pool's current activity. Workers is the maximum concurrency, Active is the number of goroutines currently running tasks, Queued is the number of occupied semaphore slots (an approximation that includes active workers), and Completed is the total number of tasks that have finished since the pool was created.
func (*Pool) Stop ¶
func (p *Pool) Stop()
Stop marks the pool as stopped and waits for all in-flight work to complete. After Stop returns, any call to Submit or SubmitCtx will panic.
func (*Pool) Submit ¶
func (p *Pool) Submit(fn func())
Submit sends a function to the pool for execution. It blocks if all worker slots are currently occupied (backpressure). The function runs in its own goroutine once a slot is available. Submit panics if the pool has been stopped.
func (*Pool) SubmitCtx ¶
SubmitCtx sends a function to the pool for execution with context support. It returns ctx.Err() if the context is cancelled or times out while waiting for a worker slot. If a slot is acquired, the function runs in its own goroutine and nil is returned. SubmitCtx panics if the pool has been stopped.
func (*Pool) SubmitTimeout ¶ added in v0.2.0
SubmitTimeout sends a function to the pool for execution, waiting at most duration d for a worker slot. It returns ErrSubmitTimeout if no slot becomes available within the deadline. SubmitTimeout panics if the pool has been stopped.