Aggiungi override probe TCP keepalive

This commit is contained in:
2026-05-06 12:58:12 +02:00
parent 6b19a8a606
commit cc4a3ed9c1
9 changed files with 154 additions and 3 deletions
+41
View File
@@ -0,0 +1,41 @@
"use strict";
let native = null;
try {
native = require("../../native/tcp_keepalive/build/Release/tcp_keepalive_native");
} catch (err) {
native = null;
}
function parseKeepAliveProbes(value) {
if (value === undefined || value === null || value === "") {
return 0;
}
value = Number(value);
if (!Number.isFinite(value) || value < 0) {
return 0;
}
return Math.floor(value);
}
function setKeepAliveProbes(socket, probes) {
if (!native || probes <= 0 || !socket || !socket._handle) {
return false;
}
const fd = socket._handle.fd;
if (!Number.isInteger(fd) || fd < 0) {
return false;
}
return native.setKeepAliveProbes(fd, probes) === true;
}
module.exports = {
available: !!native,
parseKeepAliveProbes,
setKeepAliveProbes
};
+6
View File
@@ -44,6 +44,10 @@
<label><i class="fa fa-clock-o"></i> <span>Keepalive</span></label>
<input type="text" id="node-input-keepalive" style="text-align:end; width:200px !important">
</div>
<div class="form-row">
<label><i class="fa fa-times-circle"></i> <span>Missed keepalives</span></label>
<input type="text" id="node-input-keepaliveProbes" style="text-align:end; width:120px !important">
</div>
<div id="node-row-newline" class="form-row hidden" style="padding-left:110px;">
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;">
@@ -72,6 +76,7 @@
datatype: { value: "buffer" },
newline: { value: "" },
keepalive: { value: "120000" },
keepaliveProbes: { value: "0" },
topic: { value: "" },
base64: {/*deprecated*/ value: false, required: true }
},
@@ -109,6 +114,7 @@
$("#node-input-datatype").change(updateOptions);
$("#node-input-datamode").change(updateOptions);
$("#node-input-keepalive").spinner({ min: 1 });
$("#node-input-keepaliveProbes").spinner({ min: 0 });
}
});
</script>
+12 -2
View File
@@ -19,6 +19,7 @@ module.exports = function(RED) {
var reconnectTime = RED.settings.socketReconnectTime||10000;
const msgQueueSize = RED.settings.tcpMsgQueueSize || 1000;
const Denque = require('denque');
const tcpKeepAlive = require('./tcp-keepalive');
var net = require('net');
var connectionPool = {};
@@ -45,6 +46,14 @@ module.exports = function(RED) {
*/
const dequeue = queue => queue.shift();
function setKeepAlive(socket, delay, probes, node) {
socket.setKeepAlive(true, delay);
if (probes > 0 && !tcpKeepAlive.setKeepAliveProbes(socket, probes) && !node.keepaliveProbesWarned) {
node.keepaliveProbesWarned = true;
node.warn("TCP keepalive probe count override is not available on this platform/runtime");
}
}
function TcpIn(n) {
RED.nodes.createNode(this,n);
this.host = n.host;
@@ -55,6 +64,7 @@ module.exports = function(RED) {
this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r");
this.base64 = n.base64;
this.keepalive = parseInt(n.keepalive) || 120000;
this.keepaliveProbes = tcpKeepAlive.parseKeepAliveProbes(n.keepaliveProbes);
this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server");
this.closing = false;
this.connected = false;
@@ -76,7 +86,7 @@ module.exports = function(RED) {
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
});
client.setKeepAlive(true, node.keepalive);
setKeepAlive(client, node.keepalive, node.keepaliveProbes, node);
connectionPool[id] = client;
client.on('data', function (data) {
@@ -151,7 +161,7 @@ module.exports = function(RED) {
}
else {
var server = net.createServer(function (socket) {
socket.setKeepAlive(true, node.keepalive);
setKeepAlive(socket, node.keepalive, node.keepaliveProbes, node);
var id = (1+Math.random()*4294967295).toString(16);
var fromi;
var fromp;