Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 637
Nilesh Wagh Avatar asked Aug 06 '16 09:08

Nilesh Wagh


People also ask

How do I stop async in node JS?

You can't stop a request once it is sent, the sever will process your request anyway. What you can do is simply ignore the response. Can we see the ACTUAL asynchronous operation you're trying to stop. This and your other question today both sound like you don't understand the event-driven model of Javascript/node.

What is locking in node JS?

A lock is a mechanism for making sure that only one of many concurrently running functions can access a resource at a given time. In Node. js, the most common use case for locking is ensuring that two request handlers don't conflict in their interactions with the database.

Does Nodejs need lock?

Why do you need locking on single threaded nodejs? Nodejs is single threaded, and the code execution never gets interrupted inside an event loop, so locking is unnecessary? This is true ONLY IF your critical section can be executed inside a single event loop.


1 Answers

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

like image 182
Nilesh Wagh Avatar answered Oct 13 '22 09:10

Nilesh Wagh