From 7a2e6d674a94e01a17e856b4d51ec229fad9af51 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 12 May 2010 11:42:01 -0700 Subject: [PATCH] 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. --- lib/http.js | 28 +++++++++++++++++++--- ...net-set-timeout.js => test-http-set-timeout.js} | 11 --------- 2 files changed, 25 insertions(+), 14 deletions(-) rename test/simple/{test-net-set-timeout.js => test-http-set-timeout.js} (73%) diff --git a/lib/http.js b/lib/http.js index 3f348ad..633bb86 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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(); }; diff --git a/test/simple/test-net-set-timeout.js b/test/simple/test-http-set-timeout.js similarity index 73% rename from test/simple/test-net-set-timeout.js rename to test/simple/test-http-set-timeout.js index a7dd86a..0bc816b 100644 --- a/test/simple/test-net-set-timeout.js +++ b/test/simple/test-http-set-timeout.js @@ -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.7.4