Skip to content

Commit 1af46fa

Browse files
committed
Merge pull request #35 from node-modules/evictSession
fix: pick _evictSession to httpsAgent
2 parents 47c8dcd + b85d619 commit 1af46fa

File tree

9 files changed

+242
-107
lines changed

9 files changed

+242
-107
lines changed

example/agent.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
var http = require('http');
22
var Agent = require('../');
3+
// var Agent = require('http').Agent;
34

45
var keepaliveAgent = new Agent({
56
keepAlive: true
67
});
78
// https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8
9+
810
var options = {
9-
host: 'gitcafe.com',
11+
host: 'www.taobao.com',
1012
path: '/',
1113
method: 'GET',
14+
port: 80,
1215
agent: keepaliveAgent
1316
};
1417

15-
var start = Date.now();
16-
var req = http.get(options, function (res) {
17-
console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start);
18-
console.log('HEADERS1: %j', res.headers);
19-
res.setEncoding('utf8');
20-
res.on('data', function (chunk) {
21-
console.log('BODY1: %d', chunk.length);
22-
});
23-
res.on('end', function () {
24-
process.nextTick(function () {
25-
start = Date.now();
26-
http.get(options, function (res) {
27-
console.log('STATUS2: %d, %d ms', res.statusCode, Date.now() - start);
28-
console.log('HEADERS2: %j', res.headers);
29-
res.setEncoding('utf8');
30-
res.on('data', function (chunk) {
31-
console.log('BODY2: %d', chunk.length);
32-
});
33-
});
18+
function get() {
19+
var start = Date.now();
20+
var req = http.get(options, function (res) {
21+
console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start);
22+
console.log('HEADERS1: %j', res.headers);
23+
res.on('data', function (chunk) {
24+
console.log('BODY1: %d', chunk.length);
25+
});
26+
res.on('end', function () {
27+
console.log('get end');
3428
});
3529
});
36-
});
37-
req.on('error', function (e) {
38-
console.log('problem with request: ' + e.message);
39-
});
30+
req.on('error', function (e) {
31+
console.log('problem with request: ' + e.message);
32+
});
33+
}
34+
35+
get();
4036

4137
setTimeout(function () {
4238
console.log('keep alive sockets:', keepaliveAgent);
4339
process.exit();
44-
}, 3000);
40+
}, 300000);
41+
42+
var count = 0;
43+
setInterval(function() {
44+
var name = keepaliveAgent.getName(options);
45+
var sockets = keepaliveAgent.sockets[name] || [];
46+
var freeSockets = keepaliveAgent.freeSockets[name] || [];
47+
console.log('%ss, %s, sockets: %d, destroyed: %s, free sockets: %d, destroyed: %s', ++count,
48+
name, sockets.length, sockets[0] && sockets[0].destroyed,
49+
freeSockets.length, freeSockets[0] && freeSockets[0].destroyed);
50+
}, 1000);
51+
52+
setInterval(get, 120000);

example/server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var http = require('http');
2+
3+
http.createServer(function(req, res) {
4+
req.resume();
5+
req.on('end', function() {
6+
res.statusCode = 200;
7+
res.end(req.method + ' ' + req.url);
8+
});
9+
}).listen(8080);

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
module.exports = require('./lib/agent');
1+
module.exports = require('./lib/agent');
2+
module.exports.HttpsAgent = require('./lib/https_agent');

lib/_http_agent.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@
2626
var net = require('net');
2727
var util = require('util');
2828
var EventEmitter = require('events').EventEmitter;
29-
var debug;
30-
var node10 = process.version.indexOf('v0.10.') === 0;
31-
if (node10) {
32-
debug = function () {
33-
if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) {
34-
console.log.apply(console.log, arguments);
35-
}
36-
};
37-
} else {
38-
debug = util.debuglog('agentkeepalive');
39-
}
29+
var debug = require('./utils').debug;
4030

4131
// New Agent code.
4232

lib/agent.js

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,16 @@
1010
* MIT Licensed
1111
*/
1212

13-
"use strict";
13+
'use strict';
1414

1515
/**
1616
* Module dependencies.
1717
*/
1818

19-
var util = require('util');
2019
var https = require('https');
21-
var debug;
20+
var utils = require('./utils');
2221
var OriginalAgent = require('./_http_agent').Agent;
2322
var OriginalHttpsAgent = https.Agent;
24-
var node10 = process.version.indexOf('v0.10.') === 0;
25-
26-
if (node10) {
27-
debug = function () {
28-
if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) {
29-
console.log.apply(console.log, arguments);
30-
}
31-
};
32-
} else {
33-
debug = util.debuglog('agentkeepalive');
34-
}
3523

3624
module.exports = Agent;
3725

@@ -74,7 +62,7 @@ function Agent(options) {
7462
});
7563
}
7664

77-
util.inherits(Agent, OriginalAgent);
65+
utils.inherits(Agent, OriginalAgent);
7866

7967
Agent.prototype.createSocket = function (req, options) {
8068
var socket = OriginalAgent.prototype.createSocket.call(this, req, options);
@@ -107,50 +95,3 @@ function inspect(obj) {
10795
}
10896
return res;
10997
}
110-
111-
function HttpsAgent(options) {
112-
Agent.call(this, options);
113-
this.defaultPort = 443;
114-
this.protocol = 'https:';
115-
}
116-
117-
util.inherits(HttpsAgent, Agent);
118-
119-
if (node10) {
120-
HttpsAgent.prototype.createConnection = https.globalAgent.createConnection;
121-
HttpsAgent.prototype.getName = function(options) {
122-
var name = Agent.prototype.getName.call(this, options);
123-
124-
name += ':';
125-
if (options.ca)
126-
name += options.ca;
127-
128-
name += ':';
129-
if (options.cert)
130-
name += options.cert;
131-
132-
name += ':';
133-
if (options.ciphers)
134-
name += options.ciphers;
135-
136-
name += ':';
137-
if (options.key)
138-
name += options.key;
139-
140-
name += ':';
141-
if (options.pfx)
142-
name += options.pfx;
143-
144-
name += ':';
145-
if (options.rejectUnauthorized !== undefined)
146-
name += options.rejectUnauthorized;
147-
148-
return name;
149-
};
150-
151-
} else {
152-
HttpsAgent.prototype.createConnection = OriginalHttpsAgent.prototype.createConnection;
153-
HttpsAgent.prototype.getName = OriginalHttpsAgent.prototype.getName;
154-
}
155-
156-
Agent.HttpsAgent = HttpsAgent;

lib/https_agent.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Https Agent base on custom http agent
3+
*
4+
* Copyright(c) node-modules and other contributors.
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
9+
*/
10+
11+
'use strict';
12+
13+
/**
14+
* Module dependencies.
15+
*/
16+
17+
var https = require('https');
18+
var utils = require('./utils');
19+
var HttpAgent = require('./agent');
20+
var OriginalHttpsAgent = https.Agent;
21+
22+
var HttpsAgent;
23+
24+
if (utils.isNode10) {
25+
// node v0.10
26+
HttpsAgent = function HttpsAgent(options) {
27+
HttpAgent.call(this, options);
28+
this.defaultPort = 443;
29+
this.protocol = 'https:';
30+
};
31+
32+
utils.inherits(HttpsAgent, HttpAgent);
33+
34+
HttpsAgent.prototype.createConnection = https.globalAgent.createConnection;
35+
HttpsAgent.prototype.getName = function(options) {
36+
var name = HttpAgent.prototype.getName.call(this, options);
37+
38+
name += ':';
39+
if (options.ca)
40+
name += options.ca;
41+
42+
name += ':';
43+
if (options.cert)
44+
name += options.cert;
45+
46+
name += ':';
47+
if (options.ciphers)
48+
name += options.ciphers;
49+
50+
name += ':';
51+
if (options.key)
52+
name += options.key;
53+
54+
name += ':';
55+
if (options.pfx)
56+
name += options.pfx;
57+
58+
name += ':';
59+
if (options.rejectUnauthorized !== undefined)
60+
name += options.rejectUnauthorized;
61+
62+
return name;
63+
};
64+
} else {
65+
HttpsAgent = function HttpsAgent(options) {
66+
HttpAgent.call(this, options);
67+
this.defaultPort = 443;
68+
this.protocol = 'https:';
69+
this.maxCachedSessions = this.options.maxCachedSessions;
70+
if (this.maxCachedSessions === undefined)
71+
this.maxCachedSessions = 100;
72+
73+
this._sessionCache = {
74+
map: {},
75+
list: []
76+
};
77+
};
78+
79+
utils.inherits(HttpsAgent, HttpAgent);
80+
81+
[
82+
'createConnection',
83+
'getName',
84+
'_getSession',
85+
'_cacheSession',
86+
// https://github.com/nodejs/node/pull/4982
87+
'_evictSession',
88+
].forEach(function(method) {
89+
if (typeof OriginalHttpsAgent.prototype[method] === 'function') {
90+
HttpsAgent.prototype[method] = OriginalHttpsAgent.prototype[method];
91+
}
92+
});
93+
}
94+
95+
module.exports = HttpsAgent;

lib/utils.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright(c) node-modules and other contributors.
3+
* MIT Licensed
4+
*
5+
* Authors:
6+
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
7+
*/
8+
9+
'use strict';
10+
11+
/**
12+
* Module dependencies.
13+
*/
14+
15+
var util = require('util');
16+
var debug;
17+
var node10 = process.version.indexOf('v0.10.') === 0;
18+
19+
if (node10) {
20+
debug = function () {
21+
if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) {
22+
console.log.apply(console.log, arguments);
23+
}
24+
};
25+
} else {
26+
debug = util.debuglog('agentkeepalive');
27+
}
28+
29+
exports.debug = debug;
30+
exports.isNode10 = node10;
31+
exports.inherits = util.inherits;

test/https_agent.test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*!
2-
* agentkeepalive - test/https_agent.test.js
3-
*
1+
/**
42
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
53
* Copyright(c) node-modules
64
* MIT Licensed
@@ -18,9 +16,9 @@ var urlparse = require('url').parse;
1816
var should = require('should');
1917
var pedding = require('pedding');
2018
var fs = require('fs');
19+
var utils = require('../lib/utils');
2120

22-
describe('https_agent.test.js', function () {
23-
21+
describe('test/https_agent.test.js', function() {
2422
var app = null;
2523
var port = null;
2624
var agentkeepalive = new HttpsAgent({
@@ -32,8 +30,8 @@ describe('https_agent.test.js', function () {
3230
var node10 = process.version.indexOf('v0.10.') === 0;
3331

3432
before(function (done) {
35-
if (node10) {
36-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
33+
if (utils.isNode10) {
34+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
3735
}
3836
app = https.createServer({
3937
key: fs.readFileSync(__dirname + '/fixtures/agenttest-key.pem'),
@@ -67,7 +65,7 @@ describe('https_agent.test.js', function () {
6765
});
6866

6967
after(function (done) {
70-
if (node10) {
68+
if (utils.isNode10) {
7169
// recover original setting
7270
process.env.NODE_TLS_REJECT_UNAUTHORIZED = nodeTlsRejectUnauthorized;
7371
}

0 commit comments

Comments
 (0)