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

doc: emphasize "timeout" and "request.setTimeout()" difference #52576

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kettanaito
Copy link

This change brings emphasis on the difference between these two ways of setting a socket timeout on an HTTP request:

http.request(url, { timeout: n })
http.request(url).setTimeout(n)

The two are completely different, and the current state of the docs failed to highlight that difference when I was debugging an issue.

  • The timeout option on http.request() is forwarded to the http.Agent and applies timeout immediately when the socket is created.
  • The request.setTimeout() method is not forwarded to the http.Agent, and applies timeout after the socket has connected.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem. labels Apr 18, 2024
@@ -176,7 +176,7 @@ changes:
while the `'lifo'` scheduling will keep it as low as possible.
**Default:** `'lifo'`.
* `timeout` {number} Socket timeout in milliseconds.
This will set the timeout when the socket is created.
This will immediately set the timeout when the socket is created.
Copy link
Author

Choose a reason for hiding this comment

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

immediately is a really important adverb here.

doc/api/http.md Outdated
Calls [`socket.setTimeout()`][] on the socket assigned to this request
after that socket emits the `connect` event.

Unlike the `timeout` option on `http.Agent` and `http.request()`, the
Copy link
Author

Choose a reason for hiding this comment

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

Clearly describe the difference.

@@ -3769,8 +3773,9 @@ changes:
request.
* `socketPath` {string} Unix domain socket. Cannot be used if one of `host`
or `port` is specified, as those specify a TCP Socket.
* `timeout` {number}: A number specifying the socket timeout in milliseconds.
This will set the timeout before the socket is connected.
* `timeout` {number}: Socket timeout in milliseconds.
Copy link
Author

Choose a reason for hiding this comment

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

Make the timeout option description consistent with the same option in http.Agent() constructor docs on the same page.

* `timeout` {number}: A number specifying the socket timeout in milliseconds.
This will set the timeout before the socket is connected.
* `timeout` {number}: Socket timeout in milliseconds.
This is forwarded to the [`http.Agent`][], which will set the timeout
Copy link
Author

Choose a reason for hiding this comment

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

Explicitly mention this option is forwarded to http.Agent as-is. It's important to know.

Copy link
Member

Choose a reason for hiding this comment

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

The request might not use an Agent at all.

Copy link
Author

Choose a reason for hiding this comment

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

That's a good point. Wouldn't it use the defaultAgent in that case?

I believe the agent is always present, even if not provided explicitly. Correct me if I'm wrong (something has to handle the sockets!)

Copy link
Member

@lpinca lpinca Apr 18, 2024

Choose a reason for hiding this comment

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

No agent is used when the createConnection option is specified or when the agent option is set to false.

Copy link
Author

Choose a reason for hiding this comment

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

The agent: false is tricky. I think that does mean to use a default agent. Setting agent: null, afaik, is for explicitly opting out from any agent.

But you are right, we shouldn't rely on the http.Agent always being present. Any objection if I rephrase it with "if present"?

I also wonder, what will happen with the timeout if the agent is not present?

Copy link
Member

@lpinca lpinca Apr 18, 2024

Choose a reason for hiding this comment

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

Any objection if I rephrase it with "if present"?

No objections, but I think the original wording is more general/correct.

I also wonder, what will happen with the timeout if the agent is not present?

The options are passed to net.createConnection() and the timeout option will work as expected. See https://nodejs.org/api/net.html#netcreateconnectionoptions-connectlistener and

node/lib/net.js

Lines 233 to 235 in 3790d52

if (options.timeout) {
socket.setTimeout(options.timeout);
}

Copy link
Author

Choose a reason for hiding this comment

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

I reverted the original description but kept the first line consistent between http.Agent and http.request.

@kettanaito kettanaito changed the title doc: emphasize "timeout" and "setTimeout" difference doc: emphasize "timeout" and "request.setTimeout()" difference Apr 18, 2024
* `timeout` {number}: A number specifying the socket timeout in milliseconds.
This will set the timeout before the socket is connected.
* `timeout` {number}: Socket timeout in milliseconds.
This is forwarded to the [`http.Agent`][], which will set the timeout
Copy link
Member

Choose a reason for hiding this comment

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

The request might not use an Agent at all.

Co-authored-by: Luigi Pinca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants