Skip to content
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

feat: impl the new Agent extend http.Agent #69

Merged
merged 3 commits into from
Oct 22, 2018
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
3 changes: 0 additions & 3 deletions .autod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ module.exports = {
keep: [
],
semver: [
'egg-bin@1',
'eslint@4',
'eslint-config-egg@6',
],
};
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ results
node_modules
npm-debug.log
coverage/
.nyc_output/
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
sudo: false
language: node_js
node_js:
- '4'
- '6'
- '8'
- '10'
install:
Expand Down
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License

Copyright(c) node-modules and other contributors.
Copyright(c) 2012 - 2015 fengmk2 <[email protected]>

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.
41 changes: 9 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
[download-image]: https://img.shields.io/npm/dm/agentkeepalive.svg?style=flat-square
[download-url]: https://npmjs.org/package/agentkeepalive

The Node.js's missing `keep alive` `http.Agent`. Support `http` and `https`.
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.

## Install

Expand All @@ -47,13 +48,13 @@ $ npm install agentkeepalive --save
* `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 if `keepAlive` is set to `true`.
* `freeSocketKeepAliveTimeout`: {Number} Sets the free socket to timeout
after `freeSocketKeepAliveTimeout` milliseconds of inactivity on the free socket.
* `freeSocketTimeout`: {Number} Sets the free socket to timeout
after `freeSocketTimeout` milliseconds of inactivity on the free socket.
Default is `15000`.
Only relevant if `keepAlive` is set to `true`.
* `timeout`: {Number} Sets the working socket to timeout
after `timeout` milliseconds of inactivity on the working socket.
Default is `freeSocketKeepAliveTimeout * 2`.
Default is `freeSocketTimeout * 2`.
* `maxSockets` {Number} Maximum number of sockets to allow per
host. Default = `Infinity`.
* `maxFreeSockets` {Number} Maximum number of sockets (per host) to leave open
Expand All @@ -72,8 +73,8 @@ const Agent = require('agentkeepalive');
const keepaliveAgent = new Agent({
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000,
freeSocketKeepAliveTimeout: 30000, // free socket keepalive for 30 seconds
timeout: 60000, // active socket keepalive for 60 seconds
freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
});

const options = {
Expand Down Expand Up @@ -211,7 +212,7 @@ Shortest transaction: 0.00

Socket created:

```
```bash
[proxy.js:120000] keepalive, 50 created, 60000 requestFinished, 1200 req/socket, 0 requests, 0 sockets, 0 unusedSockets, 50 timeout
{" <10ms":662," <15ms":17825," <20ms":20552," <30ms":17646," <40ms":2315," <50ms":567," <100ms":377," <150ms":56," <200ms":0," >=200ms+":0}
----------------------------------------------------------------
Expand All @@ -221,28 +222,4 @@ Socket created:

## License

```
(The MIT License)

Copyright(c) node-modules and other contributors.
Copyright(c) 2012 - 2015 fengmk2 <[email protected]>

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.
```
[MIT](LICENSE)
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '8'
- nodejs_version: '10'

Expand Down
8 changes: 3 additions & 5 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
machine:
node:
version: '4'
version: '8'

dependencies:
override:
- nvm ls

test:
override:
- nvm i 4.3.2 && rm -rf node_modules && npm i npminstall && node_modules/.bin/npminstall && npm run ci
- nvm i 4 && rm -rf node_modules && npm i npminstall && node_modules/.bin/npminstall && npm run ci
- nvm i 6 && rm -rf node_modules && npm i npminstall && node_modules/.bin/npminstall && npm run ci
- nvm i 7 && rm -rf node_modules && npm i npminstall && node_modules/.bin/npminstall && npm run ci
- nvm i 8 && rm -rf node_modules && npm i npminstall && node_modules/.bin/npminstall && npm run ci
- nvm i 10 && rm -rf node_modules && npm i npminstall && node_modules/.bin/npminstall && npm run ci

# https://circleci.com/docs/language-nodejs
# https://discuss.circleci.com/t/testing-multiple-versions-of-node/542
18 changes: 8 additions & 10 deletions example/agent.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
'use strict';

const http = require('http');
const Agent = require('../');
const Agent = require('..');

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

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

function get() {
Expand All @@ -36,15 +34,15 @@ function get() {
get();

setTimeout(() => {
console.log('keep alive sockets:', keepaliveAgent);
console.log('keep alive sockets:', agent);
process.exit();
}, 300000);

let count = 0;
setInterval(function() {
const name = keepaliveAgent.getName(options);
const sockets = keepaliveAgent.sockets[name] || [];
const freeSockets = keepaliveAgent.freeSockets[name] || [];
setInterval(() => {
const name = agent.getName(options);
const sockets = agent.sockets[name] || [];
const freeSockets = agent.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);
Expand Down
8 changes: 3 additions & 5 deletions example/https_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
const https = require('https');
const HttpsAgent = require('..').HttpsAgent;

const keepaliveAgent = new HttpsAgent({
keepAlive: true,
});
const agent = new HttpsAgent();
// https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8
const options = {
host: 'github.com',
port: 443,
path: '/',
method: 'GET',
agent: keepaliveAgent,
agent,
};

let start = Date.now();
Expand Down Expand Up @@ -44,6 +42,6 @@ req.on('error', e => {
req.end();

setTimeout(() => {
console.log('keep alive sockets:', keepaliveAgent);
console.log('keep alive sockets:', agent);
process.exit();
}, 5000);
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ declare module "agentkeepalive" {
}

interface HttpOptions extends http.AgentOptions {
keepAlive?: boolean;
freeSocketTimeout?: number;
freeSocketKeepAliveTimeout?: number;
timeout?: number;
socketActiveTTL?: number;
}

interface HttpsOptions extends https.AgentOptions {
keepAlive?: boolean;
freeSocketTimeout?: number;
freeSocketKeepAliveTimeout?: number;
timeout?: number;
socketActiveTTL?: number;
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

module.exports = require('./lib/agent');
module.exports.HttpsAgent = require('./lib/https_agent');
module.exports.constants = require('./lib/constants');
Loading