Skip to main content
zerotal

Documentation


Documentation / zerotal / lock / LockManager

Class: LockManager

Defined in: packages/core/src/lock/LockManager.ts:152

High-level entry point for distributed locking, backed by a pluggable LockDriver (memory, SQLite, or Redis).

Offers three usage styles: try (fail fast), block (wait for the lock), and lock (a manual ManagedLock handle). Both try and block always release the lock, even when the callback throws. Normally resolved via the Lock facade rather than constructed directly.

Example

// Fail fast: throw immediately if the lock is busy.
await manager.try("invoice:123", 10, async () => {
  await processInvoice(123);
});

// Blocking: wait up to 30s for the lock to free.
await manager.block("invoice:123", 10, async () => {
  await processInvoice(123);
}, { timeout: 30 });

// Manual, for flows that span multiple steps.
const lock = manager.lock("invoice:123", 10);
if (await lock.acquire()) {
  try { await processInvoice(123); } finally { await lock.release(); }
}

Constructors

Constructor

new LockManager(_driver): LockManager

Defined in: packages/core/src/lock/LockManager.ts:154

Parameters

_driver

LockDriver

Storage backend that performs the atomic acquire/release operations.

Returns

LockManager

Acquiring

lock()

lock(key, ttlSeconds): ManagedLock

Defined in: packages/core/src/lock/LockManager.ts:164

Build a named ManagedLock handle for manual acquire/release flows. Does not acquire the lock.

Parameters

key

string

Logical lock name.

ttlSeconds

number

Time-to-live, in seconds, after which the lock auto-expires.

Returns

ManagedLock


try()

try<T>(key, ttlSeconds, callback): Promise<T>

Defined in: packages/core/src/lock/LockManager.ts:179

Try to acquire the lock exactly once, execute callback, then release. The lock is always released — even when callback throws.

Type Parameters

T

T

Parameters

key

string

Logical lock name.

ttlSeconds

number

Lock time-to-live in seconds.

callback

() => T | Promise<T>

Critical section to run while the lock is held.

Returns

Promise<T>

The value returned by callback.

Throws

Immediately, if the lock is already held.


block()

block<T>(key, ttlSeconds, callback, options?): Promise<T>

Defined in: packages/core/src/lock/LockManager.ts:204

Block until the lock can be acquired (up to options.timeout seconds, defaulting to ttlSeconds), then execute callback and release. The lock is always released — even when callback throws.

Type Parameters

T

T

Parameters

key

string

Logical lock name.

ttlSeconds

number

Lock time-to-live in seconds.

callback

() => T | Promise<T>

Critical section to run while the lock is held.

options?

BlockOptions = {}

Wait timeout and poll interval.

Returns

Promise<T>

The value returned by callback.

Throws

If the lock cannot be acquired before the timeout elapses.

Configuration

dispose()

dispose(): void

Defined in: packages/core/src/lock/LockManager.ts:225

Release any background resources held by the underlying driver (timers, DB connections). Called by LockProvider when the application stops.

Returns

void