-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
async_hooks: add executionAsyncResource #30959
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
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
dd0d929
async_hooks: add currentResource
mcollina 6ab9a58
rename to ExecutionAsyncResource
mcollina 1c4abc1
benchmark fix
mcollina 2fb5a50
store resource on AsyncHooks object
Qard 63fcaa5
Update docs to use executionAsyncResource
Qard e1a4e88
Convert execution async resource to use a stack
Qard b66098d
Add doc note about use of handle APIs being unsafe
Qard b95b514
Clean up some unnecessary bits
Qard a388de9
Give top-level an empty object as a resource
Qard b3ecef0
Minor docs improvements
Qard 0fd3be8
Use stack emplace rather than push
Qard 1eb13c5
Do not push promise resource on init
Qard 9381824
Rename test files
Qard 851e478
Interact with resource stack directly
Qard 30f79b2
Cleanup some things
Qard 621d089
Handle object reuse properly
Qard c485b39
Add test for object reuse fix
Qard 36bcdfa
Make reused resource storage weak
Qard 60b33d2
Stored resource needs to be strong reference
Qard 48920e3
Reset resource after destroy and add note about strong reference safety
Qard 1d68999
Better resource stack clearing
Qard a8f00fc
Make stronger statement about unsafety of using resource properties o…
Qard 1a7243c
fixup: make benchmark tests pass
addaleax 3402fac
fixup: docs review nit
addaleax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
'use strict'; | ||
|
||
const { promisify } = require('util'); | ||
const { readFile } = require('fs'); | ||
const sleep = promisify(setTimeout); | ||
const read = promisify(readFile); | ||
const common = require('../common.js'); | ||
const { | ||
createHook, | ||
executionAsyncResource, | ||
executionAsyncId | ||
} = require('async_hooks'); | ||
const { createServer } = require('http'); | ||
|
||
// Configuration for the http server | ||
// there is no need for parameters in this test | ||
const connections = 500; | ||
const path = '/'; | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['async-resource', 'destroy'], | ||
asyncMethod: ['callbacks', 'async'], | ||
n: [1e6] | ||
}); | ||
|
||
function buildCurrentResource(getServe) { | ||
const server = createServer(getServe(getCLS, setCLS)); | ||
const hook = createHook({ init }); | ||
const cls = Symbol('cls'); | ||
hook.enable(); | ||
|
||
return { | ||
server, | ||
close | ||
}; | ||
|
||
function getCLS() { | ||
const resource = executionAsyncResource(); | ||
if (resource === null || !resource[cls]) { | ||
return null; | ||
} | ||
return resource[cls].state; | ||
} | ||
|
||
function setCLS(state) { | ||
const resource = executionAsyncResource(); | ||
if (resource === null) { | ||
return; | ||
} | ||
if (!resource[cls]) { | ||
resource[cls] = { state }; | ||
} else { | ||
resource[cls].state = state; | ||
} | ||
} | ||
|
||
function init(asyncId, type, triggerAsyncId, resource) { | ||
var cr = executionAsyncResource(); | ||
if (cr !== null) { | ||
resource[cls] = cr[cls]; | ||
} | ||
} | ||
|
||
function close() { | ||
hook.disable(); | ||
server.close(); | ||
} | ||
} | ||
|
||
function buildDestroy(getServe) { | ||
const transactions = new Map(); | ||
const server = createServer(getServe(getCLS, setCLS)); | ||
const hook = createHook({ init, destroy }); | ||
hook.enable(); | ||
|
||
return { | ||
server, | ||
close | ||
}; | ||
|
||
function getCLS() { | ||
const asyncId = executionAsyncId(); | ||
return transactions.has(asyncId) ? transactions.get(asyncId) : null; | ||
} | ||
|
||
function setCLS(value) { | ||
const asyncId = executionAsyncId(); | ||
transactions.set(asyncId, value); | ||
} | ||
|
||
function init(asyncId, type, triggerAsyncId, resource) { | ||
transactions.set(asyncId, getCLS()); | ||
} | ||
|
||
function destroy(asyncId) { | ||
transactions.delete(asyncId); | ||
} | ||
|
||
function close() { | ||
hook.disable(); | ||
server.close(); | ||
} | ||
} | ||
|
||
function getServeAwait(getCLS, setCLS) { | ||
return async function serve(req, res) { | ||
setCLS(Math.random()); | ||
await sleep(10); | ||
await read(__filename); | ||
res.setHeader('content-type', 'application/json'); | ||
res.end(JSON.stringify({ cls: getCLS() })); | ||
}; | ||
} | ||
|
||
function getServeCallbacks(getCLS, setCLS) { | ||
return function serve(req, res) { | ||
setCLS(Math.random()); | ||
setTimeout(() => { | ||
readFile(__filename, () => { | ||
res.setHeader('content-type', 'application/json'); | ||
res.end(JSON.stringify({ cls: getCLS() })); | ||
}); | ||
}, 10); | ||
}; | ||
} | ||
|
||
const types = { | ||
'async-resource': buildCurrentResource, | ||
'destroy': buildDestroy | ||
}; | ||
|
||
const asyncMethods = { | ||
'callbacks': getServeCallbacks, | ||
'async': getServeAwait | ||
}; | ||
|
||
function main({ type, asyncMethod }) { | ||
const { server, close } = types[type](asyncMethods[asyncMethod]); | ||
|
||
server | ||
.listen(common.PORT) | ||
.on('listening', () => { | ||
|
||
bench.http({ | ||
path, | ||
connections | ||
}, () => { | ||
close(); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.