Control access to shared resources with a semaphore.
Max count limits number of concurrent accesses allowed.
example
Safely using a semaphore by always releasing
// Ensure you import LockPowersimport { LockPowers } from"@andi/powers";
// acquire one of the 5 leases to my-shared-lockconst semaphore = await LockPowers.createSemaphore("my-shared-lock", 5);
try {
// may throw `LockAcquireTimeoutError`. To avoid errors, can also use `.tryAcquire()`await semaphore.acquire();
// critical code with at most 5 concurrent processes
} finally {
await semaphore.release();
}
Control access to shared resources with a semaphore.
Max count limits number of concurrent accesses allowed.
Safely using a semaphore by always releasing
// Ensure you import LockPowers import { LockPowers } from "@andi/powers"; // acquire one of the 5 leases to my-shared-lock const semaphore = await LockPowers.createSemaphore("my-shared-lock", 5); try { // may throw `LockAcquireTimeoutError`. To avoid errors, can also use `.tryAcquire()` await semaphore.acquire(); // critical code with at most 5 concurrent processes } finally { await semaphore.release(); }