Skip to content

Commit

Permalink
feat: support agentkeepalive@4 (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 23, 2018
1 parent c79eefc commit 28c38d2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ node_js:
env:
- AGENT_VERSION=2
- AGENT_VERSION=3
- AGENT_VERSION=4
matrix:
exclude:
- node_js: '0.12'
env: AGENT_VERSION=3
- node_js: '0.12'
env: AGENT_VERSION=4
- node_js: '4'
env: AGENT_VERSION=2
- node_js: '4'
env: AGENT_VERSION=4
- node_js: '6'
env: AGENT_VERSION=2
- node_js: '6'
env: AGENT_VERSION=4
- node_js: '8'
env: AGENT_VERSION=2
- node_js: '10'
Expand Down
2 changes: 0 additions & 2 deletions azure-pipelines.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
vmImage: ${{ parameters.vmImage }}
strategy:
matrix:
node_6:
node_version: 6
node_8:
node_version: 8
node_10:
Expand Down
8 changes: 4 additions & 4 deletions examples/httpclient2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var HttpClient = require('../').HttpClient;
var Agent = require('agentkeepalive');
var HttpAgent = require('agentkeepalive');
var HttpsAgent = require('agentkeepalive').HttpsAgent;

tryHttpclient(HttpClient, 'urllib1');
Expand All @@ -12,17 +12,17 @@ function tryHttpclient(HttpClient, name) {
method: 'HEAD',
timeout: 10000,
};
var agent = new Agent({
var httpAgent = new HttpAgent({
timeout: 60000,
freeSocketKeepAliveTimeout: 30000,
});
var httpsAgent = new HttpsAgent({
timeout: 60000,
freeSocketKeepAliveTimeout: 30000,
});
var urllib = new HttpClient({ agent: agent, httpsAgent: httpsAgent });
var urllib = new HttpClient({ agent: httpAgent, httpsAgent: httpsAgent });
urllib.on('response', function(info) {
console.log(name, agent, agent.getCurrentStatus());
console.log(name, httpAgent, httpAgent.getCurrentStatus());
console.log(name, httpsAgent, httpsAgent.getCurrentStatus());
console.log(name, info.res.keepAliveSocket);
});
Expand Down
19 changes: 14 additions & 5 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,26 @@ function requestWithCallback(url, args, callback) {
}

// only support agentkeepalive module for now
if (agent && agent.keepAlive && agent.freeSocketKeepAliveTimeout > 0 &&
// agentkeepalive@4: agent.options.freeSocketTimeout
// agentkeepalive@3: agent.freeSocketKeepAliveTimeout
var freeSocketTimeout = agent.options && agent.options.freeSocketTimeout || agent.freeSocketKeepAliveTimeout;
if (agent && agent.keepAlive && freeSocketTimeout > 0 &&
statusCode >= 200 && headers.connection === 'keep-alive' && headers['keep-alive']) {
// adjust freeSocketKeepAliveTimeout on the socket
// adjust freeSocketTimeout on the socket
var m = KEEP_ALIVE_RE.exec(headers['keep-alive']);
if (m) {
var seconds = parseInt(m[1]);
if (seconds > 0) {
// network delay 500ms
var serverKeepAliveTimeout = seconds * 1000 - 500;
if (serverKeepAliveTimeout < agent.freeSocketKeepAliveTimeout) {
res.socket.freeSocketKeepAliveTimeout = serverKeepAliveTimeout;
var serverSocketTimeout = seconds * 1000 - 500;
if (serverSocketTimeout < freeSocketTimeout) {
// https://github.com/node-modules/agentkeepalive/blob/master/lib/agent.js#L127
// agentkeepalive@4
if (agent.options && agent.options.freeSocketTimeout) {
res.socket.freeSocketTimeout = serverSocketTimeout;
} else {
res.socket.freeSocketKeepAliveTimeout = serverSocketTimeout;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"utility": "^1.12.0"
},
"devDependencies": {
"agentkeepalive": "^3.4.0",
"agentkeepalive": "^4.0.0",
"autod": "*",
"benchmark": "^2.1.4",
"bluebird": "*",
Expand Down
2 changes: 1 addition & 1 deletion test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = process.env.CI ? {
// npmjs.com do not support gzip now
npmWeb: 'https://cnpmjs.org',
npmRegistry: 'https://registry.npmjs.com',
npmHttpRegistry: 'http://registry.npm.taobao.org',
npmHttpRegistry: 'http://registry.npmjs.com',
} : {
npmWeb: 'https://npm.taobao.org',
npmRegistry: 'https://registry.npm.taobao.org',
Expand Down

0 comments on commit 28c38d2

Please sign in to comment.