-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
async_hooks: add using scopes to AsyncLocalStorage #61674
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
Open
Qard
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
Qard:als-run-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−0
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| SymbolDispose, | ||
| } = primordials; | ||
|
|
||
| class RunScope { | ||
| #storage; | ||
| #previousStore; | ||
| #disposed = false; | ||
|
|
||
| constructor(storage, store) { | ||
| this.#storage = storage; | ||
| this.#previousStore = storage.getStore(); | ||
| storage.enterWith(store); | ||
| } | ||
|
|
||
| [SymbolDispose]() { | ||
| if (this.#disposed) { | ||
| return; | ||
| } | ||
| this.#disposed = true; | ||
| this.#storage.enterWith(this.#previousStore); | ||
| } | ||
| } | ||
|
|
||
| module.exports = RunScope; |
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,168 @@ | ||
| /* eslint-disable no-unused-vars */ | ||
| 'use strict'; | ||
| require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { AsyncLocalStorage } = require('node:async_hooks'); | ||
|
|
||
| // Test basic RunScope with using | ||
| { | ||
| const storage = new AsyncLocalStorage(); | ||
|
|
||
| assert.strictEqual(storage.getStore(), undefined); | ||
|
|
||
| { | ||
| using scope = storage.withScope('test'); | ||
| assert.strictEqual(storage.getStore(), 'test'); | ||
| } | ||
|
|
||
| // Store should be restored to undefined | ||
| assert.strictEqual(storage.getStore(), undefined); | ||
| } | ||
|
|
||
| // Test RunScope restores previous value | ||
| { | ||
| const storage = new AsyncLocalStorage(); | ||
|
|
||
| storage.enterWith('initial'); | ||
| assert.strictEqual(storage.getStore(), 'initial'); | ||
|
|
||
| { | ||
| using scope = storage.withScope('scoped'); | ||
| assert.strictEqual(storage.getStore(), 'scoped'); | ||
| } | ||
|
|
||
| // Should restore to previous value | ||
| assert.strictEqual(storage.getStore(), 'initial'); | ||
| } | ||
|
|
||
| // Test nested RunScope | ||
| { | ||
| const storage = new AsyncLocalStorage(); | ||
| const storeValues = []; | ||
|
|
||
| { | ||
| using outer = storage.withScope('outer'); | ||
| storeValues.push(storage.getStore()); | ||
|
|
||
| { | ||
| using inner = storage.withScope('inner'); | ||
| storeValues.push(storage.getStore()); | ||
| } | ||
|
|
||
| // Should restore to outer | ||
| storeValues.push(storage.getStore()); | ||
| } | ||
|
|
||
| // Should restore to undefined | ||
| storeValues.push(storage.getStore()); | ||
|
|
||
| assert.deepStrictEqual(storeValues, ['outer', 'inner', 'outer', undefined]); | ||
| } | ||
|
|
||
| // Test RunScope with error during usage | ||
| { | ||
| const storage = new AsyncLocalStorage(); | ||
|
|
||
| storage.enterWith('before'); | ||
|
|
||
| const testError = new Error('test'); | ||
|
|
||
| assert.throws(() => { | ||
| using scope = storage.withScope('during'); | ||
| assert.strictEqual(storage.getStore(), 'during'); | ||
| throw testError; | ||
| }, testError); | ||
|
|
||
| // Store should be restored even after error | ||
| assert.strictEqual(storage.getStore(), 'before'); | ||
| } | ||
|
|
||
| // Test idempotent disposal | ||
| { | ||
| const storage = new AsyncLocalStorage(); | ||
|
|
||
| const scope = storage.withScope('test'); | ||
| assert.strictEqual(storage.getStore(), 'test'); | ||
|
|
||
| // Dispose via Symbol.dispose | ||
| scope[Symbol.dispose](); | ||
| assert.strictEqual(storage.getStore(), undefined); | ||
|
|
||
| storage.enterWith('test2'); | ||
| assert.strictEqual(storage.getStore(), 'test2'); | ||
|
|
||
| // Double dispose should be idempotent | ||
Qard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| scope[Symbol.dispose](); | ||
| assert.strictEqual(storage.getStore(), 'test2'); | ||
| } | ||
|
|
||
| // Test RunScope with defaultValue | ||
| { | ||
| const storage = new AsyncLocalStorage({ defaultValue: 'default' }); | ||
|
|
||
| assert.strictEqual(storage.getStore(), 'default'); | ||
|
|
||
| { | ||
| using scope = storage.withScope('custom'); | ||
| assert.strictEqual(storage.getStore(), 'custom'); | ||
| } | ||
|
|
||
| // Should restore to default | ||
| assert.strictEqual(storage.getStore(), 'default'); | ||
| } | ||
|
|
||
| // Test deeply nested RunScope | ||
| { | ||
| const storage = new AsyncLocalStorage(); | ||
|
|
||
| { | ||
| using s1 = storage.withScope(1); | ||
| assert.strictEqual(storage.getStore(), 1); | ||
|
|
||
| { | ||
| using s2 = storage.withScope(2); | ||
| assert.strictEqual(storage.getStore(), 2); | ||
|
|
||
| { | ||
| using s3 = storage.withScope(3); | ||
| assert.strictEqual(storage.getStore(), 3); | ||
|
|
||
| { | ||
| using s4 = storage.withScope(4); | ||
| assert.strictEqual(storage.getStore(), 4); | ||
| } | ||
|
|
||
| assert.strictEqual(storage.getStore(), 3); | ||
| } | ||
|
|
||
| assert.strictEqual(storage.getStore(), 2); | ||
| } | ||
|
|
||
| assert.strictEqual(storage.getStore(), 1); | ||
| } | ||
|
|
||
| assert.strictEqual(storage.getStore(), undefined); | ||
| } | ||
|
|
||
| // Test RunScope with multiple storages | ||
| { | ||
| const storage1 = new AsyncLocalStorage(); | ||
| const storage2 = new AsyncLocalStorage(); | ||
|
|
||
| { | ||
| using scope1 = storage1.withScope('A'); | ||
|
|
||
| { | ||
| using scope2 = storage2.withScope('B'); | ||
|
|
||
| assert.strictEqual(storage1.getStore(), 'A'); | ||
| assert.strictEqual(storage2.getStore(), 'B'); | ||
| } | ||
|
|
||
| assert.strictEqual(storage1.getStore(), 'A'); | ||
| assert.strictEqual(storage2.getStore(), undefined); | ||
| } | ||
|
|
||
| assert.strictEqual(storage1.getStore(), undefined); | ||
| assert.strictEqual(storage2.getStore(), undefined); | ||
| } | ||
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.