-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
async_hooks: expose list of types #13610
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
Changes from all commits
c81e975
2813840
df0a172
65b17b9
8f5d972
b77ba7a
3808174
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 |
---|---|---|
|
@@ -131,6 +131,16 @@ provided by AsyncHooks itself. The logging should then be skipped when | |
it was the logging itself that caused AsyncHooks callback to call. By | ||
doing this the otherwise infinite recursion is broken. | ||
|
||
#### `async_hooks.getTypes()` | ||
|
||
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. Unnecessary whitespace before the yaml comment |
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
* Returns: `{Array}` list of async resource type names | ||
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 "list of async resource type names" is redundant considering it is documented two lines below. 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 "list of async resource type names" is redundant considering it is documented two lines below. 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. Some note about why these are useful would be helpful here. |
||
|
||
Return the list of async resource type names. Names are simple strings. | ||
|
||
#### `asyncHook.enable()` | ||
|
||
* Returns {AsyncHook} A reference to `asyncHook`. | ||
|
@@ -428,6 +438,13 @@ const server = net.createServer(function onConnection(conn) { | |
}); | ||
``` | ||
|
||
#### `async_hooks.registerTypeName(type)` | ||
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. added YAML annotation needed. |
||
|
||
* `type` {string} a new type of async resource | ||
* Returns {string} the type name to use | ||
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. Why not just return 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. I find it convenient to do the same as is also already in this PR: const kTickObjectType = async_hooks.registerTypeName('TickObject'); |
||
|
||
Registers the type name for a new async resource. | ||
|
||
#### `async_hooks.triggerId()` | ||
|
||
* Returns {number} the ID of the resource responsible for calling the callback | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict'; | ||
|
||
const errors = require('internal/errors'); | ||
const async_wrap = process.binding('async_wrap'); | ||
/* Both these arrays are used to communicate between JS and C++ with as little | ||
* overhead as possible. | ||
|
@@ -461,6 +462,18 @@ function init(asyncId, type, triggerId, resource) { | |
processing_hook = false; | ||
} | ||
|
||
const JSProviders = new Set(); | ||
function registerTypeName(type) { | ||
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. I'd like to see a type check here, ensuring |
||
if (JSProviders.has(type)) | ||
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. Maybe we should make sure |
||
throw new errors.TypeError('ERR_ASYNC_PROVIDER_NAME', type); | ||
JSProviders.add(type); | ||
return type; | ||
} | ||
|
||
function getTypes() { | ||
return Object.keys(async_wrap.Providers) | ||
.concat(...JSProviders.keys()); | ||
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. No need for 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. This is certainly not the fastest method, but given that this shouldn't be a hot code path that should be ok |
||
} | ||
|
||
// Placing all exports down here because the exported classes won't export | ||
// otherwise. | ||
|
@@ -469,6 +482,8 @@ module.exports = { | |
createHook, | ||
currentId, | ||
triggerId, | ||
getTypes, | ||
registerTypeName, | ||
// Embedder API | ||
AsyncResource, | ||
runInAsyncIdScope, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
|
||
const types1 = async_hooks.getTypes(); | ||
types1.forEach((v, k) => assert.strictEqual( | ||
typeof v, | ||
'string', | ||
`${v} from types[${k}] should be a string`) | ||
); | ||
|
||
const sillyName = 'gaga'; | ||
const newType = async_hooks.registerTypeName(sillyName); | ||
assert.strictEqual( | ||
typeof newType, | ||
'string', | ||
`${newType} should be a string` | ||
); | ||
assert.strictEqual(newType, sillyName); | ||
assert.throws( | ||
() => async_hooks.registerTypeName(sillyName), | ||
common.expectsError( | ||
{ | ||
code: 'ERR_ASYNC_PROVIDER_NAME', | ||
type: TypeError, | ||
message: `"${sillyName}" type name already registered` | ||
} | ||
) | ||
); | ||
|
||
const types2 = async_hooks.getTypes(); | ||
assert.strictEqual(types2.length, types1.length + 1); | ||
assert.strictEqual(types2.includes(newType), true); |
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.
Alphabetical order.