From 8e9ec4abea57544dd2889a56fc5dbf9fe763669b Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 23 Mar 2010 20:54:16 -0700 Subject: [PATCH] Fix idle timeouts Remove process.now because it doesn't provide enough precision. --- lib/net.js | 25 +++++++++++-------------- src/node.cc | 9 --------- test/pummel/test-tcp-timeout.js | 14 +++++++------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lib/net.js b/lib/net.js index 8bdece4..fc07465 100644 --- a/lib/net.js +++ b/lib/net.js @@ -2,8 +2,7 @@ var sys = require("sys"); var fs = require("fs"); var events = require("events"); -var debugLevel = 0; -if ('NODE_DEBUG' in process.ENV) debugLevel = 1; +var debugLevel = process.env['NODE_DEBUG'] ? 1 : 0; function debug () { if (debugLevel > 0) sys.error.apply(this, arguments); } @@ -102,7 +101,7 @@ var timeout = new (function () { // the main function - creates lists on demand and the watchers associated // with them. function insert (socket, msecs) { - socket._idleStart = process.now; + socket._idleStart = new Date(); socket._idleTimeout = msecs; if (!msecs) return; @@ -122,13 +121,14 @@ var timeout = new (function () { debug('timeout callback ' + msecs); // TODO - don't stop and start the watcher all the time. // just set its repeat - var now = process.now; + var now = new Date(); + debug("now: " + now); var first; while (first = peek(list)) { var diff = now - first._idleStart; if (diff < msecs) { list.again(msecs - diff); - debug(msecs + ' list wait'); + debug(msecs + ' list wait because diff is ' + diff); return; } else { remove(first); @@ -190,7 +190,7 @@ var timeout = new (function () { insert(socket, msecs); } else { // inline append - socket._idleStart = process.now; + socket._idleStart = new Date(); socket._idleNext._idlePrev = socket._idlePrev; socket._idlePrev._idleNext = socket._idleNext; socket._idleNext = list._idleNext; @@ -280,7 +280,7 @@ function initStream (self) { allocNewPool(); } - //debug('pool.used ' + pool.used); + debug('pool.used ' + pool.used); var bytesRead; try { @@ -293,7 +293,7 @@ function initStream (self) { return; } - //debug('bytesRead ' + bytesRead + '\n'); + debug('bytesRead ' + bytesRead + '\n'); if (bytesRead == 0) { self.readable = false; @@ -449,12 +449,12 @@ Stream.prototype._writeString = function (data, encoding) { buffer.used += bytesWritten; self._writeQueueSize += bytesWritten; - //debug('charsWritten ' + charsWritten); - //debug('buffer.used ' + buffer.used); + debug('charsWritten ' + charsWritten); + debug('buffer.used ' + buffer.used); // If we didn't finish, then recurse with the rest of the string. if (charsWritten < data.length) { - //debug('recursive write'); + debug('recursive write'); self._writeString(data.slice(charsWritten), encoding); } }; @@ -499,8 +499,6 @@ Stream.prototype.write = function (data, encoding) { encoding = (encoding || 'utf8').toLowerCase(); var bytes = Buffer.byteLength(data, encoding); - //debug('write string :' + JSON.stringify(data)); - if (!pool) allocNewPool(); if (pool.length - pool.used < bytes) { @@ -620,7 +618,6 @@ Stream.prototype.flush = function () { timeout.active(self); - if (bytesWritten === null) { // EAGAIN debug('write EAGAIN'); diff --git a/src/node.cc b/src/node.cc index 3fbc8e2..452c111 100644 --- a/src/node.cc +++ b/src/node.cc @@ -539,13 +539,6 @@ static Handle SetUid(const Arguments& args) { return Undefined(); } -Handle -NowGetter (Local property, const AccessorInfo& info) -{ - HandleScope scope; - return scope.Close(Integer::New(ev_now(EV_DEFAULT_UC))); -} - v8::Handle Exit(const v8::Arguments& args) { HandleScope scope; @@ -1234,8 +1227,6 @@ static void Load(int argc, char *argv[]) { Local process_template = FunctionTemplate::New(); node::EventEmitter::Initialize(process_template); - process_template->InstanceTemplate()->SetAccessor(String::NewSymbol("now"), NowGetter, NULL); - process = Persistent::New(process_template->GetFunction()->NewInstance()); // Add a reference to the global object diff --git a/test/pummel/test-tcp-timeout.js b/test/pummel/test-tcp-timeout.js index 3f7b8f3..fe82ab3 100644 --- a/test/pummel/test-tcp-timeout.js +++ b/test/pummel/test-tcp-timeout.js @@ -12,7 +12,6 @@ var echo_server = net.createServer(function (socket) { puts("server timeout"); timeouttime = new Date; p(timeouttime); - socket.forceClose(); }); socket.addListener("data", function (d) { @@ -62,19 +61,20 @@ client.addListener("end", function () { client.close(); }); -client.addListener("close", function (had_error) { +client.addListener("close", function () { puts("client disconnect"); echo_server.close(); - assert.equal(false, had_error); }); process.addListener("exit", function () { - assert.equal(true, starttime != null); - assert.equal(true, timeouttime != null); + assert.ok(starttime != null); + assert.ok(timeouttime != null); diff = timeouttime - starttime; puts("diff = " + diff); - assert.equal(true, timeout < diff); + + assert.ok(timeout < diff); + // Allow for 800 milliseconds more - assert.equal(true, diff < timeout + 800); + assert.ok(diff < timeout + 800); }); -- 2.7.4