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, http: allow setting socket connection timeout for http request #8101

Closed
wants to merge 1 commit into from

Conversation

reneweb
Copy link
Contributor

@reneweb reneweb commented Aug 14, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

http, net

Description of change

This allows passing the socket connection timeout to http#request
such that it will be set before the socket is connecting

Fixes #7580

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. net Issues and PRs related to the net subsystem. labels Aug 14, 2016
if (args.socketTimeout) {
s.setTimeout(args.socketTimeout);
return Socket.prototype.connect.apply(s, args);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

The else branch is unnecessary. You're returning the same thing in both branches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yeah, will fix that

@jasnell
Copy link
Member

jasnell commented Aug 15, 2016

@nodejs/http

@jasnell
Copy link
Member

jasnell commented Aug 15, 2016

+1 to @cjihrig's comment. Once that is addressed this should be good.

@@ -133,10 +133,12 @@ function ClientRequest(options, cb) {
if (self.socketPath) {
self._last = true;
self.shouldKeepAlive = false;
const optionsPath = {
path: self.socketPath
const createConnectionOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change the name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It used to be optionsPath since the only option was the path. With this change we introduce non-path related options in there, which is why I felt renaming would make sense and I've chosen createConnectionOptions since we pass them to the createConnection function.

Copy link
Contributor

Choose a reason for hiding this comment

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

To me optionsPath is more general, like 'options for (unix) socket path' as opposed to 'options for TCP socket'. With that in mind, I'd prefer to see the name unchanged. If other @nodejs/collaborators disagree, at the very least I'd rather see a more succinct name like connectOptions or createOptions or similar.

Copy link
Member

Choose a reason for hiding this comment

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

I think a shorter name is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see what you mean @mscdex . I think it's ok to go back to optionsPath then.

@mcollina
Copy link
Member

mcollina commented Aug 16, 2016

Some nits (same as @mscdex), but LGTM for me.

@Trott
Copy link
Member

Trott commented Aug 16, 2016

Labeling in progress because it needs a test.

@Trott Trott added wip Issues and PRs that are still a work in progress. semver-minor PRs that contain new features and should be released in the next minor version. labels Aug 16, 2016
@mscdex
Copy link
Contributor

mscdex commented Aug 16, 2016

It also needs a documentation update

@reneweb reneweb force-pushed the http-request-socket-timeout branch from a4eafb8 to 1acb85f Compare August 21, 2016 19:46
@reneweb
Copy link
Contributor Author

reneweb commented Aug 21, 2016

I've changed everything as discussed. I also noticed that we wouldn't emit a timeout event. To make it consistent with setTimeout - which does emit a timeout event - and to make it easier to test, I added that functionality. I've also added a test and updated the docs.

@@ -134,7 +135,8 @@ function ClientRequest(options, cb) {
self._last = true;
self.shouldKeepAlive = false;
const optionsPath = {
path: self.socketPath
path: self.socketPath,
timeout: options.timeout
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nit, but to be consistent we could just reference self.timeout here instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it

@reneweb reneweb force-pushed the http-request-socket-timeout branch from 1acb85f to 16c6661 Compare August 21, 2016 22:33
};
if (req.timeout) {
socket.once('timeout', emitTimeout);
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not simply:

if (req.timeout)
  socket.once('timeout', () => req.emit('timeout'));

@jasnell
Copy link
Member

jasnell commented Aug 24, 2016

Few additional nits. Almost there!

@reneweb reneweb force-pushed the http-request-socket-timeout branch from 16c6661 to 00aa5d1 Compare August 28, 2016 01:10
@reneweb
Copy link
Contributor Author

reneweb commented Aug 28, 2016

I've fixed all the mentioned issues except for the req error handling one, which I commentent on. I've also applied the fixes to test-http-client-timeout-event, which I based my test off of, so it that is consistent.

@jasnell
Copy link
Member

jasnell commented Aug 29, 2016

LGTM if CI is green.

@santigimeno
Copy link
Member

Shouldn't the timeout option be documented in the net module too?

This allows passing the socket connection timeout to http#request
such that it will be set before the socket is connecting

Fixes nodejs#7580
@reneweb reneweb force-pushed the http-request-socket-timeout branch from 00aa5d1 to 4341e4d Compare August 31, 2016 00:01
@reneweb
Copy link
Contributor Author

reneweb commented Aug 31, 2016

True, I've added it now to the net documentation

@imyller
Copy link
Member

imyller commented Sep 24, 2016

@imyller
Copy link
Member

imyller commented Sep 24, 2016

Removing in-progress label, because test has been implemented.

@imyller imyller removed the wip Issues and PRs that are still a work in progress. label Sep 24, 2016
@imyller imyller self-assigned this Sep 24, 2016
@reneweb
Copy link
Contributor Author

reneweb commented Sep 24, 2016

From the implementation point of view it should be fine. The only thing is that I got no feedback on, yet, are the doc changes, specifically for the net docs. Unless there are any objection there, it should be good to go.

Copy link
Member

@imyller imyller left a comment

Choose a reason for hiding this comment

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

LGTM

@imyller
Copy link
Member

imyller commented Sep 24, 2016

I'm counting three LGTMs and no clear objections for landing this. CI tests have passed.

I've assigned this to myself for landing prep, but I'd like to hold until monday to give everyone time to comment.

@imyller
Copy link
Member

imyller commented Sep 26, 2016

I'll start landing this:

  • Three LGTMs
  • No objections
  • All requested changes have been made
  • CI tests passed (only known CI failures)

@imyller
Copy link
Member

imyller commented Sep 26, 2016

landed in c9b59e8

Thank you for your effort and contribution, @reneweb

@imyller imyller closed this Sep 26, 2016
@imyller imyller removed their assignment Sep 26, 2016
imyller pushed a commit that referenced this pull request Sep 26, 2016
This allows passing the socket connection timeout to http#request
such that it will be set before the socket is connecting

PR-URL: #8101
Fixes: #7580
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
jasnell pushed a commit that referenced this pull request Sep 29, 2016
This allows passing the socket connection timeout to http#request
such that it will be set before the socket is connecting

PR-URL: #8101
Fixes: #7580
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
geek pushed a commit to geek/node that referenced this pull request Sep 30, 2016
This allows passing the socket connection timeout to http#request
such that it will be set before the socket is connecting

PR-URL: nodejs#8101
Fixes: nodejs#7580
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
This allows passing the socket connection timeout to http#request
such that it will be set before the socket is connecting

PR-URL: #8101
Fixes: #7580
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Fishrock123 added a commit that referenced this pull request Oct 12, 2016
* fs:
  - `SyncWriteStream` now inherits from `Stream.Writable`. (Anna
Henningsen) #8830
    - Practically, this means that when stdio is piped to a file,
stdout and stderr will still be `Writable` streams.
  - `fs.existsSync()` has been undeprecated. `fs.exists()` remains
deprecated. (Dan Fabulich) #8364
* http: `http.request()` now accepts a `timeout` option. (Rene Weber)
#8101
* module: The module loader now maintains its own realpath cache. (Anna
Henningsen) #8100
* npm: Upgraded to 3.10.8 (Kat Marchán)
#8706
* stream: `Duplex` streams now show proper `instanceof
Stream.Writable`. (Anna Henningsen)
#8834
* timers: Improved `setTimeout`/`Interval` performance by up to 22%.
(Brian White) #8661

PR-URL: #9034
Fishrock123 added a commit that referenced this pull request Oct 12, 2016
* fs:
  - `SyncWriteStream` now inherits from `Stream.Writable`. (Anna
Henningsen) #8830
    - Practically, this means that when stdio is piped to a file,
stdout and stderr will still be `Writable` streams.
  - `fs.existsSync()` has been undeprecated. `fs.exists()` remains
deprecated. (Dan Fabulich) #8364
* http: `http.request()` now accepts a `timeout` option. (Rene Weber)
#8101
* module: The module loader now maintains its own realpath cache. (Anna
Henningsen) #8100
* npm: Upgraded to 3.10.8 (Kat Marchán)
#8706
* stream: `Duplex` streams now show proper `instanceof
Stream.Writable`. (Anna Henningsen)
#8834
* timers: Improved `setTimeout`/`Interval` performance by up to 22%.
(Brian White) #8661

PR-URL: #9034
imyller added a commit to imyller/meta-nodejs that referenced this pull request Oct 13, 2016
    * fs:
      - `SyncWriteStream` now inherits from `Stream.Writable`. (Anna
    Henningsen) nodejs/node#8830
        - Practically, this means that when stdio is piped to a file,
    stdout and stderr will still be `Writable` streams.
      - `fs.existsSync()` has been undeprecated. `fs.exists()` remains
    deprecated. (Dan Fabulich) nodejs/node#8364
    * http: `http.request()` now accepts a `timeout` option. (Rene Weber)
    nodejs/node#8101
    * module: The module loader now maintains its own realpath cache. (Anna
    Henningsen) nodejs/node#8100
    * npm: Upgraded to 3.10.8 (Kat Marchan)
    nodejs/node#8706
    * stream: `Duplex` streams now show proper `instanceof
    Stream.Writable`. (Anna Henningsen)
    nodejs/node#8834
    * timers: Improved `setTimeout`/`Interval` performance by up to 22%.
    (Brian White) nodejs/node#8661

Signed-off-by: Ilkka Myller <ilkka.myller@nodefield.com>
imyller added a commit to imyller/meta-nodejs that referenced this pull request Oct 13, 2016
    * fs:
      - `SyncWriteStream` now inherits from `Stream.Writable`. (Anna
    Henningsen) nodejs/node#8830
        - Practically, this means that when stdio is piped to a file,
    stdout and stderr will still be `Writable` streams.
      - `fs.existsSync()` has been undeprecated. `fs.exists()` remains
    deprecated. (Dan Fabulich) nodejs/node#8364
    * http: `http.request()` now accepts a `timeout` option. (Rene Weber)
    nodejs/node#8101
    * module: The module loader now maintains its own realpath cache. (Anna
    Henningsen) nodejs/node#8100
    * npm: Upgraded to 3.10.8 (Kat Marchan)
    nodejs/node#8706
    * stream: `Duplex` streams now show proper `instanceof
    Stream.Writable`. (Anna Henningsen)
    nodejs/node#8834
    * timers: Improved `setTimeout`/`Interval` performance by up to 22%.
    (Brian White) nodejs/node#8661

Signed-off-by: Ilkka Myller <ilkka.myller@nodefield.com>
@bnoordhuis
Copy link
Member

Can the people who authored/reviewed this comment on #12005? It's unclear if the code actually does what the documentation suggests it does.

@jasnell
Copy link
Member

jasnell commented Mar 24, 2017

Ping @indutny @mcollina

@mcollina
Copy link
Member

@bnoordhuis to the best of my knowledge, it does what is supposed to be doing. The doc is definitely not clear in what is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. net Issues and PRs related to the net subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
10 participants