diff --git a/History.md b/History.md new file mode 100644 index 0000000..643d557 --- /dev/null +++ b/History.md @@ -0,0 +1,16 @@ + +0.2.0 / 2013-11-06 +================== + + * use keepalive agent on node 0.11+ impl + +0.1.5 / 2013-06-24 +================== + + * support coveralls + * add node 0.10 test + * add 0.8.22 original https.js + * add original http.js module to diff + * update jscover + * mv pem to fixtures + * add https agent usage diff --git a/example/agent.js b/example/agent.js new file mode 100644 index 0000000..8cfacf2 --- /dev/null +++ b/example/agent.js @@ -0,0 +1,44 @@ +var http = require('http'); +var Agent = require('../'); + +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', + path: '/', + method: 'GET', + 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); + }); + }); + }); + }); +}); +req.on('error', function (e) { + console.log('problem with request: ' + e.message); +}); + +setTimeout(function () { + console.log('keep alive sockets:', keepaliveAgent); + process.exit(); +}, 3000); diff --git a/example/https_agent.js b/example/https_agent.js index 034023b..272620a 100644 --- a/example/https_agent.js +++ b/example/https_agent.js @@ -1,22 +1,38 @@ var https = require('https'); var HttpsAgent = require('../').HttpsAgent; -var keepaliveAgent = new HttpsAgent(); +var keepaliveAgent = new HttpsAgent({ + keepAlive: true +}); // https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8 var options = { - host: 'www.google.com', + host: 'github.com', port: 443, - path: '/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8', + path: '/', method: 'GET', agent: keepaliveAgent }; +var start = Date.now(); var req = https.request(options, function (res) { - console.log('STATUS: ' + res.statusCode); - console.log('HEADERS: ' + JSON.stringify(res.headers)); + 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('BODY: ' + chunk); + console.log('BODY1: %d', chunk.length); + }); + res.on('end', function () { + process.nextTick(function () { + start = Date.now(); + https.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); + }); + }); + }); }); }); @@ -26,7 +42,6 @@ req.on('error', function (e) { req.end(); setTimeout(function () { - console.log('keep alive sockets:'); - console.log(keepaliveAgent.unusedSockets); + console.log('keep alive sockets:', keepaliveAgent); process.exit(); -}, 2000); \ No newline at end of file +}, 5000); diff --git a/lib/_http_agent.js b/lib/_http_agent.js index c09bb82..a955e16 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -104,7 +104,7 @@ function Agent(options) { freeSockets = freeSockets || []; self.freeSockets[name] = freeSockets; socket.setKeepAlive(true, self.keepAliveMsecs); - socket.unref(); + socket.unref && socket.unref(); socket._httpMessage = null; self.removeSocket(socket, options); freeSockets.push(socket); @@ -170,7 +170,7 @@ Agent.prototype.addRequest = function(req, options) { if (!this.freeSockets[name].length) delete this.freeSockets[name]; - socket.ref(); + socket.ref && socket.ref(); req.onSocket(socket); this.sockets[name].push(socket); } else if (sockLen < this.maxSockets) { diff --git a/package.json b/package.json index f33da9f..9c9ad94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "agentkeepalive", - "version": "0.1.5", + "version": "0.2.0", "description": "Missing keepalive http.Agent", "main": "index.js", "directories": { @@ -32,6 +32,7 @@ "coveralls": "*", "mocha-lcov-reporter": "*" }, + "engines": { "node": ">= 0.10.0" }, "author": "fengmk2 (http://fengmk2.github.com)", "license": "MIT" }