Skip to content

Commit

Permalink
net: Track state of setKeepAlive and prevent unnecessary system calls
Browse files Browse the repository at this point in the history
The state of .setKeepAlive() 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.
  • Loading branch information
rustyconover committed Jan 28, 2020
1 parent d6f1e39 commit 4b2477c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 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 kSetKeepAlive = Symbol('kSetKeepAlive');

function Socket(options) {
if (!(this instanceof Socket)) return new Socket(options);
Expand All @@ -272,6 +272,7 @@ function Socket(options) {
this._parent = null;
this._host = null;
this[kLastWriteQueueSize] = 0;
this[kSetKeepAlive] = null;
this[kTimeout] = null;
this[kBuffer] = null;
this[kBufferCb] = null;
Expand Down Expand Up @@ -501,8 +502,15 @@ Socket.prototype.setKeepAlive = function(setting, msecs) {
return this;
}

if (this._handle.setKeepAlive)
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
const newValue = [setting, ~~(msecs / 1000)];
if (this._handle.setKeepAlive &&
(this[kSetKeepAlive] == null ||
this[kSetKeepAlive][0] !== newValue[0] ||
this[kSetKeepAlive][1] !== newValue[1])
) {
this[kSetKeepAlive] = newValue;
this._handle.setKeepAlive(...newValue);
}

return this;
};
Expand Down

0 comments on commit 4b2477c

Please sign in to comment.