Skip to main content

Lock middlewares

Initial configuration​

To begin using the lock middlewares, you'll need to create and configure a LockFactory instance:

./samples/lock.ts
import { LockFactory } from "eridu-tech/lock";
import { MemoryLockAdapter } from "eridu-tech/lock/memory-lock-adapter";

export const lockFactory = new LockFactory({
adapter: new MemoryLockAdapter(),
});

withLockFactory middleware​

The Lock middleware wraps function calls with a distributed lock, ensuring mutual exclusion across processes. Before executing the wrapped function, a lock is acquired on a key derived from the function's arguments. If another process already holds the lock, the call waits (or fails immediately for non-blocking locks) until the lock is released.

Usage​

./samples/with-lock.ts
import { withLockFactory } from "eridu-tech/lock/middlewares";
import { use } from "eridu-tech/middleware";
import { lockFactory } from "./lock.js";

const withLock = withLockFactory(lockFactory);

const processJob = async (jobId: string): Promise<void> => {
// Critical section — only one process should execute this at a time
// ... process the job
};

// Wrap with distributed lock
const safeProcess = use(
processJob,
withLock({
key: ([jobId]) => `job:${jobId}`,
}),
);

await safeProcess("job-123"); // Acquires lock, processes, releases lock
info

Here is a complete list of settings for the withLock function.

Settings​

OptionTypeDescription
keyInvocable<TParameters, string>A function that produces the lock key from the wrapped function's arguments. The lock is acquired on this key, ensuring mutual exclusion across processes for the same key
lockIdInvocable<TParameters, string>Optional function that produces a unique identifier for the current lock acquisition attempt. Defaults to a UUID (v4)
ttlITimeSpan | nullTime-to-live for the lock. null means the lock never expires automatically; if omitted the factory's default TTL is used

Further information​

For further information refer to eridu-tech/lock API docs.