-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces new AsyncLocal API to provide capabilities for building continuation local storage on top of it. The implementation is based on async hooks. Public API is inspired by ThreadLocal class in Java.
- Loading branch information
Showing
9 changed files
with
293 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,26 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
const asyncLocalOne = new AsyncLocal(); | ||
const asyncLocalTwo = new AsyncLocal(); | ||
|
||
setTimeout(() => { | ||
assert.strictEqual(asyncLocalOne.get(), undefined); | ||
assert.strictEqual(asyncLocalTwo.get(), undefined); | ||
|
||
asyncLocalOne.set('foo'); | ||
asyncLocalTwo.set('bar'); | ||
assert.strictEqual(asyncLocalOne.get(), 'foo'); | ||
assert.strictEqual(asyncLocalTwo.get(), 'bar'); | ||
|
||
asyncLocalOne.set('baz'); | ||
asyncLocalTwo.set(42); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocalOne.get(), 'baz'); | ||
assert.strictEqual(asyncLocalTwo.get(), 42); | ||
}, 0); | ||
}, 0); |
This file contains 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,26 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.get(), undefined); | ||
|
||
asyncLocal.set('A'); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.get(), 'A'); | ||
|
||
asyncLocal.set('B'); | ||
setTimeout(() => { | ||
assert.strictEqual(asyncLocal.get(), 'B'); | ||
}, 0); | ||
|
||
assert.strictEqual(asyncLocal.get(), 'B'); | ||
}, 0); | ||
|
||
assert.strictEqual(asyncLocal.get(), 'A'); | ||
}, 0); |
This file contains 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,24 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
async function asyncFunc() { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, 0); | ||
}); | ||
} | ||
|
||
async function testAwait() { | ||
asyncLocal.set('foo'); | ||
await asyncFunc(); | ||
assert.strictEqual(asyncLocal.get(), 'foo'); | ||
} | ||
|
||
testAwait().then(common.mustCall(() => | ||
assert.strictEqual(asyncLocal.get(), 'foo') | ||
)); |
This file contains 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,33 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const { AsyncLocal } = async_hooks; | ||
|
||
assert.strictEqual(new AsyncLocal().get(), undefined); | ||
|
||
const asyncLocal = new AsyncLocal(); | ||
|
||
assert.strictEqual(asyncLocal.get(), undefined); | ||
|
||
asyncLocal.set(42); | ||
assert.strictEqual(asyncLocal.get(), 42); | ||
asyncLocal.set('foo'); | ||
assert.strictEqual(asyncLocal.get(), 'foo'); | ||
const obj = {}; | ||
asyncLocal.set(obj); | ||
assert.strictEqual(asyncLocal.get(), obj); | ||
|
||
asyncLocal.remove(); | ||
assert.strictEqual(asyncLocal.get(), undefined); | ||
|
||
// Throws on modification after removal | ||
common.expectsError( | ||
() => asyncLocal.set('bar'), { | ||
code: 'ERR_ASYNC_LOCAL_CANNOT_SET_VALUE', | ||
type: Error, | ||
}); | ||
|
||
// Subsequent .remove() does not throw | ||
asyncLocal.remove(); |