From d3b965995a45db35b16137f81564e125d105dabe Mon Sep 17 00:00:00 2001 From: theanarkh Date: Wed, 29 Jun 2022 17:58:35 +0800 Subject: [PATCH] net: fix net keepalive and noDelay 1. support setKeepAlive again 2. set keepalive and nodelay to socket which is created by server PR-URL: https://github.com/nodejs/node/pull/43561 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/net.js | 14 +++++++++++++- test/parallel/test-net-server-keepalive.js | 10 ++++++++++ test/parallel/test-net-server-nodelay.js | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index 6f6930aaf8d..e227914c874 100644 --- a/lib/net.js +++ b/lib/net.js @@ -554,7 +554,16 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs) { return this; } - if (this._handle.setKeepAlive && enable !== this[kSetKeepAlive]) { + if (!this._handle.setKeepAlive) { + return this; + } + + if (enable !== this[kSetKeepAlive] || + ( + enable && + this[kSetKeepAliveInitialDelay] !== initialDelay + ) + ) { this[kSetKeepAlive] = enable; this[kSetKeepAliveInitialDelay] = initialDelay; this._handle.setKeepAlive(enable, initialDelay); @@ -1660,9 +1669,12 @@ function onconnection(err, clientHandle) { }); if (self.noDelay && clientHandle.setNoDelay) { + socket[kSetNoDelay] = true; clientHandle.setNoDelay(true); } if (self.keepAlive && clientHandle.setKeepAlive) { + socket[kSetKeepAlive] = true; + socket[kSetKeepAliveInitialDelay] = self.keepAliveInitialDelay; clientHandle.setKeepAlive(true, self.keepAliveInitialDelay); } diff --git a/test/parallel/test-net-server-keepalive.js b/test/parallel/test-net-server-keepalive.js index 384787d22bb..6f3db6468f6 100644 --- a/test/parallel/test-net-server-keepalive.js +++ b/test/parallel/test-net-server-keepalive.js @@ -7,6 +7,15 @@ const server = net.createServer({ keepAlive: true, keepAliveInitialDelay: 1000 }, common.mustCall((socket) => { + const setKeepAlive = socket._handle.setKeepAlive; + socket._handle.setKeepAlive = common.mustCall((enable, initialDelay) => { + assert.strictEqual(enable, true); + assert.match(String(initialDelay), /^2|3$/); + return setKeepAlive.call(socket._handle, enable, initialDelay); + }, 2); + socket.setKeepAlive(true, 1000); + socket.setKeepAlive(true, 2000); + socket.setKeepAlive(true, 3000); socket.destroy(); server.close(); })).listen(0, common.mustCall(() => { @@ -20,6 +29,7 @@ server._handle.onconnection = common.mustCall((err, clientHandle) => { assert.strictEqual(enable, server.keepAlive); assert.strictEqual(initialDelayMsecs, server.keepAliveInitialDelay); setKeepAlive.call(clientHandle, enable, initialDelayMsecs); + clientHandle.setKeepAlive = setKeepAlive; }); onconnection.call(server._handle, err, clientHandle); }); diff --git a/test/parallel/test-net-server-nodelay.js b/test/parallel/test-net-server-nodelay.js index f5ee77f71c5..a7f11475abe 100644 --- a/test/parallel/test-net-server-nodelay.js +++ b/test/parallel/test-net-server-nodelay.js @@ -6,6 +6,8 @@ const net = require('net'); const server = net.createServer({ noDelay: true }, common.mustCall((socket) => { + socket._handle.setNoDelay = common.mustNotCall(); + socket.setNoDelay(true); socket.destroy(); server.close(); })).listen(0, common.mustCall(() => { @@ -18,6 +20,7 @@ server._handle.onconnection = common.mustCall((err, clientHandle) => { clientHandle.setNoDelay = common.mustCall((enable) => { assert.strictEqual(enable, server.noDelay); setNoDelay.call(clientHandle, enable); + clientHandle.setNoDelay = setNoDelay; }); onconnection.call(server._handle, err, clientHandle); });