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();
}

Hierarchy

Index

Accessors

identifier

  • get identifier(): string

isAcquired

  • get isAcquired(): boolean

key

  • get key(): string

limit

  • get limit(): number
  • Returns number

Methods

acquire

  • acquire(): Promise<void>
  • Attempt to acquire lock. @throws LockAcquireTimeoutError if lock is not acquired within the configured lockTimeout

    Returns Promise<void>

release

  • release(): Promise<void>

tryAcquire

  • tryAcquire(): Promise<boolean>
  • Method to acquire lock. Will not throw an error if acquireTimeout occurs.

    Returns Promise<boolean>

Generated using TypeDoc