Skip to content

Commit bc7cadb

Browse files
fengmk2dead-horse
authored andcommitted
feat: use socket custom freeSocketKeepAliveTimeout first (#59)
1 parent 138eda8 commit bc7cadb

File tree

5 files changed

+85
-8
lines changed

5 files changed

+85
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ node_js:
44
- '4.3.2'
55
- '4'
66
- '6'
7-
- '7'
87
- '8'
8+
- '9'
99
install:
1010
- npm i npminstall && npminstall
1111
script:

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ environment:
33
- nodejs_version: '4.3.2'
44
- nodejs_version: '4'
55
- nodejs_version: '6'
6-
- nodejs_version: '7'
76
- nodejs_version: '8'
7+
- nodejs_version: '9'
88

99
install:
1010
- ps: Install-Product node $env:nodejs_version
@@ -13,6 +13,6 @@ install:
1313
test_script:
1414
- node --version
1515
- npm --version
16-
- npm run ci
16+
- npm run test
1717

1818
build: off

lib/_http_agent.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ function Agent(options) {
119119
socket.once('error', freeSocketErrorListener);
120120
}
121121
// set free keepalive timer
122-
socket.setTimeout(self.freeSocketKeepAliveTimeout);
122+
// try to use socket custom freeSocketKeepAliveTimeout first
123+
const freeSocketKeepAliveTimeout = socket.freeSocketKeepAliveTimeout || self.freeSocketKeepAliveTimeout;
124+
socket.setTimeout(freeSocketKeepAliveTimeout);
125+
debug(`push to free socket queue and wait for ${freeSocketKeepAliveTimeout}ms`);
123126
// [patch end]
124127
}
125128
} else {
@@ -179,13 +182,14 @@ function handleSocketCreation(req) {
179182
}
180183
// [patch end]
181184

182-
Agent.prototype.addRequest = function addRequest(req, options) {
185+
Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
186+
localAddress/*legacy*/) {
183187
// Legacy API: addRequest(req, host, port, localAddress)
184188
if (typeof options === 'string') {
185189
options = {
186190
host: options,
187-
port: arguments[2],
188-
localAddress: arguments[3]
191+
port,
192+
localAddress
189193
};
190194
}
191195

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"node": ">= 4.0.0"
4646
},
4747
"ci": {
48-
"version": "4.3.2, 4, 6, 7, 8"
48+
"version": "4.3.2, 4, 6, 8, 9"
4949
},
5050
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)",
5151
"license": "MIT"

test/server_timeout.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const http = require('http');
5+
const Agent = require('..');
6+
7+
describe('test/server_timeout.test.js', () => {
8+
let port;
9+
let server;
10+
before(done => {
11+
server = http.createServer((req, res) => {
12+
if (server.keepAliveTimeout) {
13+
res.setHeader('Keep-Alive', `timeout=${parseInt(server.keepAliveTimeout / 1000)}`);
14+
}
15+
res.end('Hello World, ' + req.connection.remotePort);
16+
});
17+
server.on('clientError', (err, socket) => {
18+
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
19+
});
20+
server.keepAliveTimeout = 1000;
21+
server.listen(0, err => {
22+
port = server.address().port;
23+
done(err);
24+
});
25+
});
26+
27+
it('should handle Keep-Alive header and not throw reset error', done => {
28+
const keepaliveAgent = new Agent({
29+
keepAlive: true,
30+
});
31+
32+
let count = 0;
33+
function request() {
34+
count++;
35+
const req = http.request({
36+
method: 'GET',
37+
port,
38+
path: '/',
39+
agent: keepaliveAgent,
40+
}, res => {
41+
assert(res.statusCode === 200);
42+
const chunks = [];
43+
res.on('data', data => {
44+
chunks.push(data);
45+
});
46+
res.on('end', () => {
47+
const text = Buffer.concat(chunks).toString();
48+
console.log('[%s] status: %s, text: %s, headers: %j', count, text, res.statusCode, res.headers);
49+
assert(res.headers.connection === 'keep-alive');
50+
assert(res.headers['keep-alive'] === 'timeout=1');
51+
const m = /^timeout=(\d+?)/.exec(res.headers['keep-alive']);
52+
if (m) {
53+
const keepAliveTimeout = parseInt(m[1]) * 1000 - 500;
54+
if (keepAliveTimeout > 0) {
55+
res.socket.freeSocketKeepAliveTimeout = keepAliveTimeout;
56+
}
57+
}
58+
if (count > 5) {
59+
done();
60+
}
61+
});
62+
});
63+
req.on('error', err => {
64+
console.error('[%s] error: %s', count, err);
65+
done(err);
66+
});
67+
req.end();
68+
}
69+
70+
setInterval(request, server.keepAliveTimeout);
71+
request();
72+
});
73+
});

0 commit comments

Comments
 (0)