Default to 2 second timeout for http servers
authorRyan Dahl <ry@tinyclouds.org>
Wed, 12 May 2010 18:42:01 +0000 (11:42 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 12 May 2010 18:49:28 +0000 (11:49 -0700)
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
test/simple/test-http-set-timeout.js [moved from test/simple/test-net-set-timeout.js with 73% similarity]

index 3f348ad..633bb86 100644 (file)
@@ -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();
 };
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 (file)
@@ -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);
   });
 });