Move setTimeout and friends into timers module
This commit is contained in:
parent
5cc29b80f2
commit
79944006e2
122
lib/timers.js
122
lib/timers.js
@ -14,7 +14,7 @@ function debug () {
|
|||||||
// IDLE TIMEOUTS
|
// IDLE TIMEOUTS
|
||||||
//
|
//
|
||||||
// Because often many sockets will have the same idle timeout we will not
|
// Because often many sockets will have the same idle timeout we will not
|
||||||
// use one timeout watcher per socket. It is too much overhead. Instead
|
// use one timeout watcher per item. It is too much overhead. Instead
|
||||||
// we'll use a single watcher for all sockets with the same timeout value
|
// we'll use a single watcher for all sockets with the same timeout value
|
||||||
// and a linked list. This technique is described in the libev manual:
|
// and a linked list. This technique is described in the libev manual:
|
||||||
// http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
|
// http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
|
||||||
@ -24,14 +24,14 @@ function debug () {
|
|||||||
// value = list
|
// value = list
|
||||||
var lists = {};
|
var lists = {};
|
||||||
|
|
||||||
// show the most idle socket
|
// show the most idle item
|
||||||
function peek (list) {
|
function peek (list) {
|
||||||
if (list._idlePrev == list) return null;
|
if (list._idlePrev == list) return null;
|
||||||
return list._idlePrev;
|
return list._idlePrev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// remove the most idle socket from the list
|
// remove the most idle item from the list
|
||||||
function shift (list) {
|
function shift (list) {
|
||||||
var first = list._idlePrev;
|
var first = list._idlePrev;
|
||||||
remove(first);
|
remove(first);
|
||||||
@ -39,28 +39,28 @@ function shift (list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// remove a socket from its list
|
// remove a item from its list
|
||||||
function remove (socket) {
|
function remove (item) {
|
||||||
socket._idleNext._idlePrev = socket._idlePrev;
|
item._idleNext._idlePrev = item._idlePrev;
|
||||||
socket._idlePrev._idleNext = socket._idleNext;
|
item._idlePrev._idleNext = item._idleNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// remove a socket from its list and place at the end.
|
// remove a item from its list and place at the end.
|
||||||
function append (list, socket) {
|
function append (list, item) {
|
||||||
remove(socket);
|
remove(item);
|
||||||
socket._idleNext = list._idleNext;
|
item._idleNext = list._idleNext;
|
||||||
socket._idleNext._idlePrev = socket;
|
item._idleNext._idlePrev = item;
|
||||||
socket._idlePrev = list;
|
item._idlePrev = list;
|
||||||
list._idleNext = socket;
|
list._idleNext = item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// the main function - creates lists on demand and the watchers associated
|
// the main function - creates lists on demand and the watchers associated
|
||||||
// with them.
|
// with them.
|
||||||
function insert (socket, msecs) {
|
function insert (item, msecs) {
|
||||||
socket._idleStart = new Date();
|
item._idleStart = new Date();
|
||||||
socket._idleTimeout = msecs;
|
item._idleTimeout = msecs;
|
||||||
|
|
||||||
if (!msecs) return;
|
if (!msecs) return;
|
||||||
|
|
||||||
@ -105,17 +105,17 @@ function insert (socket, msecs) {
|
|||||||
list.again(msecs);
|
list.again(msecs);
|
||||||
}
|
}
|
||||||
|
|
||||||
append(list, socket);
|
append(list, item);
|
||||||
assert(list._idleNext != list); // list is not empty
|
assert(list._idleNext != list); // list is not empty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var unenroll = exports.unenroll = function (socket) {
|
var unenroll = exports.unenroll = function (item) {
|
||||||
if (socket._idleNext) {
|
if (item._idleNext) {
|
||||||
socket._idleNext._idlePrev = socket._idlePrev;
|
item._idleNext._idlePrev = item._idlePrev;
|
||||||
socket._idlePrev._idleNext = socket._idleNext;
|
item._idlePrev._idleNext = item._idleNext;
|
||||||
|
|
||||||
var list = lists[socket._idleTimeout];
|
var list = lists[item._idleTimeout];
|
||||||
// if empty then stop the watcher
|
// if empty then stop the watcher
|
||||||
//debug('unenroll');
|
//debug('unenroll');
|
||||||
if (list && list._idlePrev == list) {
|
if (list && list._idlePrev == list) {
|
||||||
@ -127,33 +127,73 @@ var unenroll = exports.unenroll = function (socket) {
|
|||||||
|
|
||||||
|
|
||||||
// Does not start the time, just sets up the members needed.
|
// Does not start the time, just sets up the members needed.
|
||||||
exports.enroll = function (socket, msecs) {
|
exports.enroll = function (item, msecs) {
|
||||||
// if this socket was already in a list somewhere
|
// if this item was already in a list somewhere
|
||||||
// then we should unenroll it from that
|
// then we should unenroll it from that
|
||||||
if (socket._idleNext) unenroll(socket);
|
if (item._idleNext) unenroll(item);
|
||||||
|
|
||||||
socket._idleTimeout = msecs;
|
item._idleTimeout = msecs;
|
||||||
socket._idleNext = socket;
|
item._idleNext = item;
|
||||||
socket._idlePrev = socket;
|
item._idlePrev = item;
|
||||||
};
|
};
|
||||||
|
|
||||||
// call this whenever the socket is active (not idle)
|
// call this whenever the item is active (not idle)
|
||||||
// it will reset its timeout.
|
// it will reset its timeout.
|
||||||
exports.active = function (socket) {
|
exports.active = function (item) {
|
||||||
var msecs = socket._idleTimeout;
|
var msecs = item._idleTimeout;
|
||||||
if (msecs) {
|
if (msecs) {
|
||||||
var list = lists[msecs];
|
var list = lists[msecs];
|
||||||
if (socket._idleNext == socket) {
|
if (item._idleNext == item) {
|
||||||
insert(socket, msecs);
|
insert(item, msecs);
|
||||||
} else {
|
} else {
|
||||||
// inline append
|
// inline append
|
||||||
socket._idleStart = new Date();
|
item._idleStart = new Date();
|
||||||
socket._idleNext._idlePrev = socket._idlePrev;
|
item._idleNext._idlePrev = item._idlePrev;
|
||||||
socket._idlePrev._idleNext = socket._idleNext;
|
item._idlePrev._idleNext = item._idleNext;
|
||||||
socket._idleNext = list._idleNext;
|
item._idleNext = list._idleNext;
|
||||||
socket._idleNext._idlePrev = socket;
|
item._idleNext._idlePrev = item;
|
||||||
socket._idlePrev = list;
|
item._idlePrev = list;
|
||||||
list._idleNext = socket;
|
list._idleNext = item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Timers
|
||||||
|
function addTimerListener (callback) {
|
||||||
|
var timer = this;
|
||||||
|
// Special case the no param case to avoid the extra object creation.
|
||||||
|
if (arguments.length > 2) {
|
||||||
|
var args = Array.prototype.slice.call(arguments, 2);
|
||||||
|
timer.callback = function () { callback.apply(timer, args); };
|
||||||
|
} else {
|
||||||
|
timer.callback = callback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
exports.setTimeout = function (callback, after) {
|
||||||
|
var timer = new Timer();
|
||||||
|
addTimerListener.apply(timer, arguments);
|
||||||
|
timer.start(after, 0);
|
||||||
|
return timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.setInterval = function (callback, repeat) {
|
||||||
|
var timer = new Timer();
|
||||||
|
addTimerListener.apply(timer, arguments);
|
||||||
|
timer.start(repeat, repeat ? repeat : 1);
|
||||||
|
return timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.clearTimeout = function (timer) {
|
||||||
|
if (timer instanceof Timer) {
|
||||||
|
timer.callback = null;
|
||||||
|
timer.stop();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.clearInterval = exports.clearTimeout;
|
||||||
|
45
src/node.js
45
src/node.js
@ -425,45 +425,26 @@ var constants; // lazy loaded.
|
|||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Timers
|
|
||||||
function addTimerListener (callback) {
|
|
||||||
var timer = this;
|
|
||||||
// Special case the no param case to avoid the extra object creation.
|
|
||||||
if (arguments.length > 2) {
|
|
||||||
var args = Array.prototype.slice.call(arguments, 2);
|
|
||||||
timer.callback = function () { callback.apply(timer, args); };
|
|
||||||
} else {
|
|
||||||
timer.callback = callback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var Timer; // lazy load
|
global.setTimeout = function () {
|
||||||
|
var t = module.requireNative('timers');
|
||||||
global.setTimeout = function (callback, after) {
|
return t.setTimeout.apply(this, arguments);
|
||||||
if (!Timer) Timer = process.binding("timer").Timer;
|
|
||||||
var timer = new Timer();
|
|
||||||
addTimerListener.apply(timer, arguments);
|
|
||||||
timer.start(after, 0);
|
|
||||||
return timer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
global.setInterval = function (callback, repeat) {
|
global.setInterval = function () {
|
||||||
if (!Timer) Timer = process.binding("timer").Timer;
|
var t = module.requireNative('timers');
|
||||||
var timer = new Timer();
|
return t.setInterval.apply(this, arguments);
|
||||||
addTimerListener.apply(timer, arguments);
|
|
||||||
timer.start(repeat, repeat ? repeat : 1);
|
|
||||||
return timer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
global.clearTimeout = function (timer) {
|
global.clearTimeout = function () {
|
||||||
if (!Timer) Timer = process.binding("timer").Timer;
|
var t = module.requireNative('timers');
|
||||||
if (timer instanceof Timer) {
|
return t.clearTimeout.apply(this, arguments);
|
||||||
timer.callback = null;
|
|
||||||
timer.stop();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
global.clearInterval = global.clearTimeout;
|
global.clearInterval = function () {
|
||||||
|
var t = module.requireNative('timers');
|
||||||
|
return t.clearInterval.apply(this, arguments);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
var stdout;
|
var stdout;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user