2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const Timer = process.binding('timer_wrap').Timer;
|
|
|
|
const L = require('_linklist');
|
|
|
|
const assert = require('assert').ok;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const kOnTimeout = Timer.kOnTimeout | 0;
|
2013-08-15 17:23:36 +00:00
|
|
|
|
2011-07-11 22:30:24 +00:00
|
|
|
// Timeout values > TIMEOUT_MAX are set to 1.
|
2015-01-21 16:36:59 +00:00
|
|
|
const TIMEOUT_MAX = 2147483647; // 2^31-1
|
2011-07-11 22:30:24 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const debug = require('util').debuglog('timer');
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
|
2011-01-13 10:09:03 +00:00
|
|
|
// IDLE TIMEOUTS
|
|
|
|
//
|
|
|
|
// Because often many sockets will have the same idle timeout we will not
|
|
|
|
// 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
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Object containing all lists, timers
|
|
|
|
// key = time in milliseconds
|
|
|
|
// value = list
|
|
|
|
var lists = {};
|
2010-10-26 18:56:32 +00:00
|
|
|
|
|
|
|
// the main function - creates lists on demand and the watchers associated
|
|
|
|
// with them.
|
2010-12-02 04:59:06 +00:00
|
|
|
function insert(item, msecs) {
|
2013-05-22 18:32:54 +00:00
|
|
|
item._idleStart = Timer.now();
|
2010-10-26 19:14:17 +00:00
|
|
|
item._idleTimeout = msecs;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
if (msecs < 0) return;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
|
|
|
var list;
|
|
|
|
|
|
|
|
if (lists[msecs]) {
|
|
|
|
list = lists[msecs];
|
|
|
|
} else {
|
|
|
|
list = new Timer();
|
2011-06-08 02:35:50 +00:00
|
|
|
list.start(msecs, 0);
|
|
|
|
|
2011-01-18 22:26:32 +00:00
|
|
|
L.init(list);
|
2010-10-26 18:56:32 +00:00
|
|
|
|
|
|
|
lists[msecs] = list;
|
2012-12-27 19:40:42 +00:00
|
|
|
list.msecs = msecs;
|
2013-08-15 17:23:36 +00:00
|
|
|
list[kOnTimeout] = listOnTimeout;
|
2012-12-27 19:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
L.append(list, item);
|
|
|
|
assert(!L.isEmpty(list)); // list is not empty
|
|
|
|
}
|
|
|
|
|
|
|
|
function listOnTimeout() {
|
|
|
|
var msecs = this.msecs;
|
|
|
|
var list = this;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('timeout callback %d', msecs);
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2013-05-22 18:32:54 +00:00
|
|
|
var now = Timer.now();
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('now: %s', now);
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2014-12-09 15:01:05 +00:00
|
|
|
var diff, first, threw;
|
2012-12-27 19:40:42 +00:00
|
|
|
while (first = L.peek(list)) {
|
2013-09-24 21:12:11 +00:00
|
|
|
diff = now - first._idleStart;
|
2012-10-26 05:49:22 +00:00
|
|
|
if (diff < msecs) {
|
2012-12-27 19:40:42 +00:00
|
|
|
list.start(msecs - diff, 0);
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('%d list wait because diff is %d', msecs, diff);
|
2012-12-27 19:40:42 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
L.remove(first);
|
|
|
|
assert(first !== L.peek(list));
|
|
|
|
|
|
|
|
if (!first._onTimeout) continue;
|
|
|
|
|
|
|
|
// v0.4 compatibility: if the timer callback throws and the
|
|
|
|
// domain or uncaughtException handler ignore the exception,
|
|
|
|
// other timers that expire on this tick should still run.
|
|
|
|
//
|
|
|
|
// https://github.com/joyent/node/issues/2631
|
2014-01-09 19:11:40 +00:00
|
|
|
var domain = first.domain;
|
|
|
|
if (domain && domain._disposed)
|
2013-09-24 21:12:11 +00:00
|
|
|
continue;
|
|
|
|
|
2012-12-27 19:40:42 +00:00
|
|
|
try {
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.enter();
|
2013-09-24 21:12:11 +00:00
|
|
|
threw = true;
|
2012-12-27 19:40:42 +00:00
|
|
|
first._onTimeout();
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2012-12-27 19:40:42 +00:00
|
|
|
threw = false;
|
|
|
|
} finally {
|
|
|
|
if (threw) {
|
2014-01-29 01:36:22 +00:00
|
|
|
// We need to continue processing after domain error handling
|
|
|
|
// is complete, but not by using whatever domain was left over
|
|
|
|
// when the timeout threw its exception.
|
|
|
|
var oldDomain = process.domain;
|
|
|
|
process.domain = null;
|
2012-12-27 19:40:42 +00:00
|
|
|
process.nextTick(function() {
|
2013-08-15 17:23:36 +00:00
|
|
|
list[kOnTimeout]();
|
2012-12-27 19:40:42 +00:00
|
|
|
});
|
2014-01-29 01:36:22 +00:00
|
|
|
process.domain = oldDomain;
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-27 19:40:42 +00:00
|
|
|
}
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('%d list empty', msecs);
|
2012-12-27 19:40:42 +00:00
|
|
|
assert(L.isEmpty(list));
|
|
|
|
list.close();
|
|
|
|
delete lists[msecs];
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const unenroll = exports.unenroll = function(item) {
|
2011-01-18 22:26:32 +00:00
|
|
|
L.remove(item);
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2011-01-13 10:22:09 +00:00
|
|
|
var list = lists[item._idleTimeout];
|
|
|
|
// if empty then stop the watcher
|
|
|
|
debug('unenroll');
|
2011-01-18 22:26:32 +00:00
|
|
|
if (list && L.isEmpty(list)) {
|
2011-01-13 10:22:09 +00:00
|
|
|
debug('unenroll: list empty');
|
2011-06-08 02:35:50 +00:00
|
|
|
list.close();
|
|
|
|
delete lists[item._idleTimeout];
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
2011-12-22 13:42:20 +00:00
|
|
|
// if active is called later, then we want to make sure not to insert again
|
|
|
|
item._idleTimeout = -1;
|
2010-10-26 18:56:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Does not start the time, just sets up the members needed.
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.enroll = function(item, msecs) {
|
2010-10-26 19:14:17 +00:00
|
|
|
// if this item was already in a list somewhere
|
2010-10-26 18:56:32 +00:00
|
|
|
// then we should unenroll it from that
|
2010-10-26 19:14:17 +00:00
|
|
|
if (item._idleNext) unenroll(item);
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2013-03-21 17:38:30 +00:00
|
|
|
// Ensure that msecs fits into signed int32
|
|
|
|
if (msecs > 0x7fffffff) {
|
|
|
|
msecs = 0x7fffffff;
|
|
|
|
}
|
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
item._idleTimeout = msecs;
|
2011-01-18 22:26:32 +00:00
|
|
|
L.init(item);
|
2010-10-26 18:56:32 +00:00
|
|
|
};
|
|
|
|
|
2011-01-13 10:22:09 +00:00
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
// call this whenever the item is active (not idle)
|
2010-10-26 18:56:32 +00:00
|
|
|
// it will reset its timeout.
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.active = function(item) {
|
2010-10-26 19:14:17 +00:00
|
|
|
var msecs = item._idleTimeout;
|
2010-10-26 19:52:31 +00:00
|
|
|
if (msecs >= 0) {
|
2010-10-26 18:56:32 +00:00
|
|
|
var list = lists[msecs];
|
2011-07-01 19:01:12 +00:00
|
|
|
if (!list || L.isEmpty(list)) {
|
2010-10-26 19:14:17 +00:00
|
|
|
insert(item, msecs);
|
2010-10-26 18:56:32 +00:00
|
|
|
} else {
|
2013-05-22 18:32:54 +00:00
|
|
|
item._idleStart = Timer.now();
|
2011-01-18 22:26:32 +00:00
|
|
|
L.append(list, item);
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-10-26 19:14:17 +00:00
|
|
|
|
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
/*
|
|
|
|
* DOM-style timers
|
|
|
|
*/
|
2010-10-26 19:14:17 +00:00
|
|
|
|
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.setTimeout = function(callback, after) {
|
2011-11-12 12:31:26 +00:00
|
|
|
var timer;
|
2010-10-26 19:14:17 +00:00
|
|
|
|
2012-07-01 20:58:22 +00:00
|
|
|
after *= 1; // coalesce to number or NaN
|
|
|
|
|
|
|
|
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
|
2011-07-11 22:30:24 +00:00
|
|
|
after = 1; // schedule on next tick, follows browser behaviour
|
|
|
|
}
|
2011-06-08 02:35:50 +00:00
|
|
|
|
2012-07-13 02:19:01 +00:00
|
|
|
timer = new Timeout(after);
|
2011-06-08 02:35:50 +00:00
|
|
|
|
2011-07-11 22:30:24 +00:00
|
|
|
if (arguments.length <= 2) {
|
|
|
|
timer._onTimeout = callback;
|
2010-10-26 19:52:31 +00:00
|
|
|
} else {
|
2011-07-11 22:30:24 +00:00
|
|
|
/*
|
|
|
|
* Sometimes setTimeout is called with arguments, EG
|
|
|
|
*
|
|
|
|
* setTimeout(callback, 2000, "hello", "world")
|
|
|
|
*
|
|
|
|
* If that's the case we need to call the callback with
|
|
|
|
* those args. The overhead of an extra closure is not
|
|
|
|
* desired in the normal case.
|
|
|
|
*/
|
|
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
|
|
|
timer._onTimeout = function() {
|
|
|
|
callback.apply(timer, args);
|
2015-01-15 17:55:29 +00:00
|
|
|
};
|
2010-10-26 19:52:31 +00:00
|
|
|
}
|
2010-10-26 19:14:17 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
if (process.domain) timer.domain = process.domain;
|
|
|
|
|
2011-07-11 22:30:24 +00:00
|
|
|
exports.active(timer);
|
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
return timer;
|
|
|
|
};
|
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.clearTimeout = function(timer) {
|
2013-08-15 17:23:36 +00:00
|
|
|
if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
|
|
|
|
timer[kOnTimeout] = timer._onTimeout = null;
|
2013-03-21 11:19:03 +00:00
|
|
|
if (timer instanceof Timeout) {
|
2011-06-08 02:35:50 +00:00
|
|
|
timer.close(); // for after === 0
|
|
|
|
} else {
|
|
|
|
exports.unenroll(timer);
|
|
|
|
}
|
2010-10-29 07:00:43 +00:00
|
|
|
}
|
2010-10-26 19:52:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.setInterval = function(callback, repeat) {
|
2012-07-01 20:58:22 +00:00
|
|
|
repeat *= 1; // coalesce to number or NaN
|
|
|
|
|
|
|
|
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
|
2011-07-11 22:30:24 +00:00
|
|
|
repeat = 1; // schedule on next tick, follows browser behaviour
|
|
|
|
}
|
|
|
|
|
2013-03-21 11:19:03 +00:00
|
|
|
var timer = new Timeout(repeat);
|
2011-06-08 02:35:50 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
2013-03-21 11:19:03 +00:00
|
|
|
timer._onTimeout = wrapper;
|
|
|
|
timer._repeat = true;
|
|
|
|
|
|
|
|
if (process.domain) timer.domain = process.domain;
|
|
|
|
exports.active(timer);
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
return timer;
|
2013-03-21 11:19:03 +00:00
|
|
|
|
|
|
|
function wrapper() {
|
|
|
|
callback.apply(this, args);
|
|
|
|
// If callback called clearInterval().
|
|
|
|
if (timer._repeat === false) return;
|
|
|
|
// If timer is unref'd (or was - it's permanently removed from the list.)
|
|
|
|
if (this._handle) {
|
|
|
|
this._handle.start(repeat, 0);
|
|
|
|
} else {
|
|
|
|
timer._idleTimeout = repeat;
|
|
|
|
exports.active(timer);
|
|
|
|
}
|
|
|
|
}
|
2010-10-26 19:14:17 +00:00
|
|
|
};
|
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.clearInterval = function(timer) {
|
2013-03-21 11:19:03 +00:00
|
|
|
if (timer && timer._repeat) {
|
|
|
|
timer._repeat = false;
|
|
|
|
clearTimeout(timer);
|
2010-10-26 19:14:17 +00:00
|
|
|
}
|
|
|
|
};
|
2012-07-13 02:19:01 +00:00
|
|
|
|
2013-03-21 11:19:03 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const Timeout = function(after) {
|
2012-07-13 02:19:01 +00:00
|
|
|
this._idleTimeout = after;
|
|
|
|
this._idlePrev = this;
|
|
|
|
this._idleNext = this;
|
2012-10-25 04:53:35 +00:00
|
|
|
this._idleStart = null;
|
|
|
|
this._onTimeout = null;
|
2013-03-21 11:19:03 +00:00
|
|
|
this._repeat = false;
|
2012-07-13 02:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.unref = function() {
|
2014-12-15 16:25:31 +00:00
|
|
|
if (this._handle) {
|
|
|
|
this._handle.unref();
|
|
|
|
} else if (typeof(this._onTimeout) === 'function') {
|
2013-05-22 18:32:54 +00:00
|
|
|
var now = Timer.now();
|
2012-10-25 04:53:35 +00:00
|
|
|
if (!this._idleStart) this._idleStart = now;
|
|
|
|
var delay = this._idleStart + this._idleTimeout - now;
|
2012-08-17 12:11:33 +00:00
|
|
|
if (delay < 0) delay = 0;
|
2012-07-13 02:19:01 +00:00
|
|
|
exports.unenroll(this);
|
|
|
|
this._handle = new Timer();
|
2013-08-15 17:23:36 +00:00
|
|
|
this._handle[kOnTimeout] = this._onTimeout;
|
2012-08-17 12:11:33 +00:00
|
|
|
this._handle.start(delay, 0);
|
2012-08-11 22:31:44 +00:00
|
|
|
this._handle.domain = this.domain;
|
2012-07-13 02:19:01 +00:00
|
|
|
this._handle.unref();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.ref = function() {
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.ref();
|
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.close = function() {
|
|
|
|
this._onTimeout = null;
|
|
|
|
if (this._handle) {
|
2013-08-15 17:23:36 +00:00
|
|
|
this._handle[kOnTimeout] = null;
|
2012-07-13 02:19:01 +00:00
|
|
|
this._handle.close();
|
|
|
|
} else {
|
|
|
|
exports.unenroll(this);
|
|
|
|
}
|
|
|
|
};
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
var immediateQueue = {};
|
2012-08-08 02:12:01 +00:00
|
|
|
L.init(immediateQueue);
|
|
|
|
|
|
|
|
|
|
|
|
function processImmediate() {
|
2013-07-11 22:58:11 +00:00
|
|
|
var queue = immediateQueue;
|
2014-12-09 15:01:05 +00:00
|
|
|
var domain, immediate;
|
2013-02-06 02:13:02 +00:00
|
|
|
|
2013-07-11 22:58:11 +00:00
|
|
|
immediateQueue = {};
|
|
|
|
L.init(immediateQueue);
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2013-07-11 22:58:11 +00:00
|
|
|
while (L.isEmpty(queue) === false) {
|
2013-09-24 21:12:11 +00:00
|
|
|
immediate = L.shift(queue);
|
2014-01-09 19:11:40 +00:00
|
|
|
domain = immediate.domain;
|
2013-09-24 21:12:11 +00:00
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.enter();
|
2013-09-24 21:12:11 +00:00
|
|
|
|
|
|
|
var threw = true;
|
|
|
|
try {
|
|
|
|
immediate._onImmediate();
|
|
|
|
threw = false;
|
|
|
|
} finally {
|
|
|
|
if (threw) {
|
|
|
|
if (!L.isEmpty(queue)) {
|
|
|
|
// Handle any remaining on next tick, assuming we're still
|
|
|
|
// alive to do so.
|
|
|
|
while (!L.isEmpty(immediateQueue)) {
|
|
|
|
L.append(queue, L.shift(immediateQueue));
|
|
|
|
}
|
|
|
|
immediateQueue = queue;
|
|
|
|
process.nextTick(processImmediate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2013-07-11 22:58:11 +00:00
|
|
|
}
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2013-07-11 22:58:11 +00:00
|
|
|
// Only round-trip to C++ land if we have to. Calling clearImmediate() on an
|
|
|
|
// immediate that's in |queue| is okay. Worst case is we make a superfluous
|
|
|
|
// call to NeedImmediateCallbackSetter().
|
|
|
|
if (L.isEmpty(immediateQueue)) {
|
|
|
|
process._needImmediateCallback = false;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
2012-08-27 19:51:25 +00:00
|
|
|
}
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
2013-10-07 19:39:52 +00:00
|
|
|
function Immediate() { }
|
|
|
|
|
|
|
|
Immediate.prototype.domain = undefined;
|
|
|
|
Immediate.prototype._onImmediate = undefined;
|
|
|
|
Immediate.prototype._idleNext = undefined;
|
|
|
|
Immediate.prototype._idlePrev = undefined;
|
|
|
|
|
|
|
|
|
2012-08-08 02:12:01 +00:00
|
|
|
exports.setImmediate = function(callback) {
|
2013-10-07 19:39:52 +00:00
|
|
|
var immediate = new Immediate();
|
2013-12-10 05:01:25 +00:00
|
|
|
var args, index;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
L.init(immediate);
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
immediate._onImmediate = callback;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
if (arguments.length > 1) {
|
2013-12-10 05:01:25 +00:00
|
|
|
args = [];
|
|
|
|
for (index = 1; index < arguments.length; index++)
|
|
|
|
args.push(arguments[index]);
|
2013-02-06 02:13:02 +00:00
|
|
|
|
|
|
|
immediate._onImmediate = function() {
|
2013-02-26 17:26:09 +00:00
|
|
|
callback.apply(immediate, args);
|
2012-08-08 02:12:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
if (!process._needImmediateCallback) {
|
|
|
|
process._needImmediateCallback = true;
|
|
|
|
process._immediateCallback = processImmediate;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 21:12:11 +00:00
|
|
|
if (process.domain)
|
|
|
|
immediate.domain = process.domain;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
L.append(immediateQueue, immediate);
|
|
|
|
|
|
|
|
return immediate;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.clearImmediate = function(immediate) {
|
|
|
|
if (!immediate) return;
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
immediate._onImmediate = undefined;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
L.remove(immediate);
|
|
|
|
|
|
|
|
if (L.isEmpty(immediateQueue)) {
|
2013-02-06 02:13:02 +00:00
|
|
|
process._needImmediateCallback = false;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
|
|
|
};
|
2013-05-17 22:04:24 +00:00
|
|
|
|
|
|
|
|
2014-05-06 12:05:32 +00:00
|
|
|
// Internal APIs that need timeouts should use timers._unrefActive instead of
|
2013-05-17 22:04:24 +00:00
|
|
|
// timers.active as internal timeouts shouldn't hold the loop open
|
|
|
|
|
|
|
|
var unrefList, unrefTimer;
|
|
|
|
|
|
|
|
|
|
|
|
function unrefTimeout() {
|
2013-07-08 01:20:07 +00:00
|
|
|
var now = Timer.now();
|
2013-05-17 22:04:24 +00:00
|
|
|
|
|
|
|
debug('unrefTimer fired');
|
|
|
|
|
2014-12-09 15:01:05 +00:00
|
|
|
var diff, domain, first, threw;
|
2013-05-17 22:04:24 +00:00
|
|
|
while (first = L.peek(unrefList)) {
|
2013-09-24 21:12:11 +00:00
|
|
|
diff = now - first._idleStart;
|
2013-05-17 22:04:24 +00:00
|
|
|
|
|
|
|
if (diff < first._idleTimeout) {
|
|
|
|
diff = first._idleTimeout - diff;
|
|
|
|
unrefTimer.start(diff, 0);
|
|
|
|
unrefTimer.when = now + diff;
|
|
|
|
debug('unrefTimer rescheudling for later');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
L.remove(first);
|
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
domain = first.domain;
|
|
|
|
|
2013-05-17 22:04:24 +00:00
|
|
|
if (!first._onTimeout) continue;
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain && domain._disposed) continue;
|
2013-05-17 22:04:24 +00:00
|
|
|
|
|
|
|
try {
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain) domain.enter();
|
2013-09-24 21:12:11 +00:00
|
|
|
threw = true;
|
2013-05-17 22:04:24 +00:00
|
|
|
debug('unreftimer firing timeout');
|
|
|
|
first._onTimeout();
|
|
|
|
threw = false;
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2013-05-17 22:04:24 +00:00
|
|
|
} finally {
|
|
|
|
if (threw) process.nextTick(unrefTimeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('unrefList is empty');
|
|
|
|
unrefTimer.when = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports._unrefActive = function(item) {
|
|
|
|
var msecs = item._idleTimeout;
|
|
|
|
if (!msecs || msecs < 0) return;
|
|
|
|
assert(msecs >= 0);
|
|
|
|
|
|
|
|
L.remove(item);
|
|
|
|
|
|
|
|
if (!unrefList) {
|
|
|
|
debug('unrefList initialized');
|
|
|
|
unrefList = {};
|
|
|
|
L.init(unrefList);
|
|
|
|
|
|
|
|
debug('unrefTimer initialized');
|
|
|
|
unrefTimer = new Timer();
|
|
|
|
unrefTimer.unref();
|
|
|
|
unrefTimer.when = -1;
|
2013-08-15 17:23:36 +00:00
|
|
|
unrefTimer[kOnTimeout] = unrefTimeout;
|
2013-05-17 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2013-07-08 01:20:07 +00:00
|
|
|
var now = Timer.now();
|
2013-05-17 22:04:24 +00:00
|
|
|
item._idleStart = now;
|
|
|
|
|
|
|
|
if (L.isEmpty(unrefList)) {
|
|
|
|
debug('unrefList empty');
|
|
|
|
L.append(unrefList, item);
|
|
|
|
|
|
|
|
unrefTimer.start(msecs, 0);
|
|
|
|
unrefTimer.when = now + msecs;
|
|
|
|
debug('unrefTimer scheduled');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var when = now + msecs;
|
|
|
|
|
|
|
|
debug('unrefList find where we can insert');
|
|
|
|
|
|
|
|
var cur, them;
|
|
|
|
|
|
|
|
for (cur = unrefList._idlePrev; cur != unrefList; cur = cur._idlePrev) {
|
|
|
|
them = cur._idleStart + cur._idleTimeout;
|
|
|
|
|
|
|
|
if (when < them) {
|
|
|
|
debug('unrefList inserting into middle of list');
|
|
|
|
|
|
|
|
L.append(cur, item);
|
|
|
|
|
|
|
|
if (unrefTimer.when > when) {
|
|
|
|
debug('unrefTimer is scheduled to fire too late, reschedule');
|
|
|
|
unrefTimer.start(msecs, 0);
|
|
|
|
unrefTimer.when = when;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('unrefList append to end');
|
|
|
|
L.append(unrefList, item);
|
|
|
|
};
|