Browse Source

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.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
7a2e6d674a
  1. 28
      lib/http.js
  2. 11
      test/simple/test-http-set-timeout.js

28
lib/http.js

@ -552,6 +552,11 @@ function connectionListener (socket) {
// we need to keep track of the order they were sent. // we need to keep track of the order they were sent.
var responses = []; var responses = [];
socket.setTimeout(2*60*1000); // 2 minute timeout
socket.addListener('timeout', function () {
socket.destroy();
});
var parser = parsers.alloc(); var parser = parsers.alloc();
parser.reinitialize('request'); parser.reinitialize('request');
parser.socket = socket; parser.socket = socket;
@ -817,22 +822,39 @@ exports.cat = function (url, encoding_, headers_) {
client.https = true; client.https = true;
} }
var callbackSent = false;
req.addListener('response', function (res) { req.addListener('response', function (res) {
if (res.statusCode < 200 || res.statusCode >= 300) { if (res.statusCode < 200 || res.statusCode >= 300) {
if (callback) callback(res.statusCode); if (callback && !callbackSent) {
callback(res.statusCode);
callbackSent = true;
}
client.end(); client.end();
return; return;
} }
res.setBodyEncoding(encoding); res.setBodyEncoding(encoding);
res.addListener('data', function (chunk) { content += chunk; }); res.addListener('data', function (chunk) { content += chunk; });
res.addListener('end', function () { res.addListener('end', function () {
if (callback) callback(null, content); if (callback && !callbackSent) {
callback(null, content);
callbackSent = true;
}
}); });
}); });
client.addListener("error", function (err) { 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(); req.end();
}; };

11
test/simple/test-net-set-timeout.js → test/simple/test-http-set-timeout.js

@ -8,15 +8,6 @@ server = http.createServer(function (req, res) {
req.connection.addListener('timeout', function(){ req.connection.addListener('timeout', function(){
sys.debug("TIMEOUT"); 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(); server.close();
}); });
}); });
@ -32,8 +23,6 @@ server.addListener('listening', function () {
http.cat('http://localhost:8000/', 'utf8', function (err, content) { http.cat('http://localhost:8000/', 'utf8', function (err, content) {
clearTimeout(errorTimer); clearTimeout(errorTimer);
if (err) throw err;
sys.puts('HTTP REQUEST COMPLETE (this is good)'); sys.puts('HTTP REQUEST COMPLETE (this is good)');
sys.puts(content);
}); });
}); });
Loading…
Cancel
Save