JSPM

  • Created
  • Published
  • Downloads 14221801
  • Score
    100M100P100Q216643F
  • License MIT

Missing keepalive http.Agent

Package Exports

  • agentkeepalive

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (agentkeepalive) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

agentkeepalive

Build Status

The nodejs's missing keep alive http.Agent.

jscoverage: 100%

Install

$ npm install agentkeepalive

Usage

var http = require('http');
var Agent = require('agentkeepalive');

var keepaliveAgent = new Agent({
  maxSockets: 10,
  maxKeepAliveTime: 30000 // keepalive for 30 seconds
});

var options = {
  host: 'cnodejs.org',
  port: 80,
  path: '/',
  method: 'GET',
  agent: keepaliveAgent
};

var req = http.request(options, function (res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function (e) {
  console.log('problem with request: ' + e.message);
});
req.end();

setTimeout(function () {
  console.log('keep alive sockets:');
  console.log(keepaliveAgent.unusedSockets);
}, 2000);

Benchmark

run the benchmark:

cd benchmark
sh start.sh

50 maxSockets, 60 concurrent, 1000 requests per concurrent, 5ms delay

Keep alive agent:

Transaction rate:      2214.02 trans/sec

Normal agent:

Transaction rate:      1138.30 trans/sec

Socket created:

[proxy.js] keepalive, 50 created, 0 requests, 50 sockets, 50 unusedSockets
[proxy.js] normal   , 52555 created, 0 requests, 0 sockets

License

(The MIT License)

Copyright (c) 2012 fengmk2 fengmk2@gmail.com;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.