Home > @imqueue/rpc > IMQLock

IMQLock class

Class IMQLock. Implements promise-based locks.

Signature:

export declare class IMQLock 

Example

~~~typescript import { IMQLock, AcquiredLock } from './index.js';

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

if (IMQLock.locked('doSomething')) { // skipping error handling this way can cause dead-locks, // so it is always good to wrap locked calls in try/catch! // BTW, IMQLock uses timeouts to avoid dead-locks try { // this code is called only once across multiple async calls, // so all promises will be resolved with the same value const res = Math.random(); IMQLock.release('doSomething', res); return res; }

catch (err) { // release acquired locks with error IMQLock.release('doSomething', null, err); throw err; } }

return lock; }

(async () => { for (let i = 0; i < 10; ++i) { // run doSomething() asynchronously 10 times doSomething().then((res) => console.log(res)); } })(); ~~~

Properties

Property

Modifiers

Type

Description

deadlockTimeout

static

number

Deadlock timeout in milliseconds

{number}

logger

static

ILogger

Logger used to log errors that appear during locked calls

{ILogger}

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.