forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: set default timeout in agent keepSocketAlive
Previous location of setting the timeout would override behaviour of custom HttpAgents' keepSocketAlive. Moving it into the default keepSocketAlive allows it to interoperate with custom agents. Fixes: nodejs#33111 PR-URL: nodejs#33127 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
- Loading branch information
Showing
9 changed files
with
169 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
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
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
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(common.mustCall((req, res) => { | ||
server.close(); | ||
// do nothing, wait client socket timeout and close | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const agent = new http.Agent({ timeout: 100 }); | ||
const req = http.get({ | ||
path: '/', | ||
port: server.address().port, | ||
timeout: 50, | ||
agent | ||
}, common.mustNotCall()) | ||
.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.message, 'socket hang up'); | ||
assert.strictEqual(err.code, 'ECONNRESET'); | ||
})); | ||
req.on('timeout', common.mustCall(() => { | ||
req.abort(); | ||
})); | ||
})); |
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,29 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
server.close(); | ||
// do nothing, wait client socket timeout and close | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
let req; | ||
const timer = setTimeout(() => { | ||
req.abort(); | ||
assert.fail('should not timeout here'); | ||
}, 100); | ||
|
||
const agent = new http.Agent({ timeout: 50 }); | ||
req = http.get({ | ||
path: '/', | ||
port: server.address().port, | ||
agent | ||
}, common.mustNotCall()) | ||
.on('error', common.mustCall((err) => { | ||
clearTimeout(timer); | ||
assert.strictEqual(err.message, 'Socket timeout'); | ||
assert.strictEqual(err.code, 'ERR_HTTP_SOCKET_TIMEOUT'); | ||
})); | ||
})); |
52 changes: 52 additions & 0 deletions
52
test/sequential/test-http-request-on-free-socket-timeout.js
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,52 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer(common.mustCallAtLeast((req, res) => { | ||
const content = 'Hello Agent'; | ||
res.writeHead(200, { | ||
'Content-Length': content.length.toString(), | ||
}); | ||
res.write(content); | ||
res.end(); | ||
}, 2)); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const agent = new http.Agent({ timeout: 100, keepAlive: true }); | ||
const req = http.get({ | ||
path: '/', | ||
port: server.address().port, | ||
agent | ||
}, common.mustCall((res) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
res.resume(); | ||
res.on('end', common.mustCall()); | ||
})); | ||
|
||
const timer = setTimeout(() => { | ||
assert.fail('should not timeout here'); | ||
req.abort(); | ||
}, 1000); | ||
|
||
req.on('socket', common.mustCall((socket) => { | ||
// wait free socket become free and timeout | ||
socket.on('timeout', common.mustCall(() => { | ||
// free socket should be destroyed | ||
assert.strictEqual(socket.writable, false); | ||
// send new request will be fails | ||
clearTimeout(timer); | ||
const newReq = http.get({ | ||
path: '/', | ||
port: server.address().port, | ||
agent | ||
}, common.mustCall((res) => { | ||
// agent must create a new socket to handle request | ||
assert.notStrictEqual(newReq.socket, socket); | ||
assert.strictEqual(res.statusCode, 200); | ||
res.resume(); | ||
res.on('end', common.mustCall(() => server.close())); | ||
})); | ||
})); | ||
})); | ||
})); |