workpool

package
v0.138.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

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

type Event struct {
	Pool       string
	Phase      Phase
	InFlight   int64
	QueueDepth int64
}

Event is the per-phase payload handed to an Observer.

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

func New(name string, size int) *Pool

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) InFlight

func (p *Pool) InFlight() int64

InFlight returns the current number of running tasks.

func (*Pool) Name

func (p *Pool) Name() string

Name returns the pool slug.

func (*Pool) QueueDepth

func (p *Pool) QueueDepth() int64

QueueDepth returns the current number of Submit callers blocked waiting for a slot.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the configured concurrency limit.

func (*Pool) Submit

func (p *Pool) Submit(task func()) error

Submit blocks until a slot is available, then starts task in a goroutine. Returns ErrClosed if the pool has been closed.

func (*Pool) SubmitCtx

func (p *Pool) SubmitCtx(ctx context.Context, task func()) error

SubmitCtx is Submit bounded by ctx. If ctx fires while waiting for a slot, ctx.Err() is returned and task is not started.

func (*Pool) TrySubmit

func (p *Pool) TrySubmit(task func()) bool

TrySubmit starts task only if a slot is immediately available. Returns true on success, false if the pool is full or closed.

func (*Pool) Wait

func (p *Pool) Wait()

Wait blocks until every submitted task has finished. Does NOT close the pool — further Submits are allowed afterwards.

Jump to

Keyboard shortcuts

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