mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 14:25:18 +00:00

Instead of exposing internals of async_hooks & async_wrap throughout the code base, create necessary helper methods within the internal async_hooks that allows easy usage by Node.js internals. This stops every single internal user of async_hooks from importing a ton of functions, constants and internal Aliased Buffers from C++ async_wrap. Adds functions initHooksExist, afterHooksExist, and destroyHooksExist to determine whether the related emit methods need to be triggered. Adds clearDefaultTriggerAsyncId and clearAsyncIdStack on the JS side as an alternative to always calling C++. Moves async_id_symbol and trigger_async_id_symbol to internal async_hooks as they are never used in C++. Renames newUid to newAsyncId for added clarity of its purpose. Adjusts usage throughout the codebase, as well as in a couple of tests. PR-URL: https://github.com/nodejs/node/pull/18720 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
147 lines
4.0 KiB
JavaScript
147 lines
4.0 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
getDefaultTriggerAsyncId,
|
|
newAsyncId,
|
|
initHooksExist,
|
|
emitInit
|
|
} = require('internal/async_hooks');
|
|
// Symbols for storing async id state.
|
|
const async_id_symbol = Symbol('asyncId');
|
|
const trigger_async_id_symbol = Symbol('triggerId');
|
|
|
|
const errors = require('internal/errors');
|
|
|
|
// Timeout values > TIMEOUT_MAX are set to 1.
|
|
const TIMEOUT_MAX = 2 ** 31 - 1;
|
|
|
|
const refreshFnSymbol = Symbol('refresh()');
|
|
const unrefedSymbol = Symbol('unrefed');
|
|
|
|
module.exports = {
|
|
TIMEOUT_MAX,
|
|
kTimeout: Symbol('timeout'), // For hiding Timeouts on other internals.
|
|
async_id_symbol,
|
|
trigger_async_id_symbol,
|
|
Timeout,
|
|
refreshFnSymbol,
|
|
setUnrefTimeout,
|
|
validateTimerDuration
|
|
};
|
|
|
|
var timers;
|
|
function getTimers() {
|
|
if (timers === undefined) {
|
|
timers = require('timers');
|
|
}
|
|
return timers;
|
|
}
|
|
|
|
// Timer constructor function.
|
|
// The entire prototype is defined in lib/timers.js
|
|
function Timeout(callback, after, args, isRepeat, isUnrefed) {
|
|
after *= 1; // coalesce to number or NaN
|
|
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
|
|
if (after > TIMEOUT_MAX) {
|
|
process.emitWarning(`${after} does not fit into` +
|
|
' a 32-bit signed integer.' +
|
|
'\nTimeout duration was set to 1.',
|
|
'TimeoutOverflowWarning');
|
|
}
|
|
after = 1; // schedule on next tick, follows browser behavior
|
|
}
|
|
|
|
this._called = false;
|
|
this._idleTimeout = after;
|
|
this._idlePrev = this;
|
|
this._idleNext = this;
|
|
this._idleStart = null;
|
|
// this must be set to null first to avoid function tracking
|
|
// on the hidden class, revisit in V8 versions after 6.2
|
|
this._onTimeout = null;
|
|
this._onTimeout = callback;
|
|
this._timerArgs = args;
|
|
this._repeat = isRepeat ? after : null;
|
|
this._destroyed = false;
|
|
|
|
this[unrefedSymbol] = isUnrefed;
|
|
|
|
this[async_id_symbol] = newAsyncId();
|
|
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
|
|
if (initHooksExist()) {
|
|
emitInit(this[async_id_symbol],
|
|
'Timeout',
|
|
this[trigger_async_id_symbol],
|
|
this);
|
|
}
|
|
}
|
|
|
|
Timeout.prototype[refreshFnSymbol] = function refresh() {
|
|
if (this._handle) {
|
|
// Would be more ideal with uv_timer_again(), however that API does not
|
|
// cause libuv's sorted timers data structure (a binary heap at the time
|
|
// of writing) to re-sort itself. This causes ordering inconsistencies.
|
|
this._handle.start(this._idleTimeout);
|
|
} else if (this[unrefedSymbol]) {
|
|
getTimers()._unrefActive(this);
|
|
} else {
|
|
getTimers().active(this);
|
|
}
|
|
};
|
|
|
|
function setUnrefTimeout(callback, after, arg1, arg2, arg3) {
|
|
// Type checking identical to setTimeout()
|
|
if (typeof callback !== 'function') {
|
|
throw new errors.TypeError('ERR_INVALID_CALLBACK');
|
|
}
|
|
|
|
let i, args;
|
|
switch (arguments.length) {
|
|
// fast cases
|
|
case 1:
|
|
case 2:
|
|
break;
|
|
case 3:
|
|
args = [arg1];
|
|
break;
|
|
case 4:
|
|
args = [arg1, arg2];
|
|
break;
|
|
default:
|
|
args = [arg1, arg2, arg3];
|
|
for (i = 5; i < arguments.length; i++) {
|
|
// extend array dynamically, makes .apply run much faster in v6.0.0
|
|
args[i - 2] = arguments[i];
|
|
}
|
|
break;
|
|
}
|
|
|
|
const timer = new Timeout(callback, after, args, false, true);
|
|
getTimers()._unrefActive(timer);
|
|
|
|
return timer;
|
|
}
|
|
|
|
// Type checking used by timers.enroll() and Socket#setTimeout()
|
|
function validateTimerDuration(msecs) {
|
|
if (typeof msecs !== 'number') {
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
|
|
'number', msecs);
|
|
}
|
|
|
|
if (msecs < 0 || !isFinite(msecs)) {
|
|
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'msecs',
|
|
'a non-negative finite number', msecs);
|
|
}
|
|
|
|
// Ensure that msecs fits into signed int32
|
|
if (msecs > TIMEOUT_MAX) {
|
|
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` +
|
|
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
|
|
'TimeoutOverflowWarning');
|
|
return TIMEOUT_MAX;
|
|
}
|
|
|
|
return msecs;
|
|
}
|