Giter VIP home page Giter VIP logo

node-mutex's Introduction

Warning

This lib was written for big full-stack JS project tactoom.com in 2011 and still works successfully on production.

But I want to warn you, this lib is very old and was highly tested only for node 0.4.x (and with other old shit). It is also proven by using only in one project, so there's no any guarantees at all.

I do not suggest you to use it for something vital.

Mutex.lock(key, lifetime, callback) / Mutex.free(key, callback)

// Initialize mutex object
var Mutex = require('mutex');
var mutex = new Mutex();

// Acquire 'foo' for 10 secods (maximum)
mutex.lock('foo', 10000, function(err, locked, ttl){
    if (locked) {
        // do something with 'foo' ...
        
        // free 'foo'
        mutex.free('foo');
    }
    else {
        // 'foo' is locked at the moment
    }
})

Mutex.isolate(key, lifetime, fn, callback)

// Acquire 'foo' for 10 secods (maximum)
mutex.isolate('foo', 10000, function isolated(callback){
    
    // do something with 'foo' ...
    
    // 'foo' will be freed atomatically
    callback(null, 'some result');
    
}, function after(err, result) {
    
    // failed to acquire 'foo'
    if (result === false) {
        
    }
    // 'foo' was acquired and processed
    else {
        console.log(result); // 'some result'
    }
    
})

Mutex.isolateRetry(key, lifetime, fn, callback)

Same as isolate() but it will retry to lock again and again until it will be freed.

// Acquire 'foo' for 10 secods (maximum)
mutex.isolateRetry('foo', 10000, function isolated(callback){
    
    // do something with 'foo' ...
    
    // 'foo' will be freed atomatically
    callback(null, 'some result');
    
}, function after(err, result) {
    
    console.log(result); // 'some result'
})

Mutex.isolateCondRetry(key, lifetime, checkFn, fn, callback)

Conditional isolation. If checkFn() returns 'mutex.continue' it will isolate, else it will not lock.

var a = true; // or 'false'

// Acquire 'foo' for 10 secods (maximum)
mutex.isolateCondRetry('foo', 10000, function check(callback) {
    
    // If some condition
    if (a) {
        // We need to isolate
        callback(null, mutex.continue);
    }
    else {
        callback(null, 'cached')
    }
    
}, function isolated(callback){
    
    // do something with 'foo' ...
    
    // 'foo' will be freed atomatically
    callback(null, 'some result');
    
}, function after(err, result) {
    
    // if a == true --> 'some result'
    // if a == false --> 'cached'
    console.log(result); 
})

Installation

$ npm install mutex

node-mutex's People

Contributors

gockxml avatar mchotin avatar

Watchers

 avatar  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.