How to lock on object which shared by multiple async method in nodejs?

Nilesh Wagh picture Nilesh Wagh · Aug 6, 2016 · Viewed 15.5k times · Source

I have one object with different properties in nodejs, there are different async function which access and modify that object with some complex execution. A single async function may have internal callbacks (or async functions), that may take some time to execute and then that function will modify that object. I want to lock that object until I'm done with all modification, only after that any other async function will access it.

Example:

var machineList = {};

function operation1() {
    waitForMachineList();
    //complex logic
    //modification of machineList, some closure and callbacks functions and again modification of machineList
    leaveFromMachineList();
}
function operation2() {
    waitForMachineList();
    //complex logic
    //modification of machineList, some closure and callbacks functions and again modification of machineList
    leaveFromMachineList();
}
function operation3() {
    waitForMachineList();
    //complex logic
    //modification of machineList, some closure and callbacks functions and again modification of machineList
    leaveFromMachineList();
}
function operation4() {
    waitForMachineList();
    //complex logic
    //modification of machineList, some closure and callbacks functions and again modification of machineList
    leaveFromMachineList();
}

Suppose machineList is one complex object, and there are different operations are done on this object by different async methods (operation1(), operation2(), ...) to modify it. Those operation are called in any sequence and any number of time as per request comes from client. Each request will execute single operation.

There are some internal closure functions and callbacks (or async functions) in each operation function, it may take some time. But I want to lock machineList object till any single operation is done.

On start of any operation, I want to lock object like waitForMachineList() and will release lock after leaveFromMachineList().

So finally I want to implement locking mechanism in nodejs. Just like Critical Session in C++ and lock in C#.

So please some will help to implement it in nodejs? or suggest me any node module which I can use for this.

Answer

Nilesh Wagh picture Nilesh Wagh · Aug 6, 2016

I have done Locking using async-lock node module. Now I can achieve the goal which is mention in question.

Example:

var AsyncLock = require('async-lock');
var lock = new AsyncLock();

function operation1() {
    console.log("Execute operation1");
    lock.acquire("key1", function(done) {
        console.log("lock1 enter")
        setTimeout(function() {
            console.log("lock1 Done")
            done();
        }, 3000)
    }, function(err, ret) {
        console.log("lock1 release")
    }, {});
}

function operation2() {
    console.log("Execute operation2");
    lock.acquire("key1", function(done) {
        console.log("lock2 enter")
        setTimeout(function() {
            console.log("lock2 Done")
            done();
        }, 1000)
    }, function(err, ret) {
        console.log("lock2 release")
    }, {});
}

function operation3() {
    console.log("Execute operation3");
    lock.acquire("key1", function(done) {
        console.log("lock3 enter")
        setTimeout(function() {
            console.log("lock3 Done")
            done();
        }, 1)
    }, function(err, ret) {
        console.log("lock3 release")
    }, {});
}operation1(); operation2(); operation3();

Output:

Execute operation1

lock1 enter

Execute operation2

Execute operation3

lock1 Done

lock1 release

lock2 enter

lock2 Done

lock2 release

lock3 enter

lock3 Done

lock3 release