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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Distributed lock client backed by mongo.
pm install --save mongo-lock-node
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();
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:
lockID
field. test
is will compile lib/ to javascript in the dist/ folder
build