Fix idle timeouts

Remove process.now because it doesn't provide enough precision.
This commit is contained in:
Ryan Dahl 2010-03-23 20:54:16 -07:00
parent 3238944c7a
commit 8e9ec4abea
3 changed files with 18 additions and 30 deletions

View File

@ -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');

View File

@ -539,13 +539,6 @@ static Handle<Value> SetUid(const Arguments& args) {
return Undefined();
}
Handle<Value>
NowGetter (Local<String> property, const AccessorInfo& info)
{
HandleScope scope;
return scope.Close(Integer::New(ev_now(EV_DEFAULT_UC)));
}
v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
HandleScope scope;
@ -1234,8 +1227,6 @@ static void Load(int argc, char *argv[]) {
Local<FunctionTemplate> process_template = FunctionTemplate::New();
node::EventEmitter::Initialize(process_template);
process_template->InstanceTemplate()->SetAccessor(String::NewSymbol("now"), NowGetter, NULL);
process = Persistent<Object>::New(process_template->GetFunction()->NewInstance());
// Add a reference to the global object

View File

@ -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);
});