Skip to content

Commit

Permalink
net: fix net keepalive and noDelay
Browse files Browse the repository at this point in the history
1. support setKeepAlive again
2. set keepalive and nodelay to socket which is created by server

PR-URL: #43561
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
theanarkh authored and targos committed Jul 12, 2022
1 parent 4bda6e0 commit ad1d054
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-net-server-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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);
});
3 changes: 3 additions & 0 deletions test/parallel/test-net-server-nodelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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);
});

0 comments on commit ad1d054

Please sign in to comment.