You're viewing archived documentation for @imqueue/rpc v1.17.1. APIs may differ from the current release. View the latest version ↗

IMQLock class

Class IMQLock. Implements promise-based locks.

Signature:

export declare class IMQLock 

Example

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

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

if (IMQLock.locked('doSomething')) { // avoiding err handling in this way can cause ded-locks // so it is good always try catch locked calls! // BTW, IMQLock uses timeouts to avoid dead-locks try

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 which appears 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 given key is locked, false otherwise

release(key, value, err)

static

Releases previously acquired lock for a given key