Readonly[toThe well-known symbol Symbol.toStringTag used by Object.prototype.toString().
Returns 'Semaphore' so that Object.prototype.toString.call(sem)
produces '[object Semaphore]'.
ReadonlycapacityThe maximum number of permits (concurrency limit), set at construction.
Acquires a permit, waiting if necessary until one is available.
Important: Always release the permit in a finally block to avoid
leaking permits on exceptions. Prefer withPermit for automatic
release.
A promise that resolves to a SemaphorePermit.
Returns the number of permits currently available (not acquired).
Note: this is a snapshot and may change immediately after the call as other async operations acquire/release permits.
Custom toString implementation showing available/capacity.
Attempts to acquire a permit without waiting.
Some(permit) if a permit was available, None if the limit
has been reached.
Acquires a permit, executing the callback with at most capacity
concurrent callers. The permit is automatically released when the
callback settles (success or rejection).
This is the recommended way to use the semaphore as it avoids leaking permits on exceptions.
The return type of the callback.
A promise that resolves to the callback's return value.
A counting semaphore for limiting async concurrency.
Allows up to
capacityconcurrent operations. Eachacquire()consumes one permit; eachrelease()returns one. When the limit is reached,acquire()waits for a permit to be released. Waiters are served in FIFO order.Unlike
Mutex<T>,Semaphoredoes not protect a value — it is a pure concurrency counter. For exclusive access to a value, useMutex<T>.Since
1.10.0
See
https://docs.rs/tokio/latest/tokio/sync/struct.Semaphore.html
Example
Example