IMQLock class

In-process, promise-based locks used to collapse concurrent identical calls: the first caller executes the work while later callers for the same key wait and are then resolved with the first caller's result.

Signature:

export declare class IMQLock 

Remarks

These are not distributed locks. The lock table is a set of plain static objects held in memory, and nothing here touches Redis, the network or any shared store. Separate processes, cluster workers and service replicas each maintain their own independent locks and will run the guarded code concurrently. lock() inherits the same limitation. Use a Redis- or database-backed lock if you need mutual exclusion across processes.

Keys are used verbatim, with no prefixing or namespacing, so they are global to the process and unrelated call sites sharing a string share a lock.

Example

import { IMQLock, type AcquiredLock } from '@imqueue/rpc';

async function doSomething(): Promise<number | AcquiredLock<number>> {
    const lock: AcquiredLock<number> =
        await IMQLock.acquire<number>('doSomething');

    // locked() is the only reliable way to tell holder from waiter
    if (IMQLock.locked('doSomething')) {
        // always wrap locked work in try/catch and release on both paths,
        // otherwise waiters hang until the deadlock timeout fires
        try {
            // runs only once across all concurrent calls; every waiter
            // resolves with this same value
            const res = Math.random();

            IMQLock.release('doSomething', res);

            return res;
        } catch (err) {
            // reject every waiter with the same error
            IMQLock.release('doSomething', null, err);
            throw err;
        }
    }

    return lock;
}

for (let i = 0; i < 10; ++i) {
    doSomething().then(res => console.log(res));
}

Properties

Property

Modifiers

Type

Description

deadlockTimeout

static

number

Deadlock timeout in milliseconds

logger

static

ILogger

Logger used to log errors that appear during locked calls

Methods

Method

Modifiers

Description

acquire(key, callback, metadata)

static

Acquires a lock for a given key.

locked(key)

static

Returns true if the given key is locked, false otherwise.

release(key, value, err)

static

Releases a previously acquired lock for a given key.

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.