Quick Start¶
1. Start the server¶
The server listens on 127.0.0.1:6388 by default. See the dflockd docs for configuration options.
2. Install the client¶
3. Use withLock (recommended)¶
The simplest way to use a lock. Acquires, runs your callback, and releases automatically — even if the callback throws.
import { DistributedLock } from "dflockd-client";
const lock = new DistributedLock({ key: "my-resource" });
await lock.withLock(async () => {
// critical section — lock is held here
console.log("doing work...");
});
// lock is released
4. Manual acquire / release¶
For more control over the lock lifecycle:
import { DistributedLock } from "dflockd-client";
const lock = new DistributedLock({
key: "my-resource",
acquireTimeoutS: 10,
leaseTtlS: 20,
});
const ok = await lock.acquire();
if (!ok) {
console.error("could not acquire lock");
process.exit(1);
}
try {
// critical section — lock is held and auto-renewed
} finally {
await lock.release();
}
What happens under the hood¶
- The client opens a TCP connection to the dflockd server.
- It sends a lock request with the key and timeout.
- The server grants the lock immediately if it's free, or enqueues the client in FIFO order.
- Once acquired, the client automatically renews the lease in the background.
- On release, the connection is closed and the server grants the lock to the next FIFO waiter.