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 setKeepAlive and prevent unnecessary system calls #31551

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
17 changes: 16 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ function initSocketHandle(self) {
const kBytesRead = Symbol('kBytesRead');
const kBytesWritten = Symbol('kBytesWritten');
const kSetNoDelay = Symbol('kSetNoDelay');
const kSetKeepAlive = Symbol('kSetKeepAlive');

function Socket(options) {
if (!(this instanceof Socket)) return new Socket(options);
Expand All @@ -273,6 +274,7 @@ function Socket(options) {
this._host = null;
this[kSetNoDelay] = false;
this[kLastWriteQueueSize] = 0;
this[kSetKeepAlive] = null;
this[kTimeout] = null;
this[kBuffer] = null;
this[kBufferCb] = null;
Expand Down Expand Up @@ -500,13 +502,26 @@ Socket.prototype.setNoDelay = function(enable) {


Socket.prototype.setKeepAlive = function(setting, msecs) {
if (setting != null && typeof setting !== 'boolean')
throw new ERR_INVALID_ARG_TYPE('enable', 'boolean', setting);

if (msecs != null && typeof msecs !== 'number')
throw new ERR_INVALID_ARG_TYPE('msecs', 'number', msecs);

if (msecs === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is correct?

return;
}

if (!this._handle) {
this.once('connect', () => this.setKeepAlive(setting, msecs));
return this;
}

if (this._handle.setKeepAlive)
if (this._handle.setKeepAlive &&
this[kSetKeepAlive] !== setting) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this need to include the msec setting as well?

this._handle.setKeepAlive(setting, ~~(msecs / 1000));
this[kSetKeepAlive] = setting;
}
rustyconover marked this conversation as resolved.
Show resolved Hide resolved

return this;
};
Expand Down