recall

← recall

semaphore pattern

A counter that limits how many things can do something at once. Acquire decrements, release increments, blocks at zero. The simplest concurrency cap when you don't need a full worker pool.

A counter that limits how many things can do something at once. Acquire decrements, release increments, blocks at zero. The simplest concurrency cap when you don't need a full worker pool.

symptoms

  • unbounded concurrency overwhelming a downstream
  • need a parallelism cap without a full work queue

causes

  • uncontrolled goroutine / promise / thread spawning

fixes

  • wrap downstream calls in semaphore.acquire / release
  • select a cap based on downstream's known limits

you might say

  • throttle with a semaphore
  • cap parallelism

related

topics: concurrency, locks

references: