diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2010-05-12 10:01:27 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-05-12 10:06:13 -0700 |
commit | d2cff34fa3db99dfd1ff622662964600d295325b (patch) | |
tree | 5bf94a4fa430792f3bf2192a25723c5f15090703 | |
parent | f7ff548dd01c18f14e6f23c5d6ab628e00c429da (diff) | |
download | nodejs-d2cff34fa3db99dfd1ff622662964600d295325b.tar.gz nodejs-d2cff34fa3db99dfd1ff622662964600d295325b.tar.bz2 nodejs-d2cff34fa3db99dfd1ff622662964600d295325b.zip |
Idle timeout changes
- setTimeout should active the timeout too. (test-net-set-timeout tests
this.)
- 'timeout' event is not automatically followed by an 'error' event. That
is the user is now responsible for destroying the stream if there is an
idle timeout.
-rw-r--r-- | doc/api.markdown | 16 | ||||
-rw-r--r-- | lib/net.js | 6 | ||||
-rw-r--r-- | test/pummel/test-tcp-timeout.js | 11 | ||||
-rw-r--r-- | test/simple/test-net-set-timeout.js | 39 |
4 files changed, 57 insertions, 15 deletions
diff --git a/doc/api.markdown b/doc/api.markdown index 2660f9503..0365bf671 100644 --- a/doc/api.markdown +++ b/doc/api.markdown @@ -2124,8 +2124,11 @@ call `stream.end()` when this event is emitted. `function () { }` -Emitted if the stream times out from inactivity. The -`'close'` event will be emitted immediately following this event. +Emitted if the stream times out from inactivity. This is only to notify that +the stream has been idle. The user must manually close the connection. + +See also: `stream.setTimeout()` + ### Event: 'drain' @@ -2233,10 +2236,13 @@ Resumes reading after a call to `pause()`. ### stream.setTimeout(timeout) Sets the stream to timeout after `timeout` milliseconds of inactivity on -the stream. By default all `net.Stream` objects have a timeout of 60 -seconds (60000 ms). +the stream. By default `net.Stream` do not have a timeout. + +When an idle timeout is triggered the stream will receive a `'timeout'` +event but the connection will not be severed. The user must manually `end()` +or `destroy()` the stream. -If `timeout` is 0, then the idle timeout is disabled. +If `timeout` is 0, then the existing idle timeout is disabled. ### stream.setNoDelay(noDelay=true) diff --git a/lib/net.js b/lib/net.js index 1daf841ed..4b1450c8a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -146,7 +146,6 @@ var timeout = new (function () { remove(first); assert(first != peek(list)); first.emit('timeout'); - first.destroy(new Error('idle timeout')); } } debug(msecs + ' list empty'); @@ -816,7 +815,10 @@ Stream.prototype.setKeepAlive = function (enable, time) { }; Stream.prototype.setTimeout = function (msecs) { - timeout.enroll(this, msecs); + if (msecs > 0) { + timeout.enroll(this, msecs); + if (this.fd) { timeout.active(this); } + } }; diff --git a/test/pummel/test-tcp-timeout.js b/test/pummel/test-tcp-timeout.js index 1b1fc0ca8..d7626db9d 100644 --- a/test/pummel/test-tcp-timeout.js +++ b/test/pummel/test-tcp-timeout.js @@ -5,8 +5,6 @@ starttime = null; timeouttime = null; timeout = 1000; -gotError = false - var echo_server = net.createServer(function (socket) { socket.setTimeout(timeout); @@ -14,11 +12,11 @@ var echo_server = net.createServer(function (socket) { puts("server timeout"); timeouttime = new Date; p(timeouttime); + socket.destroy(); }); socket.addListener("error", function (e) { - assert.ok(e instanceof Error); - gotError = true; + throw new Error("Server side socket should not get error. We disconnect willingly."); }) socket.addListener("data", function (d) { @@ -59,8 +57,7 @@ client.addListener("data", function (chunk) { }); client.addListener("timeout", function () { - puts("client timeout - this shouldn't happen"); - assert.ok(false); + throw new Error("client timeout - this shouldn't happen"); }); client.addListener("end", function () { @@ -84,6 +81,4 @@ process.addListener("exit", function () { // Allow for 800 milliseconds more assert.ok(diff < timeout + 800); - - assert.ok(gotError); }); diff --git a/test/simple/test-net-set-timeout.js b/test/simple/test-net-set-timeout.js new file mode 100644 index 000000000..a7dd86ad8 --- /dev/null +++ b/test/simple/test-net-set-timeout.js @@ -0,0 +1,39 @@ +require("../common"); +var sys = require('sys'), + http = require('http'); + +server = http.createServer(function (req, res) { + sys.puts('got request. setting 1 second timeout'); + req.connection.setTimeout(500); + + 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(); + }); +}); +server.listen(8000); + + +server.addListener('listening', function () { + sys.puts('Server running at http://127.0.0.1:8000/'); + + errorTimer =setTimeout(function () { + throw new Error('Timeout was not sucessful'); + }, 2000); + + 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); + }); +}); |