GithubHelp home page GithubHelp logo

hextar / mutex-node Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ttiny/mutex-node

0.0 1.0 0.0 340 KB

Cross-process named mutex for Node.js using file locks (and not redis!).

JavaScript 98.57% Shell 1.43%

mutex-node's Introduction

Mutex

Cross-process named mutex for Node.js using file locks (and not redis!).

npm install https://github.com/Perennials/mutex-node

This is port from https://github.com/Perennials/providerkit-core-php/blob/master/src/sync/Mutex.php.

Example

var Mutex = require( 'Mutex' );

var mutex = new Mutex( 'should_happen_one_at_a_time' );
mutex.lock();
// .. do something and rest assured this is the only process doing it
mutex.unlock();

API

Constructor

Constructs a mutex.

  • It starts in unlocked state.
  • The name you provide goes as a file name with extension .mutex in the system temporary directory, so don't get too fancy with the name.
  • Consider this will only work if the processes that are to mutually exclude each other use the same temp dir.
  • If .unlock() is not properly called the lock file will remain in the temp directory, which will not affect future locking, but just leave trash behind.
new Mutex(
	name:String
);

.lock()

Acquires the lock without waiting. Meaning if the lock is busy the function will fail immediately.

  • If the function works in sync or async mode is determined by the presence of a callback argument.
  • In sync mode the function will throw on errors.
  • The lock can not outlive the process life time, even if it is not released.
.lock(
	callback:function( err:Error|null )|undefined
) : Boolean;

.waitLock()

Acquires the lock waiting for it to become available if necessary. This waiting does not relate to the asyncness of the function.

  • If the function works in sync or async mode is determined by the presence of a callback argument.
  • In sync mode the function may possibly block for very long time until the lock becomes available.
.waitLock(
	callback:function( err:Error|null )|undefined
) : Boolean;

.tryLock()

Acquires the lock without waiting and without throwing.

  • If the function works in sync or async mode is determined by the presence of a callback argument.
  • In sync mode the function will not throw but just return false.
.tryLock(
	callback:function( err:Error|null )|undefined
) : Boolean;

.unlock()

Releases the lock.

  • If the function works in sync or async mode is determined by the presence of a callback argument.
  • If this function is not properly called the lock file will remain in the temp directory, which will not affect future locking, but just leave trash behind.
.unlock(
	callback:function( err:Error|null )|undefined
) : Boolean;

.isLocked()

Checks if the mutex is locked.

.isLocked() : Boolean;

Authors

Borislav Peev (borislav.asdf at gmail dot com)

mutex-node's People

Contributors

bobef avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.