-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: set socket timeout when socket connects
`request.setTimeout()` calls `socket.setTimeout()` as soon as a socket is assigned to the request. This makes the `timeout` event to be emitted on the request even if the underlying socket never connects. This commit makes `socket.setTimeout()` to be called only when the underlying socket is connected. PR-URL: #8895 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer((req, res) => { | ||
// This space is intentionally left blank. | ||
}); | ||
|
||
server.listen(0, common.localhostIPv4, common.mustCall(() => { | ||
const port = server.address().port; | ||
const req = http.get(`http://${common.localhostIPv4}:${port}`); | ||
|
||
req.setTimeout(1); | ||
req.on('socket', common.mustCall((socket) => { | ||
assert.strictEqual(socket._idleTimeout, undefined); | ||
socket.on('connect', common.mustCall(() => { | ||
assert.strictEqual(socket._idleTimeout, 1); | ||
})); | ||
})); | ||
req.on('timeout', common.mustCall(() => req.abort())); | ||
req.on('error', common.mustCall((err) => { | ||
assert.strictEqual('socket hang up', err.message); | ||
server.close(); | ||
})); | ||
})); |