Skip to content

rogermadjos/node-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-mutex

Build Status npm version

How to install

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.

How to use

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();
});

How to use ( Promise API )

var mutex = require( 'node-mutex' )();

mutex
  .lock( 'key' )
  .then( function( unlock ) {

    //synchronized code block

  	unlock();
  } );

Options

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 the lock expires (3000)
  • pub: optional, the redis client to publish events on
  • sub: optional, the redis client to subscribe to events on

License

MIT

About

Mutex for distributed nodejs application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •