Skip to content

Commit

Permalink
net: fix net.Server keepalive and noDelay
Browse files Browse the repository at this point in the history
PR-URL: #43497
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
theanarkh authored Jun 22, 2022
1 parent b970634 commit dbe5874
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1659,12 +1659,11 @@ function onconnection(err, clientHandle) {
writable: true
});

if (self.noDelay && handle.setNoDelay) {
handle.setNoDelay(true);
if (self.noDelay && clientHandle.setNoDelay) {
clientHandle.setNoDelay(true);
}

if (self.keepAlive && self.setKeepAlive) {
handle.setKeepAlive(true, handle.keepAliveInitialDelay);
if (self.keepAlive && clientHandle.setKeepAlive) {
clientHandle.setKeepAlive(true, self.keepAliveInitialDelay);
}

self._connections++;
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-net-server-keepalive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

const server = net.createServer({
keepAlive: true,
keepAliveInitialDelay: 1000
}, common.mustCall((socket) => {
socket.destroy();
server.close();
})).listen(0, common.mustCall(() => {
net.connect(server.address().port);
}));

const onconnection = server._handle.onconnection;
server._handle.onconnection = common.mustCall((err, clientHandle) => {
const setKeepAlive = clientHandle.setKeepAlive;
clientHandle.setKeepAlive = common.mustCall((enable, initialDelayMsecs) => {
assert.strictEqual(enable, server.keepAlive);
assert.strictEqual(initialDelayMsecs, server.keepAliveInitialDelay);
setKeepAlive.call(clientHandle, enable, initialDelayMsecs);
});
onconnection.call(server._handle, err, clientHandle);
});
23 changes: 23 additions & 0 deletions test/parallel/test-net-server-nodelay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

const server = net.createServer({
noDelay: true
}, common.mustCall((socket) => {
socket.destroy();
server.close();
})).listen(0, common.mustCall(() => {
net.connect(server.address().port);
}));

const onconnection = server._handle.onconnection;
server._handle.onconnection = common.mustCall((err, clientHandle) => {
const setNoDelay = clientHandle.setNoDelay;
clientHandle.setNoDelay = common.mustCall((enable) => {
assert.strictEqual(enable, server.noDelay);
setNoDelay.call(clientHandle, enable);
});
onconnection.call(server._handle, err, clientHandle);
});

0 comments on commit dbe5874

Please sign in to comment.