-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: overridable keep-alive behavior of
Agent
Introduce two overridable `Agent` methods: * `keepSocketAlive(socket)` * `reuseSocket(socket, req)` These methods can be overridden by particular `Agent` class child to make keep-alive behavior customizable. Motivation: destroy persisted sockets after some configurable timeout. It is very non-trivial to do it with available primitives. Such program will most likely need to poke with undocumented events and methods of `Agent`. With introduced API such behavior is easy to implement. Backport-PR-URL: #18168 PR-URL: #13005 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
7510b97
commit a1171fd
Showing
3 changed files
with
121 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const http = require('http'); | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end('ok'); | ||
}).listen(0, common.mustCall(() => { | ||
const agent = http.Agent({ | ||
keepAlive: true, | ||
maxSockets: 5, | ||
maxFreeSockets: 2 | ||
}); | ||
|
||
const keepSocketAlive = agent.keepSocketAlive; | ||
const reuseSocket = agent.reuseSocket; | ||
|
||
let called = 0; | ||
let expectedSocket; | ||
agent.keepSocketAlive = common.mustCall((socket) => { | ||
assert(socket); | ||
|
||
called++; | ||
if (called === 1) { | ||
return false; | ||
} else if (called === 2) { | ||
expectedSocket = socket; | ||
return keepSocketAlive.call(agent, socket); | ||
} | ||
|
||
assert.strictEqual(socket, expectedSocket); | ||
return false; | ||
}, 3); | ||
|
||
agent.reuseSocket = common.mustCall((socket, req) => { | ||
assert.strictEqual(socket, expectedSocket); | ||
assert(req); | ||
|
||
return reuseSocket.call(agent, socket, req); | ||
}, 1); | ||
|
||
function req(callback) { | ||
http.request({ | ||
method: 'GET', | ||
path: '/', | ||
agent, | ||
port: server.address().port | ||
}, common.mustCall((res) => { | ||
res.resume(); | ||
res.once('end', common.mustCall(() => { | ||
setImmediate(callback); | ||
})); | ||
})).end(); | ||
} | ||
|
||
// Should destroy socket instead of keeping it alive | ||
req(common.mustCall(() => { | ||
// Should keep socket alive | ||
req(common.mustCall(() => { | ||
// Should reuse the socket | ||
req(common.mustCall(() => { | ||
server.close(); | ||
})); | ||
})); | ||
})); | ||
})); |