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 <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 5f79448 commit 1390c28

3 files changed

Lines changed: 121 additions & 6 deletions

File tree

‎doc/api/http.md‎

Lines changed: 36 additions & 0 deletions
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‎

Lines changed: 18 additions & 6 deletions
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+
}elseif(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
varsocket=this.freeSockets[name].shift();
145-
debug('have free socket');
146146

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

151-
socket.ref();
151+
this.reuseSocket(socket,req);
152152
req.onSocket(socket);
153153
this.sockets[name].push(socket);
154154
}elseif(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=functionkeepSocketAlive(socket){
283+
socket.setKeepAlive(true,this.keepAliveMsecs);
284+
socket.unref();
285+
286+
returntrue;
287+
};
288+
289+
Agent.prototype.reuseSocket=functionreuseSocket(socket,req){
290+
debug('have free socket');
291+
socket.ref();
292+
};
293+
294+
Agent.prototype.destroy=functiondestroy(){
283295
varsets=[this.freeSockets,this.sockets];
284296
for(vars=0;s<sets.length;s++){
285297
varset=sets[s];
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
5+
consthttp=require('http');
6+
7+
constserver=http.createServer((req,res)=>{
8+
res.end('ok');
9+
}).listen(0,common.mustCall(()=>{
10+
constagent=http.Agent({
11+
keepAlive: true,
12+
maxSockets: 5,
13+
maxFreeSockets: 2
14+
});
15+
16+
constkeepSocketAlive=agent.keepSocketAlive;
17+
constreuseSocket=agent.reuseSocket;
18+
19+
letcalled=0;
20+
letexpectedSocket;
21+
agent.keepSocketAlive=common.mustCall((socket)=>{
22+
assert(socket);
23+
24+
called++;
25+
if(called===1){
26+
returnfalse;
27+
}elseif(called===2){
28+
expectedSocket=socket;
29+
returnkeepSocketAlive.call(agent,socket);
30+
}
31+
32+
assert.strictEqual(socket,expectedSocket);
33+
returnfalse;
34+
},3);
35+
36+
agent.reuseSocket=common.mustCall((socket,req)=>{
37+
assert.strictEqual(socket,expectedSocket);
38+
assert(req);
39+
40+
returnreuseSocket.call(agent,socket,req);
41+
},1);
42+
43+
functionreq(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)