Clever/mongo-lock-node

Name: mongo-lock-node

Owner: Clever

Description: Implements a distributed lock client backed by mongo

Created: 2017-03-03 19:44:00.0

Updated: 2018-01-09 02:17:06.0

Pushed: 2017-11-01 16:26:46.0

Homepage: null

Size: 16

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

mongo-lock-node

Distributed lock client backed by mongo.

Installation
pm install --save mongo-lock-node
Usage
rt * as MongoClient from "mongodb";
rt {RWMutex} from "mongo-lock-node";

onnect to mongo
t db = await MongoClient.connect("mongodb://localhost:27017/test");
t collection = db.collection("locks");
t collection.createIndex("lockID", { unique: true });

reate a client
t lockID = "lock_1"; // must be unique per lock
t clientID = "client_1"; // must be unique per client
t lock = new RWMutex(collection, lockID, clientID);

cquire the write lock
{
ait lock.lock();
tch (err) {
 other try/catch blocks omitted for brevity
nsole.error(err.message); // => "error acquiring lock lock_1: connection interrupted"
row err;

o other clients acquire a read or write lock while you have the write lock
ole.log("doing important things...");
elease the write lock
t lock.unlock();

cquire the read lock
t lock.rLock();
ther clients can also acquire a read lock, no clients can acquire the write lock
ole.log("reading important things...");
elease the read lock
t lock.rUnlock();
Gotchas

The current implementation is limited in a few ways. We may address these issues in the future but right now you should be aware of them before using this library:

  1. Re-enterable locks. RWMutex treats a clientID already existing on the lock in the db as a lock that this client owns. It re-enters the lock and proceeds as if you have the lock.
  2. No heartbeat. Our current requirements for this project do not include heartbeats, so any client that does not call unlock will remain on the lock forever.
  3. Manual setup. This library does not currently support setup. The collection you pass to the constructor must have a unique index on the lockID field.
Testing
 test
Building for local use
is will compile lib/ to javascript in the dist/ folder
 build

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.