Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
async_hooks: add AsyncLocal class#27172
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
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2afe57a
async_hooks: add AsyncLocal class
Flarna a80e811
fix lint CRLF => LF
Flarna 9ec3977
Add test to re-enter an AsyncResource
Flarna 1cef6c3
Improve docs, throw on setting a new value from onChangedCb
Flarna 593552e
fix lint-md issue
Flarna dfe9477
add one more await test
Flarna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,7 +8,9 @@ const { | ||
| const { | ||
| ERR_ASYNC_CALLBACK, | ||
| ERR_INVALID_ASYNC_ID | ||
| ERR_ASYNCLOCAL_NO_RECURSION, | ||
| ERR_INVALID_ASYNC_ID, | ||
| ERR_INVALID_ARG_TYPE | ||
| } = require('internal/errors').codes; | ||
| const { validateString } = require('internal/validators'); | ||
| const internal_async_hooks = require('internal/async_hooks'); | ||
| @@ -200,6 +202,139 @@ class AsyncResource { | ||
| } | ||
| // AsyncLocal // | ||
| const kStack = Symbol('stack'); | ||
| const kIsFirst = Symbol('is-first'); | ||
| const kMap = Symbol('map'); | ||
| const kOnChangedCb = Symbol('on-changed-cb'); | ||
| const kHooks = Symbol('hooks'); | ||
| const kSet = Symbol('set'); | ||
| const kInOnChangedCb = Symbol('in-on-changed-cb'); | ||
| const kInvokeOnChangedCb = Symbol('invoke-on-changed-cb'); | ||
| class AsyncLocal { | ||
| constructor(options = {}) { | ||
| if (typeof options !== 'object' || options === null) | ||
| throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); | ||
| const { onChangedCb = null } = options; | ||
| if (onChangedCb !== null && typeof onChangedCb !== 'function') | ||
| throw new ERR_INVALID_ARG_TYPE('options.onChangedCb', | ||
| 'function', | ||
| onChangedCb); | ||
| this[kOnChangedCb] = onChangedCb; | ||
| this[kMap] = new Map(); | ||
| const fns = { | ||
| init: (asyncId, type, triggerAsyncId, resource) => { | ||
| // Propagate value from current id to new (execution graph) | ||
| const value = this[kMap].get(executionAsyncId()); | ||
| if (value) | ||
| this[kMap].set(asyncId, value); | ||
| }, | ||
| destroy: (asyncId) => this[kSet](asyncId, null), | ||
| }; | ||
| if (this[kOnChangedCb]) { | ||
| // Avoid setting a value from onChangedCb | ||
| this[kInOnChangedCb] = false; | ||
| // Change notification requires to keep a stack of async local values | ||
| this[kStack] = []; | ||
| // Indicates that first value was stored (before callback "missing") | ||
| this[kIsFirst] = true; | ||
| // Use before/after hooks to signal changes because of execution | ||
| fns.before = (asyncId) => { | ||
| const stack = this[kStack]; | ||
| const cVal = this[kMap].get(asyncId); | ||
| const pVal = stack[stack.length - 1]; | ||
| stack.push(pVal); | ||
Flarna marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| if (cVal !== pVal) | ||
| this[kInvokeOnChangedCb](pVal, cVal, true); | ||
| }; | ||
| fns.after = (asyncId) => { | ||
| const stack = this[kStack]; | ||
| const pVal = this[kMap].get(asyncId); | ||
| stack.pop(); | ||
| const cVal = stack[stack.length - 1]; | ||
| if (cVal !== pVal) | ||
| this[kInvokeOnChangedCb](pVal, cVal, true); | ||
| }; | ||
| } | ||
| this[kHooks] = createHook(fns); | ||
| } | ||
| set value(val) { | ||
| if (this[kInOnChangedCb]) | ||
| throw new ERR_ASYNCLOCAL_NO_RECURSION(); | ||
| val = val === null ? undefined : val; | ||
| const id = executionAsyncId(); | ||
| const onChangedCb = this[kOnChangedCb]; | ||
| let pVal; | ||
| if (onChangedCb) | ||
| pVal = this[kMap].get(id); | ||
| this[kSet](id, val); | ||
| if (onChangedCb && pVal !== val) | ||
| this[kInvokeOnChangedCb](pVal, val, false); | ||
| } | ||
| get value() { | ||
| return this[kMap].get(executionAsyncId()); | ||
| } | ||
| [kSet](id, val) { | ||
| const map = this[kMap]; | ||
| if (val == null) { | ||
| map.delete(id); | ||
| if (map.size === 0) | ||
| this[kHooks].disable(); | ||
| if (this[kOnChangedCb]) { | ||
| const stack = this[kStack]; | ||
| if (map.size === 0) { | ||
| // Hooks have been disabled so next set is the first one | ||
| stack.length = 0; | ||
| this[kIsFirst] = true; | ||
| } else { | ||
| stack[stack.length - 1] = undefined; | ||
| } | ||
| } | ||
| } else { | ||
| map.set(id, val); | ||
| if (map.size === 1) | ||
| this[kHooks].enable(); | ||
| if (this[kOnChangedCb]) { | ||
| const stack = this[kStack]; | ||
| if (this[kIsFirst]) { | ||
| // First value set => "simulate" before hook | ||
| this[kIsFirst] = false; | ||
| stack.push(val); | ||
| } else { | ||
| stack[stack.length - 1] = val; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| [kInvokeOnChangedCb](p, c, e) { | ||
| try { | ||
| this[kInOnChangedCb] = true; | ||
| this[kOnChangedCb](p, c, e); | ||
| } finally { | ||
| this[kInOnChangedCb] = false; | ||
| } | ||
| } | ||
| } | ||
| // Placing all exports down here because the exported classes won't export | ||
| // otherwise. | ||
| module.exports = { | ||
| @@ -209,4 +344,6 @@ module.exports = { | ||
| triggerAsyncId, | ||
| // Embedder API | ||
| AsyncResource, | ||
| // CLS API | ||
| AsyncLocal | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.