Skip to content

Commit 1390c28

Browse files
indutnyMylesBorins
authored andcommitted
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]>
1 parent 5f79448 commit 1390c28

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

doc/api/http.md

+36
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,42 @@ socket/stream from this function, or by passing the socket/stream to `callback`.
159159

160160
`callback` has a signature of `(err, stream)`.
161161

162+
### agent.keepSocketAlive(socket)
163+
<!-- YAML
164+
added: REPLACEME
165+
-->
166+
167+
* `socket` {net.Socket}
168+
169+
Called when `socket` is detached from a request and could be persisted by the
170+
Agent. Default behavior is to:
171+
172+
```js
173+
socket.unref();
174+
socket.setKeepAlive(agent.keepAliveMsecs);
175+
```
176+
177+
This method can be overridden by a particular `Agent` subclass. If this
178+
method returns a falsy value, the socket will be destroyed instead of persisting
179+
it for use with the next request.
180+
181+
### agent.reuseSocket(socket, request)
182+
<!-- YAML
183+
added: REPLACEME
184+
-->
185+
186+
* `socket` {net.Socket}
187+
* `request` {http.ClientRequest}
188+
189+
Called when `socket` is attached to `request` after being persisted because of
190+
the keep-alive options. Default behavior is to:
191+
192+
```js
193+
socket.ref();
194+
```
195+
196+
This method can be overridden by a particular `Agent` subclass.
197+
162198
### agent.destroy()
163199
<!-- YAML
164200
added: v0.11.4

lib/_http_agent.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ function Agent(options) {
6767

6868
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
6969
socket.destroy();
70-
} else {
70+
} else if (self.keepSocketAlive(socket)) {
7171
freeSockets = freeSockets || [];
7272
self.freeSockets[name] = freeSockets;
73-
socket.setKeepAlive(true, self.keepAliveMsecs);
74-
socket.unref();
7573
socket._httpMessage = null;
7674
self.removeSocket(socket, options);
7775
freeSockets.push(socket);
76+
} else {
77+
// Implementation doesn't want to keep socket alive
78+
socket.destroy();
7879
}
7980
} else {
8081
socket.destroy();
@@ -142,13 +143,12 @@ Agent.prototype.addRequest = function(req, options) {
142143
if (freeLen) {
143144
// we have a free socket, so use that.
144145
var socket = this.freeSockets[name].shift();
145-
debug('have free socket');
146146

147147
// don't leak
148148
if (!this.freeSockets[name].length)
149149
delete this.freeSockets[name];
150150

151-
socket.ref();
151+
this.reuseSocket(socket, req);
152152
req.onSocket(socket);
153153
this.sockets[name].push(socket);
154154
} else if (sockLen < this.maxSockets) {
@@ -279,7 +279,19 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
279279
}
280280
};
281281

282-
Agent.prototype.destroy = function() {
282+
Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
283+
socket.setKeepAlive(true, this.keepAliveMsecs);
284+
socket.unref();
285+
286+
return true;
287+
};
288+
289+
Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
290+
debug('have free socket');
291+
socket.ref();
292+
};
293+
294+
Agent.prototype.destroy = function destroy() {
283295
var sets = [this.freeSockets, this.sockets];
284296
for (var s = 0; s < sets.length; s++) {
285297
var set = sets[s];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const http = require('http');
6+
7+
const server = http.createServer((req, res) => {
8+
res.end('ok');
9+
}).listen(0, common.mustCall(() => {
10+
const agent = http.Agent({
11+
keepAlive: true,
12+
maxSockets: 5,
13+
maxFreeSockets: 2
14+
});
15+
16+
const keepSocketAlive = agent.keepSocketAlive;
17+
const reuseSocket = agent.reuseSocket;
18+
19+
let called = 0;
20+
let expectedSocket;
21+
agent.keepSocketAlive = common.mustCall((socket) => {
22+
assert(socket);
23+
24+
called++;
25+
if (called === 1) {
26+
return false;
27+
} else if (called === 2) {
28+
expectedSocket = socket;
29+
return keepSocketAlive.call(agent, socket);
30+
}
31+
32+
assert.strictEqual(socket, expectedSocket);
33+
return false;
34+
}, 3);
35+
36+
agent.reuseSocket = common.mustCall((socket, req) => {
37+
assert.strictEqual(socket, expectedSocket);
38+
assert(req);
39+
40+
return reuseSocket.call(agent, socket, req);
41+
}, 1);
42+
43+
function req(callback) {
44+
http.request({
45+
method: 'GET',
46+
path: '/',
47+
agent,
48+
port: server.address().port
49+
}, common.mustCall((res) => {
50+
res.resume();
51+
res.once('end', common.mustCall(() => {
52+
setImmediate(callback);
53+
}));
54+
})).end();
55+
}
56+
57+
// Should destroy socket instead of keeping it alive
58+
req(common.mustCall(() => {
59+
// Should keep socket alive
60+
req(common.mustCall(() => {
61+
// Should reuse the socket
62+
req(common.mustCall(() => {
63+
server.close();
64+
}));
65+
}));
66+
}));
67+
}));

0 commit comments

Comments
 (0)