Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: Track state of setNoDelay and prevent unnecessary system calls. #31543

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function initSocketHandle(self) {

const kBytesRead = Symbol('kBytesRead');
const kBytesWritten = Symbol('kBytesWritten');

const kSetNoDelay = Symbol('kSetNoDelay');

function Socket(options) {
if (!(this instanceof Socket)) return new Socket(options);
Expand All @@ -271,6 +271,7 @@ function Socket(options) {
this[kHandle] = null;
this._parent = null;
this._host = null;
this[kSetNoDelay] = false;
this[kLastWriteQueueSize] = 0;
this[kTimeout] = null;
this[kBuffer] = null;
Expand Down Expand Up @@ -488,8 +489,11 @@ Socket.prototype.setNoDelay = function(enable) {
}

// Backwards compatibility: assume true when `enable` is omitted
if (this._handle.setNoDelay)
this._handle.setNoDelay(enable === undefined ? true : !!enable);
const new_value = enable === undefined ? true : !!enable;
rustyconover marked this conversation as resolved.
Show resolved Hide resolved
if (this._handle.setNoDelay && new_value !== this[kSetNoDelay]) {
this[kSetNoDelay] = new_value;
this._handle.setNoDelay(new_value);
}

return this;
};
Expand Down
12 changes: 10 additions & 2 deletions test/parallel/test-net-socket-setnodelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ socket.setNoDelay();

socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(genSetNoDelay(true), truthyValues.length)
setNoDelay: common.mustCall(genSetNoDelay(true), 1)
}
});
truthyValues.forEach((testVal) => socket.setNoDelay(testVal));

socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(genSetNoDelay(false), falseyValues.length)
setNoDelay: common.mustNotCall(genSetNoDelay(false))
rustyconover marked this conversation as resolved.
Show resolved Hide resolved
}
});
falseyValues.forEach((testVal) => socket.setNoDelay(testVal));

socket = new net.Socket({
handle: {
setNoDelay: common.mustCall(() => {}, 3)
}
});
truthyValues.concat(falseyValues).concat(truthyValues)
.forEach((testVal) => socket.setNoDelay(testVal));

// If a handler doesn't have a setNoDelay function it shouldn't be called.
// In the case below, if it is called an exception will be thrown
socket = new net.Socket({
Expand Down