Skip to content

Commit a3b1e98

Browse files
committed
http: subclass of Agent can override the initialization
Make userland's Agent inherit from http.Agent more easily.
1 parent 3b12a8d commit a3b1e98

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

lib/_http_agent.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ function Agent(options) {
6363
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
6464
self.maxFreeSockets = self.options.maxFreeSockets || 256;
6565

66+
self.init();
67+
}
68+
69+
util.inherits(Agent, EventEmitter);
70+
71+
Agent.defaultMaxSockets = Infinity;
72+
73+
Agent.prototype.createConnection = net.createConnection;
74+
75+
Agent.prototype.init = function init() {
76+
var self = this;
6677
self.on('free', function(socket, options) {
6778
var name = self.getName(options);
6879
debug('agent.on(free)', name);
@@ -105,13 +116,7 @@ function Agent(options) {
105116
}
106117
}
107118
});
108-
}
109-
110-
util.inherits(Agent, EventEmitter);
111-
112-
Agent.defaultMaxSockets = Infinity;
113-
114-
Agent.prototype.createConnection = net.createConnection;
119+
};
115120

116121
// Get the key for a given set of request options
117122
Agent.prototype.getName = function getName(options) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
class DestroyAgent extends http.Agent {
8+
constructor(options = {}) {
9+
// change all socket keepAlive by default
10+
options.keepAlive = true;
11+
super(options);
12+
13+
this.requestCount = 0;
14+
this.destroyCount = 0;
15+
}
16+
17+
init() {
18+
this.on('free', (socket, options) => {
19+
const name = this.getName(options);
20+
console.info('free socket %s', name);
21+
this.destroyCount++;
22+
socket.destroy();
23+
});
24+
}
25+
26+
addRequest(req, options) {
27+
this.requestCount++;
28+
console.info('new request %s %s', req.method, req.path);
29+
return super.addRequest(req, options);
30+
}
31+
}
32+
33+
const server = http.Server(function(req, res) {
34+
res.writeHead(200);
35+
res.end('hello world\n');
36+
});
37+
38+
server.listen(0, () => {
39+
const port = server.address().port;
40+
const destroyAgent = new DestroyAgent();
41+
http.get({ port: port, path: '/', agent: destroyAgent }, (res) => {
42+
res.resume();
43+
res.on('end', () => {
44+
http.get({ port: port, path: '/again', agent: destroyAgent }, (res) => {
45+
res.resume();
46+
res.on('end', () => {
47+
// wait socket "close callbacks" phase execute
48+
setTimeout(() => {
49+
console.error('requestCount: %s, destroyCount: %s, closing server',
50+
destroyAgent.requestCount, destroyAgent.destroyCount);
51+
assert.strictEqual(destroyAgent.requestCount, 2);
52+
assert.strictEqual(destroyAgent.destroyCount, 2);
53+
server.close();
54+
}, 100);
55+
});
56+
}).on('error', function(e) {
57+
console.log('Error!', e);
58+
process.exit(1);
59+
});
60+
});
61+
}).on('error', function(e) {
62+
console.log('Error!', e);
63+
process.exit(1);
64+
});
65+
});

0 commit comments

Comments
 (0)