Distributed lock implementation based on MongoDB for Node.js, version 2.0.4. It supports one-shot lock attempts and automatic retry via polling, lock refresh (including auto-refresh), and data payloads attached to locks. Uses MongoDB TTL indexes to clean up stray locks. The 2.0.0 major release removed pubsub-based event propagation via a capped collection and EventEmitter, simplifying the internals to rely solely on polling. Compared to alternatives like Redis-based locks, mongo-dlock leverages existing MongoDB infrastructure without additional services. It defaults to a 15-second lock expiration and a 5-second polling interval.
npm install mongo-dlockNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates initialization, acquiring a lock with automatic retry, and releasing with proper cleanup.
Update code to not depend on pubsub events. Polling is now the only mechanism.
Ensure unlock() is called to stop autorefresh and release the lock.
Use a function returning the update object if dynamic updates are needed (e.g., including a timestamp on each refresh).
Wrap calls with util.promisify or use async/await with a promise adapter.
Ensure MongoDB is running and provide the correct URL via opts.url or environment variable MONGO_URL.
No action needed; wait_lock retries automatically. If using try_lock, handle the error gracefully.
Use `const MDL = require('mongo-dlock')` for CommonJS, or `import MDL from 'mongo-dlock'` for ESM.Check the error parameter in the MDL callback before using Locks.