npm install node-mutex
node-mutex ensures synchronicity of critical code blocks across multiple nodejs instances. This is achieved with the help of redis and some clever aproach to managing locks.
var mutex = require( 'node-mutex' )();
mutex.lock( 'key', function( err, unlock ) {
if ( err ) {
console.error( err );
console.error( 'Unable to acquire lock' );
}
//synchronized code block
unlock();
});var mutex = require( 'node-mutex' )();
mutex
.lock( 'key' )
.then( function( unlock ) {
//synchronized code block
unlock();
} );var mutex = require('node-mutex')(opts);List of available options:
host: host to connect redis on (127.0.0.1)port: port to connect redis on (6379)url: url to connect redis, this option replace values of host and port. Format:[redis:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]prefix: port to connect redis on (mutex:)sleepTime: maximum time in milliseconds to wait before retrying the acquisition of lock (250)expireTime: time in milliseconds before thelockexpires (3000)pub: optional, the redis client to publish events onsub: optional, the redis client to subscribe to events on
MIT