Skip to main content

Lock 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

import { withLockFactory } from "@daiso-tech/core/lock/middlewares";
import { LockFactory } from "@daiso-tech/core/lock";
import { MemoryLockAdapter } from "@daiso-tech/core/lock/memory-lock-adapter";

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

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

// 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.

Further information

For further information refer to @daiso-tech/core/lock API docs.