Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use EventEmitter#once
  • Loading branch information
Karl Böhlmark committed Nov 25, 2016
commit 516ba6f0f746ca0420147599d2f173f94a145b4d
4 changes: 2 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ function tickOnSocket(req, socket) {
if (req.timeout) {
const emitRequestTimeout = () => req.emit('timeout');
Copy link
Contributor

Choose a reason for hiding this comment

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

Not to derail this PR which is desperately needed, but isn't this section very similar to the existing ClientRequest.prototype.setTimeout method? Would just calling req.setTimeout(req.timeout) at this point achieve almost the same behavior?

Copy link
Author

Choose a reason for hiding this comment

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

I thought along the same lines when I found this bug. The code implementing this feature didn't look like I expected, but I just wanted to make a small, conceptually simple fix to get it merged as quickly as possible.

It think your suggestion would work. What does @evanlucas say?

Copy link
Contributor

Choose a reason for hiding this comment

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

I reused your unit test code and took a stab at the approach described above in #9831

The primary difference is that any timeout set applies from the time a socket is assigned to a request, i.e. it includes socket pre-connect timeouts. In existing code, if a timeout is set via ClientRequest.prototype.setTimeout, it is "reapplied" and starts counting only from the time the socket connects. Perhaps this would count as an API change.

In other HTTP libraries, the connect-timeout and the transaction-timeout are two separate parameters, e.g. in curl https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html and https://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html

socket.once('timeout', emitRequestTimeout);
req.on('response', (r) => {
r.on('end', () => {
req.once('response', (res) => {
res.once('end', () => {
socket.removeListener('timeout', emitRequestTimeout);
});
});
Expand Down