Skip to content

Class: MemoryMutexDriver ​

The memory driver ({type: 'memory'}, the default of a Mutex): the locks live in the process memory, shared by every component and driver of the process using the same prefix, and invisible to the other processes. Use it for development, tests and an application running as a single process. It needs no package.

The locks expire after their ttl unless renewed. The fencing token is a counter of the process, starting again at 1 when the process restarts. The locks are lost with the process.

Example ​

typescript
import {MemoryMutexDriver, type DriverLock} from 'lakutata/com/mutex'

const driver: MemoryMutexDriver = new MemoryMutexDriver('test:')
const first: DriverLock | undefined = await driver.acquire('job', 1000)
console.log(await driver.acquire('job', 1000)) //undefined: held by the first
if (first) console.log(await driver.release('job', first)) //true
await driver.close()

Extends ​

Constructors ​

Constructor ​

new MemoryMutexDriver(prefix?): MemoryMutexDriver

Create a driver on the process memory.

Parameters ​

prefix? ​

string

The prefix added to the lock names; the drivers of the process with the same prefix share their locks. '' by default.

Returns ​

MemoryMutexDriver

Overrides ​

MutexDriver.constructor

Properties ​

expiring ​

readonly expiring: boolean

true: the locks expire after their ttl unless renewed.

Overrides ​

MutexDriver.expiring


onLost ​

onLost: (names) => void

Called with the names of the locks the driver lost at once, when the connection holding advisory locks closes or fails. A Mutex sets it; it does nothing by default.

Parameters ​

names ​

string[]

Returns ​

void

Inherited from ​

MutexDriver.onLost

Methods ​

acquire() ​

acquire(name, ttl): Promise<DriverLock | undefined>

Try to acquire a lock once: granted when nobody holds it or its holder let it expire.

Parameters ​

name ​

string

The lock name.

ttl ​

number

How long the lock stays held without a renewal, in milliseconds.

Returns ​

Promise<DriverLock | undefined>

The lock with a fencing token, or undefined when another holder has it.

Overrides ​

MutexDriver.acquire


close() ​

close(): Promise<void>

Do nothing: the locks stay in the process memory until they are released or expire.

Returns ​

Promise<void>

Overrides ​

MutexDriver.close


extend() ​

extend(name, lock, ttl): Promise<boolean>

Renew a lock still held by this holder, for ttl milliseconds from now. A lock expired is not renewed (lost), even when no other holder took it yet.

Parameters ​

name ​

string

The lock name.

lock ​

DriverLock

The lock acquired.

ttl ​

number

How long the lock stays held without another renewal, in milliseconds.

Returns ​

Promise<boolean>

Whether it was renewed.

Overrides ​

MutexDriver.extend


release() ​

release(name, lock): Promise<boolean>

Release a lock still held by this holder.

Parameters ​

name ​

string

The lock name.

lock ​

DriverLock

The lock acquired.

Returns ​

Promise<boolean>

Whether it was released (false when another holder has taken it since).

Overrides ​

MutexDriver.release

Released under the MIT License.