Skip to content

Commit 451ff15

Browse files
fredericgermainisaacs
authored andcommitted
http: Remove timeout handler when data arrives
1 parent 58a5bc1 commit 451ff15

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

lib/http.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,10 @@ function responseOnEnd() {
14741474
assert(!socket.writable);
14751475
} else {
14761476
debug('AGENT socket keep-alive');
1477+
if (req.timeoutCb) {
1478+
socket.setTimeout(0, req.timeoutCb);
1479+
req.timeoutCb = null;
1480+
}
14771481
socket.removeListener('close', socketCloseListener);
14781482
socket.removeListener('error', socketErrorListener);
14791483
socket.emit('free');
@@ -1554,6 +1558,9 @@ ClientRequest.prototype.setTimeout = function(msecs, callback) {
15541558
}
15551559

15561560
if (this.socket && this.socket.writable) {
1561+
if (this.timeoutCb)
1562+
this.socket.setTimeout(0, this.timeoutCb);
1563+
this.timeoutCb = emitTimeout;
15571564
this.socket.setTimeout(msecs, emitTimeout);
15581565
return;
15591566
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
26+
var request_number = 0;
27+
var requests_sent = 0;
28+
var requests_done = 0;
29+
var options = {
30+
method: 'GET',
31+
port: common.PORT,
32+
host: '127.0.0.1',
33+
};
34+
35+
//http.globalAgent.maxSockets = 15;
36+
37+
var server = http.createServer(function(req, res) {
38+
var m = /\/(.*)/.exec(req.url),
39+
reqid = parseInt(m[1], 10);
40+
if ( reqid % 2 ) {
41+
// do not reply the request
42+
} else {
43+
res.writeHead(200, {'Content-Type': 'text/plain'});
44+
res.write(reqid.toString());
45+
res.end();
46+
}
47+
request_number+=1;
48+
});
49+
50+
server.listen(options.port, options.host, function() {
51+
var req;
52+
53+
for (requests_sent = 0; requests_sent < 30; requests_sent+=1) {
54+
options.path = '/' + requests_sent;
55+
req = http.request(options);
56+
req.id = requests_sent;
57+
req.on('response', function(res) {
58+
res.on('data', function(data) {
59+
console.log('res#'+this.req.id+' data:'+data);
60+
});
61+
res.on('end', function(data) {
62+
console.log('res#'+this.req.id+' end');
63+
requests_done += 1;
64+
});
65+
});
66+
req.on('close', function() {
67+
console.log('req#'+this.id+' close');
68+
});
69+
req.on('error', function() {
70+
console.log('req#'+this.id+' error');
71+
this.destroy();
72+
});
73+
req.setTimeout(50, function () {
74+
var req = this;
75+
console.log('req#'+this.id + ' timeout');
76+
req.abort();
77+
requests_done += 1;
78+
});
79+
req.end();
80+
}
81+
setTimeout(function() {
82+
server.close();
83+
}, 150);
84+
});
85+
86+
process.on('exit', function() {
87+
console.error('done=%j sent=%j', requests_done, requests_sent);
88+
assert.ok(requests_done == requests_sent, 'timeout on http request called too much');
89+
});

0 commit comments

Comments
 (0)