pool

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package pool provides lifecycle-managed worker pools with bounded queues for background task execution.

Index

Constants

This section is empty.

Variables

View Source
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 NewConfig

func NewConfig(options ...Option) Config

NewConfig returns configuration with provided options based on defaults.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns default configuration

func (*Config) LoadFromKoanf

func (c *Config) LoadFromKoanf(k *koanf.Koanf, path string) error

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.

func (*Future[T]) Get

func (f *Future[T]) Get(ctx context.Context) (T, error)

Get blocks until the task completes or ctx is done. Safe to call repeatedly and from multiple goroutines.

type Module

type Module struct {
	lakta.NamedBase
	// contains filtered or unexported fields
}

Module provides a Registry of named worker pools via DI.

func NewModule

func NewModule(options ...Option) *Module

NewModule creates a new worker pool module.

func (*Module) ConfigPath

func (m *Module) ConfigPath() string

ConfigPath returns the koanf path for this module's configuration.

func (*Module) Dependencies

func (m *Module) Dependencies() ([]reflect.Type, []reflect.Type)

Dependencies declares the optional types this module needs from DI before Init.

func (*Module) Init

func (m *Module) Init(ctx context.Context) error

Init builds the pools and provides the Registry to the injector.

func (*Module) LoadConfig

func (m *Module) LoadConfig(k *koanf.Koanf) error

LoadConfig loads configuration from koanf.

func (*Module) Provides

func (m *Module) Provides() []reflect.Type

Provides returns the types this module registers in DI.

func (*Module) Shutdown

func (m *Module) Shutdown(ctx context.Context) error

Shutdown closes all pools, draining queued tasks.

type Option

type Option func(m *Config)

Option configures the Module.

func WithName

func WithName(name string) Option

WithName sets the instance name for this 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 New

func New(cfg PoolConfig) *Pool

New creates a pool and starts its workers.

func (*Pool) Close

func (p *Pool) Close(ctx context.Context) error

Close stops intake and drains queued and in-flight tasks, bounded by ctx. Submit returns ErrPoolClosed afterwards. Close is idempotent.

func (*Pool) Submit

func (p *Pool) Submit(ctx context.Context, fn func(ctx context.Context) error) error

Submit enqueues a fire-and-forget task, blocking while the queue is full until ctx is done. The task context is detached from the submitter's cancellation but keeps its values. Task errors are logged, panics recovered.

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.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds the named pools defined in config and code options.

func (*Registry) Get

func (r *Registry) Get(name string) (*Pool, error)

Get returns the named pool, or an error listing the known pool names.

Jump to

Keyboard shortcuts

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