pool

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package pool is a thin wrapper around github.com/alitto/pond/v2 that gives hex applications a worker pool primitive for bounded in-process concurrency.

Use hex/pool when you need to fan out N tasks across a bounded number of goroutines. It is orthogonal to hex/queue (delivery + durability) and hex/cron (timing): a queue consumer, cron job, or HTTP handler can all use a pool to run their work concurrently.

See ADR-0010 for the wrap-vs-roll-own decision.

Example:

p := pool.New(10)                              // 10-worker pool
defer p.Shutdown(ctx)                          // waits for in-flight

for _, item := range items {
    item := item
    p.Submit(func() { process(item) })
}

// Or with results:
rp := pool.NewResult[User](10)
task := rp.SubmitErr(func() (User, error) {
    return svc.Get(ctx, id)
})
user, err := task.Wait()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Shutdown

func Shutdown(ctx context.Context, p BasePool) error

Shutdown stops the pool gracefully. It stops accepting new tasks and waits for in-flight tasks to complete, up to ctx's deadline.

Returns nil on clean drain, ctx.Err() if the deadline is hit before tasks finish.

Types

type BasePool

type BasePool interface {
	StopAndWait()
	Stop() pond.Task

	RunningWorkers() int64
	SubmittedTasks() uint64
	WaitingTasks() uint64
	SuccessfulTasks() uint64
	FailedTasks() uint64
	CompletedTasks() uint64
	DroppedTasks() uint64
	CanceledTasks() uint64

	MaxConcurrency() int
	QueueSize() int
	Stopped() bool
}

BasePool is the minimum interface every pond pool satisfies. Used by Shutdown and Snapshot so the same helpers work for Pool and ResultPool.

type Metrics

type Metrics struct {
	Timestamp       time.Time
	MaxConcurrency  int
	QueueSize       int
	RunningWorkers  int64
	SubmittedTasks  uint64
	WaitingTasks    uint64
	SuccessfulTasks uint64
	FailedTasks     uint64
	CompletedTasks  uint64
	DroppedTasks    uint64
	CanceledTasks   uint64
	Stopped         bool
}

Metrics is a point-in-time snapshot of a pool's counters. Read via Snapshot for consumers that want to expose pool health via /healthz-adjacent endpoints or metrics scrapers.

func Snapshot

func Snapshot(p BasePool) Metrics

Snapshot reads all counters from p at once.

type Option

type Option = pond.Option

Option configures a new pool. Aliased from pond so callers can pass upstream options directly if they need something the hex helpers do not surface.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets a base context on the pool. When ctx is cancelled the pool stops accepting new tasks and returns ErrPoolStopped.

func WithNonBlocking

func WithNonBlocking(v bool) Option

WithNonBlocking makes Submit drop tasks that cannot be queued instead of blocking. Combine with WithQueueSize.

func WithQueueSize

func WithQueueSize(n int) Option

WithQueueSize caps the number of pending tasks the pool will accept before blocking (or dropping, with WithNonBlocking).

type Pool

type Pool = pond.Pool

Pool is the type alias for pond's non-generic pool. Consumers get the full pond API through the alias.

func New

func New(maxConcurrency int, opts ...Option) Pool

New returns a Pool with the given maximum concurrency. Zero means unlimited (goroutine per task; usually not what you want).

type ResultPool

type ResultPool[R any] = pond.ResultPool[R]

ResultPool is the type alias for pond's typed result pool.

func NewResult

func NewResult[R any](maxConcurrency int, opts ...Option) ResultPool[R]

NewResult returns a typed ResultPool that accepts tasks returning R. Use this when you need to collect return values or errors from tasks.

type ResultTask

type ResultTask[R any] = pond.Result[R]

ResultTask represents a submitted task that returns a typed result.

type Task

type Task = pond.Task

Task represents a submitted unit of work with a Wait() method.

type TaskGroup

type TaskGroup = pond.TaskGroup

TaskGroup batches multiple tasks and lets the caller Wait for all of them at once.

Jump to

Keyboard shortcuts

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