A semaphore implementation that limits the number of concurrent operations.
The semaphore maintains an internal queue of pending requests and a set of
active tokens. When the number of active tokens is below the configured
maximum, the next request in the queue is granted immediately. Each granted
request receives a release callback; calling this callback frees a token
and triggers the next pending request.
Internally the semaphore tracks active tokens in a Map and pending
requests in an array. The request method returns a Promise
that resolves to a release function. The release function removes the
token from the active set and potentially starts the next pending request.
The semaphore is thread‑safe with respect to the event loop, as all state
mutations occur synchronously within the call stack of the request
and release methods.
Param: max
The maximum number of concurrent operations allowed.
A semaphore implementation that limits the number of concurrent operations.
The semaphore maintains an internal queue of pending requests and a set of active tokens. When the number of active tokens is below the configured maximum, the next request in the queue is granted immediately. Each granted request receives a release callback; calling this callback frees a token and triggers the next pending request.
Internally the semaphore tracks active tokens in a Map and pending requests in an array. The request method returns a Promise that resolves to a release function. The release function removes the token from the active set and potentially starts the next pending request.
The semaphore is thread‑safe with respect to the event loop, as all state mutations occur synchronously within the call stack of the request and release methods.
Param: max
The maximum number of concurrent operations allowed.