mongo-distributed-locks
Node.js distributed locking based on Mongodb.
Commands
# Add to project $ npm i mongo-distributed-locks
Usage
const DLocks = ; { let user = await User; userbalance -= fee; return user;} { return DLocks;} { let userId = 'userId'; // current user balance is 60 return Promiseall ; // current user balance is 25} { let userId = 'userId'; let lock = await DLocks; // current user balance is 60 try await ; await ; // current user balance is 25 finally await DLocks; }
API
-
static registerLoggerErrorFn(loggerErrorFn)
Registers logger error function (DLocks
can log a few error messages), if not providedconsole.error
is used`.loggerErrorFn
- logger error function. Example of usage:DLocks.registerLoggerErrorFn(logger.error.bind(logger));
-
static exec({ resource, id, fn })
Creates aDLocks
instance and executes itsexec
method. Shortcut forlet instance = new DLocks(params); instance.exec();
.resource
- operation: createPayment, or resource: user name.id
- id of the locking resource.fn
- function that does some operation on the locking resource.
-
static lock({ resource, id, fn })
Locks resource and returnslock
object. This method can be helpful when you need to manage lock manually. Accepts the same parameters as staticexec
. -
unlock(lock)
Unlocks the previously locked resource.lock
- previously generatedlock
object.
Author
Alexander Mac
License
Licensed under the MIT license.