What is agentkeepalive?
The agentkeepalive npm package is a module that provides an HTTP(S) Agent which keeps socket connections alive between keep-alive requests. It is designed to improve the performance of HTTP clients by reusing sockets for subsequent requests to the same servers, which can reduce the overhead of establishing new TCP connections.
What are agentkeepalive's main functionalities?
HTTP Keep-Alive Agent
This feature allows you to create an HTTP agent that reuses sockets for HTTP requests to improve performance.
const { HttpAgent } = require('agentkeepalive');
const keepaliveAgent = new HttpAgent();
const http = require('http');
http.request({
host: 'localhost',
port: 80,
path: '/',
agent: keepaliveAgent
});
HTTPS Keep-Alive Agent
This feature allows you to create an HTTPS agent that reuses sockets for HTTPS requests to improve performance.
const { HttpsAgent } = require('agentkeepalive').HttpsAgent;
const keepaliveAgent = new HttpsAgent();
const https = require('https');
https.request({
host: 'localhost',
port: 443,
path: '/',
agent: keepaliveAgent
});
Custom Agent Options
This feature allows you to customize the behavior of the keep-alive agent by setting options such as the maximum number of sockets, the maximum number of free sockets, socket timeout, and free socket timeout.
const { HttpAgent } = require('agentkeepalive');
const keepaliveAgent = new HttpAgent({
maxSockets: 50,
maxFreeSockets: 10,
timeout: 60000,
freeSocketTimeout: 30000
});
Other packages similar to agentkeepalive
forever-agent
forever-agent is similar to agentkeepalive in that it provides an HTTP(S) agent that keeps connections alive indefinitely. It is less configurable than agentkeepalive and does not provide the same level of control over socket reuse and timeouts.
agentkeepalive
The nodejs's missing keep alive
http.Agent
.
jscoverage: 93%
Install
$ npm install agentkeepalive
Usage
var http = require('http');
var Agent = require('agentkeepalive');
var keepaliveAgent = new Agent({
maxSockets: 10,
maxKeepAliveTime: 30000
});
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);
run the benchmark:
cd benchmark
sh start.sh
100 maxSockets, 120 concurrent, 1000 requests per concurrent, 5ms delay
Keep alive agent (30 seconds):
Transactions: 120000 hits
Availability: 100.00 %
Elapsed time: 53.86 secs
Data transferred: 8.58 MB
Response time: 0.05 secs
Transaction rate: 2228.00 trans/sec
Throughput: 0.16 MB/sec
Concurrency: 119.75
Successful transactions: 120000
Failed transactions: 0
Longest transaction: 0.23
Shortest transaction: 0.02
Normal agent:
Transactions: 120000 hits
Availability: 100.00 %
Elapsed time: 99.68 secs
Data transferred: 8.58 MB
Response time: 0.10 secs
Transaction rate: 1203.85 trans/sec
Throughput: 0.09 MB/sec
Concurrency: 119.29
Successful transactions: 120000
Failed transactions: 0
Longest transaction: 0.30
Shortest transaction: 0.00
Socket created:
[proxy.js] keepalive, 100 created, 0 requests, 0 sockets, 0 unusedSockets
[proxy.js] normal , 114412 created, 0 requests, 0 sockets
Authors
Below is the output from git-summary
.
project: agentkeepalive
commits: 10
active : 3 days
files : 13
authors:
10 fengmk2 100.0%
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.