ISemaphoreBase: {
    acquire(): Promise<boolean>;
    acquireOrFail(): Promise<void>;
    forceReleaseAll(): Promise<boolean>;
    refresh(ttl?: ITimeSpan): Promise<boolean>;
    refreshOrFail(ttl?: ITimeSpan): Promise<void>;
    release(): Promise<boolean>;
    releaseOrFail(): Promise<void>;
    runOrFail<TValue = void>(asyncFn: AsyncLazy<TValue>): Promise<TValue>;
}

Base operations for managing semaphore slot acquisition, release, and refresh cycles. Provides both safe (boolean-returning) and strict (error-throwing) versions of slot operations.

IMPORT_PATH: "@daiso-tech/core/semaphore/contracts"

Type declaration

  • acquire:function
    • Attempts to acquire a semaphore slot if the limit is not reached.

      Returns Promise<boolean>

      true if a slot was successfully acquired, false if the limit is reached

  • acquireOrFail:function
    • Acquires a semaphore slot if the limit is not reached. Throws an error if a slot cannot be acquired.

      Returns Promise<void>

      If all slots are already acquired

  • forceReleaseAll:function
    • Forces release of all slots for this semaphore key, regardless of ownership. Use with caution as it can break semaphore guarantees.

      Returns Promise<boolean>

      true if slots existed and were released, false if already empty

  • refresh:function
    • Refreshes (extends) the current slot's TTL if it is expirable. Updates the expiration time to prevent the slot from timing out.

      Parameters

      • Optionalttl: ITimeSpan

        New TTL duration. If not provided, uses the semaphore's original TTL

      Returns Promise<boolean>

      true if the slot was successfully refreshed, false if not currently held

  • refreshOrFail:function
    • Refreshes (extends) the current slot's TTL if it is expirable. Throws an error if the slot cannot be refreshed.

      Parameters

      • Optionalttl: ITimeSpan

        New TTL duration. If not provided, uses the semaphore's original TTL

      Returns Promise<void>

      If no slot is held or slot is not expirable

  • release:function
  • releaseOrFail:function
  • runOrFail:function
    • Executes an async function while holding a semaphore slot. Automatically acquires a slot before execution and releases it after completion. Throws an error if a slot cannot be acquired.

      Type Parameters

      • TValue = void

        The return type of the async function

      Parameters

      • asyncFn: AsyncLazy<TValue>

        The function to execute under semaphore protection

      Returns Promise<TValue>

      The return value of the function

      If all slots are already acquired