-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async_hooks: reduce code duplication by using a factory #13755
Changes from 3 commits
58f011f
4c00eee
8a8117e
8918fc8
e4cd110
795358a
fc397f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,9 @@ const init_symbol = Symbol('init'); | |
const before_symbol = Symbol('before'); | ||
const after_symbol = Symbol('after'); | ||
const destroy_symbol = Symbol('destroy'); | ||
const emitBeforeN = emitHookFactory(before_symbol); | ||
const emitAfterN = emitHookFactory(after_symbol); | ||
const emitDestroyN = emitHookFactory(destroy_symbol); | ||
|
||
// Setup the callbacks that node::AsyncWrap will call when there are hooks to | ||
// process. They use the same functions as the JS embedder API. These callbacks | ||
|
@@ -325,8 +328,8 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) { | |
triggerAsyncId = initTriggerId(); | ||
} | ||
|
||
// I'd prefer allowing these checks to not exist, or only throw in a debug | ||
// build, in order to improve performance. | ||
// TODO(trevnorris): I'd prefer allowing these checks to not exist, or only | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call changing this to a |
||
// throw in a debug build, in order to improve performance. | ||
if (!Number.isSafeInteger(asyncId) || asyncId < 0) | ||
throw new RangeError('asyncId must be an unsigned integer'); | ||
if (typeof type !== 'string' || type.length <= 0) | ||
|
@@ -342,24 +345,28 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) { | |
} | ||
} | ||
|
||
|
||
function emitBeforeN(asyncId) { | ||
processing_hook = true; | ||
// Use a single try/catch for all hook to avoid setting up one per iteration. | ||
try { | ||
for (var i = 0; i < active_hooks_array.length; i++) { | ||
if (typeof active_hooks_array[i][before_symbol] === 'function') { | ||
active_hooks_array[i][before_symbol](asyncId); | ||
function emitHookFactory(symbol) { | ||
// Called from native. The asyncId stack handling is taken care of there | ||
// before this is called. | ||
return function(asyncId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have we verified how this'll change the stack trace from any of these? If V8 can properly infer the name based on the variable name then great. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does that on the newer v8 versions: > const a = { foo: function () {}, bar() {}, baz: () => {} }
undefined
> a.foo.name
'foo'
> a.bar.name
'bar'
> a.baz.name
'baz' But I am not certain if there might be a context in which it is not inferred. I am also not sure where to trigger a stack trace from one of these functions as I didn't look deep into the async_hooks code so far. Can you point me out to something or give me a small example? I could enforce setting the name though. function hookFactory(symbol, name) {
const fn = function () {}
Object.defineProperty(fn, 'name', {
value: name
})
return fn
}
const hook = hookFactory(Symbol(), 'myName') There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stack trace was changed so I now explicitly set the name and it's now looking good again.
|
||
processing_hook = true; | ||
// Use a single try/catch for all hook to avoid setting up one per | ||
// iteration. | ||
try { | ||
for (var i = 0; i < active_hooks_array.length; i++) { | ||
if (typeof active_hooks_array[i][symbol] === 'function') { | ||
active_hooks_array[i][symbol](asyncId); | ||
} | ||
} | ||
} catch (e) { | ||
fatalError(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You asked how to reach this require('async_hooks').createHook({
before() { throw new Error('ah crud') },
}).enable();
setTimeout(() => {}, 10); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx |
||
} | ||
} catch (e) { | ||
fatalError(e); | ||
} | ||
processing_hook = false; | ||
processing_hook = false; | ||
|
||
if (tmp_active_hooks_array !== null) { | ||
restoreTmpHooks(); | ||
} | ||
if (tmp_active_hooks_array !== null) { | ||
restoreTmpHooks(); | ||
} | ||
}; | ||
} | ||
|
||
|
||
|
@@ -383,28 +390,6 @@ function emitBeforeS(asyncId, triggerAsyncId = asyncId) { | |
} | ||
|
||
|
||
// Called from native. The asyncId stack handling is taken care of there before | ||
// this is called. | ||
function emitAfterN(asyncId) { | ||
processing_hook = true; | ||
// Use a single try/catch for all hook to avoid setting up one per iteration. | ||
try { | ||
for (var i = 0; i < active_hooks_array.length; i++) { | ||
if (typeof active_hooks_array[i][after_symbol] === 'function') { | ||
active_hooks_array[i][after_symbol](asyncId); | ||
} | ||
} | ||
} catch (e) { | ||
fatalError(e); | ||
} | ||
processing_hook = false; | ||
|
||
if (tmp_active_hooks_array !== null) { | ||
restoreTmpHooks(); | ||
} | ||
} | ||
|
||
|
||
// TODO(trevnorris): Calling emitBefore/emitAfter from native can't adjust the | ||
// kIdStackIndex. But what happens if the user doesn't have both before and | ||
// after callbacks. | ||
|
@@ -425,26 +410,6 @@ function emitDestroyS(asyncId) { | |
} | ||
|
||
|
||
function emitDestroyN(asyncId) { | ||
processing_hook = true; | ||
// Use a single try/catch for all hook to avoid setting up one per iteration. | ||
try { | ||
for (var i = 0; i < active_hooks_array.length; i++) { | ||
if (typeof active_hooks_array[i][destroy_symbol] === 'function') { | ||
active_hooks_array[i][destroy_symbol](asyncId); | ||
} | ||
} | ||
} catch (e) { | ||
fatalError(e); | ||
} | ||
processing_hook = false; | ||
|
||
if (tmp_active_hooks_array !== null) { | ||
restoreTmpHooks(); | ||
} | ||
} | ||
|
||
|
||
// Emit callbacks for native calls. Since some state can be setup directly from | ||
// C++ there's no need to perform all the work here. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: is there a need to declare these? Stack beauty?
Otherwise could just be used in L#61
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the
emitDestroyN
hook is used only once, the other two are used in more than one spot. I made them all const for consistency.