Skip to content

fix: pick _evictSession to httpsAgent #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions example/agent.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
var http = require('http');
var Agent = require('../');
// var Agent = require('http').Agent;

var keepaliveAgent = new Agent({
keepAlive: true
});
// https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8

var options = {
host: 'gitcafe.com',
host: 'www.taobao.com',
path: '/',
method: 'GET',
port: 80,
agent: keepaliveAgent
};

var start = Date.now();
var req = http.get(options, function (res) {
console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start);
console.log('HEADERS1: %j', res.headers);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY1: %d', chunk.length);
});
res.on('end', function () {
process.nextTick(function () {
start = Date.now();
http.get(options, function (res) {
console.log('STATUS2: %d, %d ms', res.statusCode, Date.now() - start);
console.log('HEADERS2: %j', res.headers);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY2: %d', chunk.length);
});
});
function get() {
var start = Date.now();
var req = http.get(options, function (res) {
console.log('STATUS1: %d, %d ms', res.statusCode, Date.now() - start);
console.log('HEADERS1: %j', res.headers);
res.on('data', function (chunk) {
console.log('BODY1: %d', chunk.length);
});
res.on('end', function () {
console.log('get end');
});
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
}

get();

setTimeout(function () {
console.log('keep alive sockets:', keepaliveAgent);
process.exit();
}, 3000);
}, 300000);

var count = 0;
setInterval(function() {
var name = keepaliveAgent.getName(options);
var sockets = keepaliveAgent.sockets[name] || [];
var freeSockets = keepaliveAgent.freeSockets[name] || [];
console.log('%ss, %s, sockets: %d, destroyed: %s, free sockets: %d, destroyed: %s', ++count,
name, sockets.length, sockets[0] && sockets[0].destroyed,
freeSockets.length, freeSockets[0] && freeSockets[0].destroyed);
}, 1000);

setInterval(get, 120000);
9 changes: 9 additions & 0 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var http = require('http');

http.createServer(function(req, res) {
req.resume();
req.on('end', function() {
res.statusCode = 200;
res.end(req.method + ' ' + req.url);
});
}).listen(8080);
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module.exports = require('./lib/agent');
module.exports = require('./lib/agent');
module.exports.HttpsAgent = require('./lib/https_agent');
12 changes: 1 addition & 11 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@
var net = require('net');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var debug;
var node10 = process.version.indexOf('v0.10.') === 0;
if (node10) {
debug = function () {
if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) {
console.log.apply(console.log, arguments);
}
};
} else {
debug = util.debuglog('agentkeepalive');
}
var debug = require('./utils').debug;

// New Agent code.

Expand Down
65 changes: 3 additions & 62 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,16 @@
* MIT Licensed
*/

"use strict";
'use strict';

/**
* Module dependencies.
*/

var util = require('util');
var https = require('https');
var debug;
var utils = require('./utils');
var OriginalAgent = require('./_http_agent').Agent;
var OriginalHttpsAgent = https.Agent;
var node10 = process.version.indexOf('v0.10.') === 0;

if (node10) {
debug = function () {
if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) {
console.log.apply(console.log, arguments);
}
};
} else {
debug = util.debuglog('agentkeepalive');
}

module.exports = Agent;

Expand Down Expand Up @@ -74,7 +62,7 @@ function Agent(options) {
});
}

util.inherits(Agent, OriginalAgent);
utils.inherits(Agent, OriginalAgent);

Agent.prototype.createSocket = function (req, options) {
var socket = OriginalAgent.prototype.createSocket.call(this, req, options);
Expand Down Expand Up @@ -107,50 +95,3 @@ function inspect(obj) {
}
return res;
}

function HttpsAgent(options) {
Agent.call(this, options);
this.defaultPort = 443;
this.protocol = 'https:';
}

util.inherits(HttpsAgent, Agent);

if (node10) {
HttpsAgent.prototype.createConnection = https.globalAgent.createConnection;
HttpsAgent.prototype.getName = function(options) {
var name = Agent.prototype.getName.call(this, options);

name += ':';
if (options.ca)
name += options.ca;

name += ':';
if (options.cert)
name += options.cert;

name += ':';
if (options.ciphers)
name += options.ciphers;

name += ':';
if (options.key)
name += options.key;

name += ':';
if (options.pfx)
name += options.pfx;

name += ':';
if (options.rejectUnauthorized !== undefined)
name += options.rejectUnauthorized;

return name;
};

} else {
HttpsAgent.prototype.createConnection = OriginalHttpsAgent.prototype.createConnection;
HttpsAgent.prototype.getName = OriginalHttpsAgent.prototype.getName;
}

Agent.HttpsAgent = HttpsAgent;
95 changes: 95 additions & 0 deletions lib/https_agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Https Agent base on custom http agent
*
* Copyright(c) node-modules and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
* Module dependencies.
*/

var https = require('https');
var utils = require('./utils');
var HttpAgent = require('./agent');
var OriginalHttpsAgent = https.Agent;

var HttpsAgent;

if (utils.isNode10) {
// node v0.10
HttpsAgent = function HttpsAgent(options) {
HttpAgent.call(this, options);
this.defaultPort = 443;
this.protocol = 'https:';
};

utils.inherits(HttpsAgent, HttpAgent);

HttpsAgent.prototype.createConnection = https.globalAgent.createConnection;
HttpsAgent.prototype.getName = function(options) {
var name = HttpAgent.prototype.getName.call(this, options);

name += ':';
if (options.ca)
name += options.ca;

name += ':';
if (options.cert)
name += options.cert;

name += ':';
if (options.ciphers)
name += options.ciphers;

name += ':';
if (options.key)
name += options.key;

name += ':';
if (options.pfx)
name += options.pfx;

name += ':';
if (options.rejectUnauthorized !== undefined)
name += options.rejectUnauthorized;

return name;
};
} else {
HttpsAgent = function HttpsAgent(options) {
HttpAgent.call(this, options);
this.defaultPort = 443;
this.protocol = 'https:';
this.maxCachedSessions = this.options.maxCachedSessions;
if (this.maxCachedSessions === undefined)
this.maxCachedSessions = 100;

this._sessionCache = {
map: {},
list: []
};
};

utils.inherits(HttpsAgent, HttpAgent);

[
'createConnection',
'getName',
'_getSession',
'_cacheSession',
// https://github.com/nodejs/node/pull/4982
'_evictSession',
].forEach(function(method) {
if (typeof OriginalHttpsAgent.prototype[method] === 'function') {
HttpsAgent.prototype[method] = OriginalHttpsAgent.prototype[method];
}
});
}

module.exports = HttpsAgent;
31 changes: 31 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright(c) node-modules and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/

'use strict';

/**
* Module dependencies.
*/

var util = require('util');
var debug;
var node10 = process.version.indexOf('v0.10.') === 0;

if (node10) {
debug = function () {
if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) {
console.log.apply(console.log, arguments);
}
};
} else {
debug = util.debuglog('agentkeepalive');
}

exports.debug = debug;
exports.isNode10 = node10;
exports.inherits = util.inherits;
14 changes: 6 additions & 8 deletions test/https_agent.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*!
* agentkeepalive - test/https_agent.test.js
*
/**
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
* Copyright(c) node-modules
* MIT Licensed
Expand All @@ -18,9 +16,9 @@ var urlparse = require('url').parse;
var should = require('should');
var pedding = require('pedding');
var fs = require('fs');
var utils = require('../lib/utils');

describe('https_agent.test.js', function () {

describe('test/https_agent.test.js', function() {
var app = null;
var port = null;
var agentkeepalive = new HttpsAgent({
Expand All @@ -32,8 +30,8 @@ describe('https_agent.test.js', function () {
var node10 = process.version.indexOf('v0.10.') === 0;

before(function (done) {
if (node10) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
if (utils.isNode10) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
}
app = https.createServer({
key: fs.readFileSync(__dirname + '/fixtures/agenttest-key.pem'),
Expand Down Expand Up @@ -67,7 +65,7 @@ describe('https_agent.test.js', function () {
});

after(function (done) {
if (node10) {
if (utils.isNode10) {
// recover original setting
process.env.NODE_TLS_REJECT_UNAUTHORIZED = nodeTlsRejectUnauthorized;
}
Expand Down
Loading