AlgoMaster Logo

Design a Concurrency Limiter With a Semaphore

medium8 min readUpdated August 31, 2026
Listen to this chapter
Unlock Audio

Design a reusable concurrency limiter that allows at most a fixed number of callbacks to execute simultaneously.

Implement the ConcurrencyLimiter class:

  • ConcurrencyLimiter(maxConcurrent) creates a limiter containing maxConcurrent permits.
  • run(task) waits for one permit, executes task, and releases the permit when the task finishes.

At most maxConcurrent callbacks passed to the same limiter may be executing at once. A caller for which no permit is available must block until another callback releases one.

Permits must be reusable across any number of calls. The permit must also be released if the callback throws an unchecked exception or panics; the original failure should continue to the caller after cleanup.

Different ConcurrencyLimiter instances must have independent permit pools.

The judge creates all caller threads and supplies the callbacks. Standard concurrency, callback, and thread APIs are preloaded, so you do not need import, include, or using statements.

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Constraints

  • 1 <= maxConcurrent <= 32
  • At most 50000 calls to run are made per limiter.
  • A task may complete normally or fail with an unchecked exception or panic.
  • Tasks do not call run recursively on the same limiter.
  • Judge threads are not interrupted while waiting for a permit.

Starter Code

Understanding the Problem

A mutex permits one thread into a critical section. A counting semaphore generalizes that idea by maintaining a configurable number of permits.

Acquiring a permit decreases the available count. When no permits remain, further callers wait. Releasing a permit increases the count and allows one waiting caller to continue.

The semaphore limits only callbacks using the same instance. It does not prescribe their execution order, and it is not required to be fair unless the underlying primitive provides that guarantee.

Permit Ownership

Each successful acquire creates an obligation to release exactly one permit:

  • Releasing too early allows more than maxConcurrent callbacks to overlap.
  • Forgetting to release eventually blocks every later caller.
  • Releasing twice increases the pool beyond its configured capacity.

The callback can fail, so a release placed only after the normal callback return is insufficient. Use finally, defer, or equivalent exception-safe cleanup to pair every successful acquire with exactly one release.

Synchronization Strategy

Store one semaphore initialized with maxConcurrent permits.

For run(task):

  1. Acquire one permit.
  2. Enter an exception-safe cleanup scope.
  3. Invoke the callback.
  4. Release the permit in the cleanup path, whether the callback completed or failed.

The exception or panic is not swallowed. Cleanup runs first, and then the original failure continues to the caller.

Go uses a buffered channel as a counting semaphore. Sending a token acquires capacity, and receiving that token releases it.

Correctness

The semaphore begins with exactly maxConcurrent permits. A callback starts only after its caller removes one permit, so no more than maxConcurrent callbacks can be executing simultaneously.

Every acquired permit is released exactly once after its callback stops executing. Therefore, no permit is leaked or duplicated, and later calls can reuse the full configured capacity.

The acquire occurs before invoking the task and the release occurs afterward, so a waiting caller cannot enter using that permit until the previous callback has completely finished. Separate limiter instances own separate semaphores and cannot consume each other's capacity.

Code

Complexity Analysis

  • Constructor: O(maxConcurrent) space for Go's buffered channel and O(1) explicit space in the other languages.
  • run: O(1) synchronization work plus the callback's running time.

A call may wait when every permit is in use; that waiting time depends on contention.