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
  • Loading branch information
theanarkh committed Jun 24, 2022
1 parent 3507b3f commit 4bbf5ea
Show file tree
Hide file tree
Showing 3 changed files with 32 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
12 changes: 12 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,17 @@ const server = net.createServer({
keepAlive: true,
keepAliveInitialDelay: 1000
}, common.mustCall((socket) => {
const symbols = Object.getOwnPropertySymbols(socket);
for (let i = 0; i < symbols.length; i++) {
if (symbols[i].toString() === 'Symbol(kSetKeepAlive)') {
assert.strictEqual(socket[symbols[i]], server.keepAlive);
} else if (symbols[i].toString() === 'Symbol(kSetKeepAliveInitialDelay)') {
assert.strictEqual(socket[symbols[i]], server.keepAliveInitialDelay);
socket.setKeepAlive(true, 2000);
socket.setKeepAlive(true, 3000);
assert.strictEqual(socket[symbols[i]], 3000 / 1000);
}
}
socket.destroy();
server.close();
})).listen(0, common.mustCall(() => {
Expand All @@ -20,6 +31,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);
});
7 changes: 7 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,13 @@ const net = require('net');
const server = net.createServer({
noDelay: true
}, common.mustCall((socket) => {
const symbols = Object.getOwnPropertySymbols(socket);
for (let i = 0; i < symbols.length; i++) {
if (symbols[i].toString() === 'Symbol(kSetNoDelay)') {
assert.strictEqual(socket[symbols[i]], server.noDelay);
break;
}
}
socket.destroy();
server.close();
})).listen(0, common.mustCall(() => {
Expand Down

0 comments on commit 4bbf5ea

Please sign in to comment.