Skip to content

Commit

Permalink
feat: impl the new Agent extend http.Agent
Browse files Browse the repository at this point in the history
This is a BREAKING change.

Don't use patch mode anymore.
Improve code coverage to 100%.
  • Loading branch information
fengmk2 committed Oct 22, 2018
1 parent 9b81362 commit 5c9f3bb
Show file tree
Hide file tree
Showing 19 changed files with 1,024 additions and 613 deletions.
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
17 changes: 9 additions & 8 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,7 +222,7 @@ Socket created:

## License

```
```txt
(The MIT License)
Copyright(c) node-modules and other contributors.
Expand Down
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

0 comments on commit 5c9f3bb

Please sign in to comment.