Installations
npm install agentkeepalive
Score
98.6
Supply Chain
99.5
Quality
81.7
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Developer
node-modules
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
>= 8.0.0
Typescript Support
No
Node Version
18.17.0
NPM Version
6.14.18
Statistics
581 Stars
154 Commits
57 Forks
31 Watching
4 Branches
40 Contributors
Updated on 06 Nov 2024
Bundle Size
190.00 B
Minified
156.00 B
Minified + Gzipped
Languages
JavaScript (99.04%)
Shell (0.96%)
Total Downloads
Cumulative downloads
Total Downloads
2,070,436,943
Last day
-14%
2,018,937
Compared to previous day
Last week
0.8%
12,276,837
Compared to previous week
Last month
7.8%
51,316,149
Compared to previous month
Last year
-1.9%
542,796,874
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
agentkeepalive
The enhancement features keep alive
http.Agent
. Support http
and https
.
What's different from original http.Agent
?
keepAlive=true
by default- Disable Nagle's algorithm:
socket.setNoDelay(true)
- Add free socket timeout: avoid long time inactivity socket leak in the free-sockets queue.
- Add active socket timeout: avoid long time inactivity socket leak in the active-sockets queue.
- TTL for active socket.
Node.js version required
Support Node.js >= 8.0.0
Install
1$ npm install agentkeepalive --save
new Agent([options])
options
{Object} Set of configurable options to set on the agent. Can have the following fields:keepAlive
{Boolean} Keep sockets around in a pool to be used by other requests in the future. Default =true
.keepAliveMsecs
{Number} When using the keepAlive option, specifies the initial delay for TCP Keep-Alive packets. Ignored when the keepAlive option is false or undefined. Defaults to 1000. Default =1000
. Only relevant ifkeepAlive
is set totrue
.freeSocketTimeout
: {Number} Sets the free socket to timeout afterfreeSocketTimeout
milliseconds of inactivity on the free socket. The default server-side timeout is 5000 milliseconds, to avoid ECONNRESET exceptions, we set the default value to4000
milliseconds. Only relevant ifkeepAlive
is set totrue
.timeout
: {Number} Sets the working socket to timeout aftertimeout
milliseconds of inactivity on the working socket. Default isfreeSocketTimeout * 2
so long as that value is greater than or equal to 8 seconds, otherwise the default is 8 seconds.maxSockets
{Number} Maximum number of sockets to allow per host. Default =Infinity
.maxFreeSockets
{Number} Maximum number of sockets (per host) to leave open in a free state. Only relevant ifkeepAlive
is set totrue
. Default =256
.socketActiveTTL
{Number} Sets the socket active time to live, even if it's in use. If not set, the behaviour keeps the same (the socket will be released only when free) Default =null
.
Usage
1const http = require('http'); 2const Agent = require('agentkeepalive'); 3 4const keepaliveAgent = new Agent({ 5 maxSockets: 100, 6 maxFreeSockets: 10, 7 timeout: 60000, // active socket keepalive for 60 seconds 8 freeSocketTimeout: 30000, // free socket keepalive for 30 seconds 9}); 10 11const options = { 12 host: 'cnodejs.org', 13 port: 80, 14 path: '/', 15 method: 'GET', 16 agent: keepaliveAgent, 17}; 18 19const req = http.request(options, res => { 20 console.log('STATUS: ' + res.statusCode); 21 console.log('HEADERS: ' + JSON.stringify(res.headers)); 22 res.setEncoding('utf8'); 23 res.on('data', function (chunk) { 24 console.log('BODY: ' + chunk); 25 }); 26}); 27req.on('error', e => { 28 console.log('problem with request: ' + e.message); 29}); 30req.end(); 31 32setTimeout(() => { 33 if (keepaliveAgent.statusChanged) { 34 console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus()); 35 } 36}, 2000); 37
getter agent.statusChanged
counters have change or not after last checkpoint.
agent.getCurrentStatus()
agent.getCurrentStatus()
will return a object to show the status of this agent:
1{ 2 createSocketCount: 10, 3 closeSocketCount: 5, 4 timeoutSocketCount: 0, 5 requestCount: 5, 6 freeSockets: { 'localhost:57479:': 3 }, 7 sockets: { 'localhost:57479:': 5 }, 8 requests: {} 9}
Support https
1const https = require('https'); 2const HttpsAgent = require('agentkeepalive').HttpsAgent; 3 4const keepaliveAgent = new HttpsAgent(); 5// https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8 6const options = { 7 host: 'www.google.com', 8 port: 443, 9 path: '/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8', 10 method: 'GET', 11 agent: keepaliveAgent, 12}; 13 14const req = https.request(options, res => { 15 console.log('STATUS: ' + res.statusCode); 16 console.log('HEADERS: ' + JSON.stringify(res.headers)); 17 res.setEncoding('utf8'); 18 res.on('data', chunk => { 19 console.log('BODY: ' + chunk); 20 }); 21}); 22 23req.on('error', e => { 24 console.log('problem with request: ' + e.message); 25}); 26req.end(); 27 28setTimeout(() => { 29 console.log('agent status: %j', keepaliveAgent.getCurrentStatus()); 30}, 2000);
Support req.reusedSocket
This agent implements the req.reusedSocket
to determine whether a request is send through a reused socket.
When server closes connection at unfortunate time (keep-alive race), the http client will throw a ECONNRESET
error. Under this circumstance, req.reusedSocket
is useful when we want to retry the request automatically.
1const http = require('http'); 2const Agent = require('agentkeepalive'); 3const agent = new Agent(); 4 5const req = http 6 .get('http://localhost:3000', { agent }, (res) => { 7 // ... 8 }) 9 .on('error', (err) => { 10 if (req.reusedSocket && err.code === 'ECONNRESET') { 11 // retry the request or anything else... 12 } 13 })
This behavior is consistent with Node.js core. But through agentkeepalive
, you can use this feature in older Node.js version.
Benchmark
run the benchmark:
1cd benchmark 2sh start.sh
Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
node@v0.8.9
50 maxSockets, 60 concurrent, 1000 requests per concurrent, 5ms delay
Keep alive agent (30 seconds):
1Transactions: 60000 hits 2Availability: 100.00 % 3Elapsed time: 29.70 secs 4Data transferred: 14.88 MB 5Response time: 0.03 secs 6Transaction rate: 2020.20 trans/sec 7Throughput: 0.50 MB/sec 8Concurrency: 59.84 9Successful transactions: 60000 10Failed transactions: 0 11Longest transaction: 0.15 12Shortest transaction: 0.01
Normal agent:
1Transactions: 60000 hits 2Availability: 100.00 % 3Elapsed time: 46.53 secs 4Data transferred: 14.88 MB 5Response time: 0.05 secs 6Transaction rate: 1289.49 trans/sec 7Throughput: 0.32 MB/sec 8Concurrency: 59.81 9Successful transactions: 60000 10Failed transactions: 0 11Longest transaction: 0.45 12Shortest transaction: 0.00
Socket created:
1[proxy.js:120000] keepalive, 50 created, 60000 requestFinished, 1200 req/socket, 0 requests, 0 sockets, 0 unusedSockets, 50 timeout 2{" <10ms":662," <15ms":17825," <20ms":20552," <30ms":17646," <40ms":2315," <50ms":567," <100ms":377," <150ms":56," <200ms":0," >=200ms+":0} 3---------------------------------------------------------------- 4[proxy.js:120000] normal , 53866 created, 84260 requestFinished, 1.56 req/socket, 0 requests, 0 sockets 5{" <10ms":75," <15ms":1112," <20ms":10947," <30ms":32130," <40ms":8228," <50ms":3002," <100ms":4274," <150ms":181," <200ms":18," >=200ms+":33}
License
Contributors
This project follows the git-contributor spec, auto updated at Sat Aug 05 2023 02:36:31 GMT+0800
.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 15/30 approved changesets -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/node-modules/agentkeepalive/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/node-modules/agentkeepalive/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/node-modules/agentkeepalive/nodejs.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/nodejs.yml:39
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Score
4.5
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to agentkeepalive
@xplora-uk/axios-with-agentkeepalive
axios with agentkeepalive
old-agentkeepalive
Depends on agentkeepalive@0.2.2 so that a project can depend on both 0.2.2 and 1.+
@vercel/fetch
Opinionated `fetch` optimized for use inside microservices
@zeit/fetch
Opinionated `fetch` optimized for use inside microservices