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.
Input:
Output:
Input:
Output:
1 <= maxConcurrent <= 3250000 calls to run are made per limiter.run recursively on the same limiter.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.
Each successful acquire creates an obligation to release exactly one permit:
maxConcurrent callbacks to overlap.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.
Store one semaphore initialized with maxConcurrent permits.
For run(task):
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.
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.
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.