Documentation
¶
Overview ¶
Package workpool provides a bounded-concurrency worker pool backed by a semaphore and a WaitGroup.
Usage:
pool := workpool.New("ingester", 8) // max 8 concurrent tasks
for _, item := range items {
item := item
pool.Submit(func() { process(item) })
}
pool.Wait()
Package workpool is a bounded-concurrency goroutine pool used by every fleet service that fans out work (subdomain-finder, broken-links, iframe-analyzer's recursive walker, …). Today each reimplements its own semaphore + WaitGroup; this package centralises the shape and emits in-flight + queue-depth metrics via the Observer hook.
Semantics:
- New(name, size) returns a pool with `size` worker slots.
- Submit(task) blocks until a slot is free, then runs task in a goroutine. SubmitCtx blocks bounded by ctx.
- TrySubmit(task) returns false immediately if no slot is free — useful for shedding load.
- Wait() blocks until every submitted task has finished.
- Close() prevents further submits and waits for in-flight tasks.
The pool is intentionally minimal: no priority queue, no per-task timeout, no retries — those are the caller's job. The pool's only responsibility is "at most N tasks run at once."
Index ¶
- Variables
- func SetDefaultObserver(o Observer)
- type Event
- type Observer
- type Phase
- type Pool
- func (p *Pool) Close()
- func (p *Pool) InFlight() int64
- func (p *Pool) Name() string
- func (p *Pool) QueueDepth() int64
- func (p *Pool) Size() int
- func (p *Pool) Submit(task func()) error
- func (p *Pool) SubmitCtx(ctx context.Context, task func()) error
- func (p *Pool) TrySubmit(task func()) bool
- func (p *Pool) Wait()
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("workpool: closed")
ErrClosed is returned by Submit/TrySubmit after Close has been called.
Functions ¶
func SetDefaultObserver ¶
func SetDefaultObserver(o Observer)
SetDefaultObserver installs a process-wide observer. Pass nil to disable. Wired by promx.AutoWire.
Types ¶
type Observer ¶
type Observer interface {
ObserveWorkpool(Event)
}
Observer receives one event per Submit / TrySubmit lifecycle phase. Implementations MUST NOT block.
func DefaultObserver ¶
func DefaultObserver() Observer
DefaultObserver returns the current process-wide observer or nil.
type Phase ¶
type Phase string
Phase buckets the observer event types.
const ( PhaseQueued Phase = "queued" // a Submit caller is waiting for a slot PhaseStarted Phase = "started" // a task obtained a slot and began running PhaseFinished Phase = "finished" // a task returned and released its slot PhaseCanceled Phase = "canceled" // a queued task was not admitted because its context or pool closed PhaseShed Phase = "shed" // TrySubmit refused because no slot was free )
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a bounded-concurrency worker pool. Construct with New; safe for concurrent Submit calls.
func New ¶
New constructs a pool with `size` concurrent slots. Name is the pool slug ("crawler", "fanout-resolver") used as a metric label — keep it short and stable.
func (*Pool) Close ¶
func (p *Pool) Close()
Close prevents further submits and waits for in-flight tasks to finish. Safe to call multiple times.
func (*Pool) QueueDepth ¶
QueueDepth returns the current number of Submit callers blocked waiting for a slot.
func (*Pool) Submit ¶
Submit blocks until a slot is available, then starts task in a goroutine. Returns ErrClosed if the pool has been closed.
func (*Pool) SubmitCtx ¶
SubmitCtx is Submit bounded by ctx. If ctx fires while waiting for a slot, ctx.Err() is returned and task is not started.