go 35 lines · 1 tab

Bounded worker pool with backpressure

Leah Thompson Jan 2026
1 tab
package pool

import "context"

type Job func(ctx context.Context) error

type Pool struct {
  jobs chan Job
}

func New(workers, buffer int, ctx context.Context) *Pool {
  p := &Pool{jobs: make(chan Job, buffer)}
  for i := 0; i < workers; i++ {
    go func() {
      for {
        select {
        case <-ctx.Done():
          return
        case job := <-p.jobs:
          _ = job(ctx)
        }
      }
    }()
  }
  return p
}

func (p *Pool) Submit(ctx context.Context, job Job) bool {
  select {
  case <-ctx.Done():
    return false
  case p.jobs <- job:
    return true
  }
}
1 file · go Explain with highlit

I avoid unbounded goroutines when processing queues; they look fine in staging and then blow up under a burst. This worker pool keeps a fixed number of workers and a bounded channel for jobs, which creates backpressure by design. The Submit call respects ctx.Done() so callers can stop enqueueing quickly during shutdown. On the worker side, each job receives the same ctx, so cancellations propagate. The most important operational detail is sizing: the job buffer should match the acceptable in-memory backlog, and the worker count should reflect downstream capacity (DB connections, API rate limits). Once you add metrics for queue length and processing time, this becomes a predictable subsystem.


Related snips

Share this code

Here's the card — post it anywhere.

Bounded worker pool with backpressure — share card
Link copied