X Tutup
The Wayback Machine - https://web.archive.org/web/20220502052206/https://github.com/nodejs/node/pull/41310
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: add new options to net.Socket and net.Server #41310

Merged
merged 2 commits into from Feb 22, 2022

Conversation

Copy link
Contributor

@ShogunPanda ShogunPanda commented Dec 24, 2021

Hello!
This PR adds the new following options to net.Socket.connect:

  • noDelay (boolean): If set to true, calls uv_tcp_nodelay on the socket after connection. By default it is disabled.
  • keepAlive (boolean) and keepAliveDelay (number): If set to true calls uv_tcp_nodelay on the socket after connection. By default it is disabled.

This changes are needed as preliminary work on #34185 to avoid excessive JS to C++ bridging.
As I'm not a C++ expert, I encountered the following problems:

  1. Not sure how to test changes (even though test suite is passing so apparently nothing is broken)
  2. Error handling in TCPWrap::AfterConnect was tentative so some C++ expert should take a look. Moreover, what shall we do if uv_tcp_connect succeeds but any of uv_tcp_nodelay or uv_tcp_keepalive fails? Shall we close the socket or let user handle it? Currently I don't do anything.

Once this is initially reviewed, I will also add (either here or in a new PR) the analogous on inbound connections.

@nodejs-github-bot
Copy link

@nodejs-github-bot nodejs-github-bot commented Dec 24, 2021

Review requested:

@nodejs-github-bot nodejs-github-bot added c++ lib / src needs-ci labels Dec 24, 2021
@mcollina
Copy link

@mcollina mcollina commented Dec 24, 2021

How do we test those options when set via the other methods?

@addaleax
Copy link

@addaleax addaleax commented Dec 25, 2021

This changes are needed as preliminary work on #34185 to avoid excessive JS to C++ bridging.

I would want to avoid as much additional complexity as possible -- this adds quite a bit, and even adds new members to the TCPWrap class, even though they are only useful during connecting. I would personally prefer to not do this and instead call the existing C++ methods from JS.

2. Moreover, what shall we do if uv_tcp_connect succeeds but any of uv_tcp_nodelay or uv_tcp_keepalive fails? Shall we close the socket or let user handle it?

Close the socket, yes, but also let the user handle the error itself.

src/tcp_wrap.h Outdated
@@ -64,6 +65,10 @@ class TCPWrap : public ConnectionWrap<TCPWrap, uv_tcp_t> {
private:
typedef uv_tcp_t HandleType;

bool noDelay;
bool keepAlive;
unsigned int keepAliveDelay;
Copy link
Member

@addaleax addaleax Dec 25, 2021

Choose a reason for hiding this comment

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

If this PR adds new fields, then they should be using lower_snake_case_; as elsewhere in the code

Copy link
Contributor Author

@ShogunPanda ShogunPanda Dec 26, 2021

Choose a reason for hiding this comment

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

Sorry, I missed that. No worries, I'll fix once we agree on the final approach.

src/tcp_wrap.cc Outdated
@@ -325,14 +347,22 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
T addr;
int err = uv_ip_addr(*ip_address, &addr);

wrap->noDelay = args[3]->IsTrue();
wrap->keepAlive = args[4]->IsTrue();
Copy link
Member

@mcollina mcollina Dec 25, 2021

Choose a reason for hiding this comment

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

Can we attach the fields to the ConnectWrap instead?

In this way it will not increase the memory footprint for each connection.

Copy link
Contributor Author

@ShogunPanda ShogunPanda Dec 26, 2021

Choose a reason for hiding this comment

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

Sure, I can move them there so they are then discarded after connection.

@addaleax Any objection on using this approach? Or do you think calling JS methods will not impact performance too much?

Copy link
Member

@addaleax addaleax Dec 26, 2021

Choose a reason for hiding this comment

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

@ShogunPanda Well yeah, @mcollina’s suggestion is definitely something that would improve this PR 🙂

Or do you think calling JS methods will not impact performance too much?

I would still think that that’s absolutely preferable, and not expect significant performance overhead, because:

  1. These calls are performed once-per-connection, which is much less than e.g. the number of data events for typical sockets, so it will have comparably little impact, and
  2. Doing JS-only changes would still allow us to be just as fast in the common case (by calling uv_tcp_nodelay(handle, 0); in the connect handler, and only doing another call into C++ when the user explicitly requested that the value is not set)

Generally we try to stick to implementing as much logic in JS as possible, because it is the more accessible language of the two that Node.js is made out of 🙂

@ShogunPanda
Copy link
Author

@ShogunPanda ShogunPanda commented Dec 26, 2021

@addaleax About testing, I just realized i can add two more testing in the array sent to req_wrap->MakeCallback in order to test it from the JS side. Do you think this will work?

@addaleax
Copy link

@addaleax addaleax commented Dec 26, 2021

@addaleax About testing, I just realized i can add two more testing in the array sent to req_wrap->MakeCallback in order to test it from the JS side. Do you think this will work?

@ShogunPanda You mean, pass the values back to the callback in order to test that the values were passed along properly? I don’t think that’s necessary, and I don’t think we have other tests in that format. I think manual validation that the right syscalls are made (e.g. with strace) would be the only thing we can really do to test this here.

@ShogunPanda
Copy link
Author

@ShogunPanda ShogunPanda commented Dec 28, 2021

@ShogunPanda You mean, pass the values back to the callback in order to test that the values were passed along properly? I don’t think that’s necessary, and I don’t think we have other tests in that format. I think manual validation that the right syscalls are made (e.g. with strace) would be the only thing we can really do to test this here.

Ok, no problem at all. Will validate manually. I plan to resend this PR today for further evaluation.

@ShogunPanda ShogunPanda force-pushed the net-socket-options branch 2 times, most recently from fa2ae47 to 7c9b51e Compare Dec 28, 2021
@ShogunPanda ShogunPanda changed the title net: add new options to socket.connect net: add new options to net.Socket and net.Server Dec 28, 2021
@ShogunPanda
Copy link
Author

@ShogunPanda ShogunPanda commented Dec 28, 2021

@addaleax @mcollina I've rewritten the PR in JS only (as I found out JS-to-CPP calls are not introducing any significant overhead). I also added some tests.

PTAL, thanks!

lib/net.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

lgtm

@ShogunPanda
Copy link
Author

@ShogunPanda ShogunPanda commented Jan 28, 2022

Any update on this? Shall I do anything else on this or are we good to merge?

@nodejs-github-bot
Copy link

@nodejs-github-bot nodejs-github-bot commented Jan 28, 2022

@nodejs-github-bot
Copy link

@nodejs-github-bot nodejs-github-bot commented Feb 1, 2022

@mcollina mcollina added commit-queue and removed needs-ci labels Feb 21, 2022
sxa pushed a commit to sxa/node that referenced this issue Mar 7, 2022
PR-URL: nodejs#41310
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
sxa added a commit to sxa/node that referenced this issue Mar 8, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122
* deps: V8: cherry-pick 77d515484864 (Lu Yahan) nodejs#42067
* deps: V8: cherry-pick b66334313c8b (Lu Yahan) nodejs#42067

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064
sxa added a commit to sxa/node that referenced this issue Mar 8, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064
sxa added a commit to sxa/node that referenced this issue Mar 8, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064
@sxa sxa mentioned this pull request Mar 8, 2022
sxa added a commit to sxa/node that referenced this issue Mar 8, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064

PR-URL: nodejs#42254
sxa added a commit to sxa/node that referenced this issue Mar 8, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064

PR-URL: nodejs#42254
sxa added a commit to sxa/node that referenced this issue Mar 8, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064

PR-URL: nodejs#42254
aduh95 added a commit to aduh95/node that referenced this issue Mar 9, 2022
sxa added a commit to sxa/node that referenced this issue Mar 9, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064

PR-URL: nodejs#42254
BethGriggs pushed a commit that referenced this issue Mar 9, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) #42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) #41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) #35025
* doc: add release key for Bryan English (Bryan English) #42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) #42127
* deps: upgrade npm to 8.5.2 (npm team) #42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) #42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) #42064

PR-URL: #42254
aduh95 added a commit to aduh95/node that referenced this issue Mar 9, 2022
nodejs-github-bot pushed a commit that referenced this issue Mar 9, 2022
Refs: #41310

PR-URL: #42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
aduh95 added a commit to aduh95/node that referenced this issue Mar 9, 2022
Refs: nodejs#41310

PR-URL: nodejs#42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
@aduh95 aduh95 added the backport-open-v16.x label Mar 11, 2022
aduh95 pushed a commit to aduh95/node that referenced this issue Mar 11, 2022
PR-URL: nodejs#41310
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 added a commit to aduh95/node that referenced this issue Mar 11, 2022
Refs: nodejs#41310

PR-URL: nodejs#42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
aduh95 pushed a commit to aduh95/node that referenced this issue Mar 12, 2022
PR-URL: nodejs#41310
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 added a commit to aduh95/node that referenced this issue Mar 12, 2022
Refs: nodejs#41310

PR-URL: nodejs#42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
bengl pushed a commit that referenced this issue Mar 21, 2022
Refs: #41310

Backport-PR-URL: #42270
PR-URL: #42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
aduh95 pushed a commit to aduh95/node that referenced this issue Apr 10, 2022
PR-URL: nodejs#41310
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 added a commit to aduh95/node that referenced this issue Apr 10, 2022
Refs: nodejs#41310

PR-URL: nodejs#42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
danielleadams pushed a commit that referenced this issue Apr 11, 2022
PR-URL: #41310
Backport-PR-URL: #42298
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
danielleadams pushed a commit that referenced this issue Apr 11, 2022
Refs: #41310

PR-URL: #42268
Backport-PR-URL: #42298
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
xtx1130 pushed a commit to xtx1130/node that referenced this issue Apr 25, 2022
Notable changes:

* (SEMVER-MINOR) crypto: add KeyObject.prototype.equals method (Filip Skokan) nodejs#42093
* (SEMVER-MINOR) net: add new options to net.Socket and net.Server (Paolo Insogna) nodejs#41310
* (SEMVER-MINOR) src: allow preventing InitializeInspector in env (Shelley Vohr) nodejs#35025
* doc: add release key for Bryan English (Bryan English) nodejs#42102

Dependency Updates:

* deps: update nghttp2 to 1.47.0 (Yash Ladha) nodejs#42127
* deps: upgrade npm to 8.5.2 (npm team) nodejs#42122

New Collaborators:

* doc: add JakobJingleheimer to collaborators list (Jacob Smith) nodejs#42185
* doc: move bnoordhuis back to collaborators (Ben Noordhuis) nodejs#42064

PR-URL: nodejs#42254
xtx1130 pushed a commit to xtx1130/node that referenced this issue Apr 25, 2022
Refs: nodejs#41310

PR-URL: nodejs#42268
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-open-v16.x c++ commit-queue-squash lib / src semver-minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants
X Tutup