Documentation
¶
Overview ¶
Package pool provides lifecycle-managed worker pools with bounded queues for background task execution.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPoolClosed = errors.New("worker pool is closed")
ErrPoolClosed is returned by Submit after the pool has been closed.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Instance name
Name string `koanf:"-"`
// Pools defines the named pools this module manages. Prefer snake_case
// pool names; hyphens cannot be overridden via environment variables.
Pools map[string]PoolConfig `koanf:"pools"`
// CodePools holds pools registered via WithPool (code-only). A config
// entry with the same name replaces it wholesale.
CodePools map[string]PoolConfig `code_only:"WithPool" koanf:"-"`
}
Config represents configuration for the worker pool Module
func NewDefaultConfig ¶
func NewDefaultConfig() Config
NewDefaultConfig returns default configuration
func (*Config) LoadFromKoanf ¶
LoadFromKoanf loads configuration from koanf instance at the given path.
func (*Config) MergedPools ¶
func (c *Config) MergedPools() map[string]PoolConfig
MergedPools combines code-registered and config-defined pools; config wins per name.
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future holds the eventual result of a task submitted via SubmitResult.
func SubmitResult ¶
func SubmitResult[T any](ctx context.Context, p *Pool, fn func(ctx context.Context) (T, error)) (*Future[T], error)
SubmitResult enqueues a task whose result the caller awaits via the returned Future. Unlike Submit, the task keeps the submitter's live context: cancellation propagates, and a task still queued when ctx is cancelled is skipped. Panics surface as errors from Get.
type Module ¶
Module provides a Registry of named worker pools via DI.
func (*Module) ConfigPath ¶
ConfigPath returns the koanf path for this module's configuration.
func (*Module) Dependencies ¶
Dependencies declares the optional types this module needs from DI before Init.
func (*Module) LoadConfig ¶
LoadConfig loads configuration from koanf.
type Option ¶
type Option func(m *Config)
Option configures the Module.
func WithPool ¶
func WithPool(name string, cfg PoolConfig) Option
WithPool registers a pool in code (code-only); config with the same name takes precedence.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool executes submitted tasks on a bounded set of workers fed by a bounded FIFO queue.
func (*Pool) Close ¶
Close stops intake and drains queued and in-flight tasks, bounded by ctx. Submit returns ErrPoolClosed afterwards. Close is idempotent.
type PoolConfig ¶
type PoolConfig struct {
// Workers is the number of concurrent workers. Zero or less uses NumCPU.
Workers int `koanf:"workers"`
// QueueSize is the pending-task queue capacity. Nil uses the default
// (1024); zero means direct handoff to an idle worker.
QueueSize *int `koanf:"queue_size"`
}
PoolConfig configures a single pool.