Interface: DistributedSemaphore
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 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();
}
Extends
Accessors
identifier
Get Signature
get identifier(): string;
Internal
identifier helpful for debugging
Returns
string
Inherited from
DistributedMutex.identifier
isAcquired
Get Signature
get isAcquired(): boolean;
Returns
boolean
Inherited from
DistributedMutex.isAcquired
key
Get Signature
get key(): string;
Returns
string
Inherited from
DistributedMutex.key
limit
Get Signature
get limit(): number;
Returns
number
Methods
Methods
acquire()
acquire(): Promise<void>;
Attempt to acquire lock.
Returns
Promise<void>
Throws
LockAcquireTimeoutError if lock is not acquired within the configured lockTimeout
Inherited from
DistributedMutex.acquire
release()
release(): Promise<void>;
Release lock.
Returns
Promise<void>
Inherited from
DistributedMutex.release
tryAcquire()
tryAcquire(): Promise<boolean>;
Method to acquire lock. Will not throw an error if acquireTimeout occurs.
Returns
Promise<boolean>
Inherited from
DistributedMutex.tryAcquire