From d16af09e60cd9904a6908633c948913ef0101b92 Mon Sep 17 00:00:00 2001 From: Rusty Conover <rusty@conover.me> Date: Mon, 27 Jan 2020 22:09:23 -0500 Subject: [PATCH 1/3] net: Track state of setNoDelay and prevent unnecessary system calls. The state of .setNoDelay() is now tracked and code will prevent repeated system calls to setsockopt() when the value has already been set to the desired value for the socket. Change and expand the appropriate test. --- lib/net.js | 10 +++++++--- test/parallel/test-net-socket-setnodelay.js | 12 ++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/net.js b/lib/net.js index 11dd4bc900ef9b..bb3e4a5df3d167 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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); @@ -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; @@ -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; + if (this._handle.setNoDelay && new_value !== this[kSetNoDelay]) { + this[kSetNoDelay] = new_value; + this._handle.setNoDelay(new_value); + } return this; }; diff --git a/test/parallel/test-net-socket-setnodelay.js b/test/parallel/test-net-socket-setnodelay.js index 55b4c773fa5bf7..877505ee120b8e 100644 --- a/test/parallel/test-net-socket-setnodelay.js +++ b/test/parallel/test-net-socket-setnodelay.js @@ -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)) } }); 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({ From dc6c3152c1d43150929e64e21c2058aacf6c9213 Mon Sep 17 00:00:00 2001 From: Rusty Conover <rusty@conover.me> Date: Tue, 28 Jan 2020 08:33:42 -0500 Subject: [PATCH 2/3] Change to camelcase. --- lib/net.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/net.js b/lib/net.js index bb3e4a5df3d167..9ff6f12b9bc0e6 100644 --- a/lib/net.js +++ b/lib/net.js @@ -489,10 +489,10 @@ Socket.prototype.setNoDelay = function(enable) { } // Backwards compatibility: assume true when `enable` is omitted - const new_value = enable === undefined ? true : !!enable; - if (this._handle.setNoDelay && new_value !== this[kSetNoDelay]) { - this[kSetNoDelay] = new_value; - this._handle.setNoDelay(new_value); + const newValue = enable === undefined ? true : !!enable; + if (this._handle.setNoDelay && newValue !== this[kSetNoDelay]) { + this[kSetNoDelay] = newValue; + this._handle.setNoDelay(newValue); } return this; From 5dfc9ebecb0bb5d3870b69b520502d6e9dc2a30f Mon Sep 17 00:00:00 2001 From: Rusty Conover <rusty@luckydinosaur.com> Date: Thu, 30 Jan 2020 09:13:23 -0500 Subject: [PATCH 3/3] Update test/parallel/test-net-socket-setnodelay.js Co-Authored-By: Luigi Pinca <luigipinca@gmail.com> --- test/parallel/test-net-socket-setnodelay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-net-socket-setnodelay.js b/test/parallel/test-net-socket-setnodelay.js index 877505ee120b8e..e11d89daec58de 100644 --- a/test/parallel/test-net-socket-setnodelay.js +++ b/test/parallel/test-net-socket-setnodelay.js @@ -27,7 +27,7 @@ truthyValues.forEach((testVal) => socket.setNoDelay(testVal)); socket = new net.Socket({ handle: { - setNoDelay: common.mustNotCall(genSetNoDelay(false)) + setNoDelay: common.mustNotCall() } }); falseyValues.forEach((testVal) => socket.setNoDelay(testVal));