Permalink
Browse files

Default to 2 second timeout for http servers

Taking a performance hit on 'hello world' benchmark by enabling this by
default, but I think it's worth it. Hopefully we can improve performance by
resetting the timeout less often - ideally a 'hello world' benchmark would
only touch the one timer once - if it runs in less than 2 seconds. The rest
should be just link list manipulations.
1 parent ed0c109 commit 7a2e6d674a94e01a17e856b4d51ec229fad9af51 @ry ry committed May 12, 2010
Showing with 25 additions and 14 deletions.
  1. +25 −3 lib/http.js
  2. +0 −11 test/simple/{test-net-set-timeout.js → test-http-set-timeout.js}
View
@@ -552,6 +552,11 @@ function connectionListener (socket) {
// we need to keep track of the order they were sent.
var responses = [];
+ socket.setTimeout(2*60*1000); // 2 minute timeout
+ socket.addListener('timeout', function () {
+ socket.destroy();
+ });
+
var parser = parsers.alloc();
parser.reinitialize('request');
parser.socket = socket;
@@ -817,22 +822,39 @@ exports.cat = function (url, encoding_, headers_) {
client.https = true;
}
+ var callbackSent = false;
+
req.addListener('response', function (res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
- if (callback) callback(res.statusCode);
+ if (callback && !callbackSent) {
+ callback(res.statusCode);
+ callbackSent = true;
+ }
client.end();
return;
}
res.setBodyEncoding(encoding);
res.addListener('data', function (chunk) { content += chunk; });
res.addListener('end', function () {
- if (callback) callback(null, content);
+ if (callback && !callbackSent) {
+ callback(null, content);
+ callbackSent = true;
+ }
});
});
client.addListener("error", function (err) {
- if (callback) callback(err);
+ if (callback && !callbackSent) {
+ callback(err);
+ callbackSent = true;
+ }
});
+ client.addListener("close", function () {
+ if (callback && !callbackSent) {
+ callback(new Error('Connection closed unexpectedly'));
+ callbackSent = true;
+ }
+ });
req.end();
};
@@ -8,15 +8,6 @@ server = http.createServer(function (req, res) {
req.connection.addListener('timeout', function(){
sys.debug("TIMEOUT");
-
- var body="timeout\n";
- res.writeHead(200, {
- 'Content-Type': 'text/plain',
- 'Content-Length': body.length,
- 'Connection':'close'
- });
- res.end(body);
- req.connection.end();
server.close();
});
});
@@ -32,8 +23,6 @@ server.addListener('listening', function () {
http.cat('http://localhost:8000/', 'utf8', function (err, content) {
clearTimeout(errorTimer);
- if (err) throw err;
sys.puts('HTTP REQUEST COMPLETE (this is good)');
- sys.puts(content);
});
});

2 comments on commit 7a2e6d6

@tj
tj commented on 7a2e6d6 May 12, 2010

"2 second timeout" s/second/minute/ ? :D

@ry
ry commented on 7a2e6d6 May 12, 2010

yes, sorry

Please sign in to comment.