mathiask88 / node-snap7

node.js wrapper for snap7
MIT License
166 stars 59 forks source link

Network blocks with more than four simultaneous clients connections #63

Closed jla closed 4 years ago

jla commented 4 years ago

When connecting to multiple devices if some of them goes down it blocks all the communication with the other devices:

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

var LIMIT = 4

for (var i = 1; i < LIMIT + 1; i++) {
    console.log('Init Dummy: ' + i);
    var s7clientA = new snap7.S7Client();
    s7clientA.ConnectTo('192.168.0.10' + i, 0, 1, function(err) {   // Offline devices
        if(err) return console.log(' >> Connection failed. Code #' + err + ' - ' + s7clientA.ErrorText(err));
    });
}

console.log('Init Client');
var s7clientB = new snap7.S7Client();
s7clientB.ConnectTo('192.168.0.1', 0, 1, function(err) { // Online device
    if(err) return console.log(' >> Connection failed. Code #' + err + ' - ' + s7clientB.ErrorText(err));
    console.log('Client Connected');
});
Init Dummy: 1
Init Dummy: 2
Init Dummy: 3
Init Dummy: 4
Init Client

...after 30s...

 >> Connection failed. Code #60 -  TCP : Connection timed out
 >> Connection failed. Code #60 -  TCP : Connection timed out
 >> Connection failed. Code #60 -  TCP : Connection timed out
 >> Connection failed. Code #60 -  TCP : Connection timed out
Client Connected

With LIMIT = 3 it works as expected:

Init Dummy: 1
Init Dummy: 2
Init Dummy: 3
Init Client
Client Connected

...after 30s...

 >> Connection failed. Code #60 -  TCP : Connection timed out
 >> Connection failed. Code #60 -  TCP : Connection timed out
 >> Connection failed. Code #60 -  TCP : Connection timed out

I'm not sure if it's a problem in the Snap7 library. Any idea? Thanks!

jla commented 4 years ago

Found it! There is a limit in the number of concurrent asynchronous operations in node, by default is four. Each call to the snap7 library use one of the four available threads. This could be a problem with multiple devices and bad network connection.

Setting the UV_THREADPOOL_SIZE environment variable to a higher number (pe. the total number of devices) solve the problem.