-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: rewrite AsyncLocalStorage without async_hooks
- Loading branch information
Stephen Belanger
committed
Jul 28, 2023
1 parent
48345d0
commit 5a7b995
Showing
25 changed files
with
823 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict'; | ||
|
||
const { | ||
ArrayPrototypeIndexOf, | ||
ArrayPrototypePush, | ||
ArrayPrototypeSplice, | ||
ObjectIs, | ||
ReflectApply | ||
} = primordials; | ||
|
||
const { | ||
AsyncResource, | ||
createHook, | ||
executionAsyncResource | ||
} = require('async_hooks'); | ||
|
||
const storageList = []; | ||
const storageHook = createHook({ | ||
init (asyncId, type, triggerAsyncId, resource) { | ||
const currentResource = executionAsyncResource(); | ||
// Value of currentResource is always a non null object | ||
for (let i = 0; i < storageList.length; ++i) { | ||
storageList[i]._propagate(resource, currentResource, type); | ||
} | ||
}, | ||
}); | ||
|
||
class AsyncLocalStorage { | ||
constructor () { | ||
this.kResourceStore = Symbol('kResourceStore'); | ||
this.enabled = false; | ||
} | ||
|
||
static bind (fn) { | ||
return AsyncResource.bind(fn); | ||
} | ||
|
||
static snapshot () { | ||
return AsyncLocalStorage.bind((cb, ...args) => cb(...args)); | ||
} | ||
|
||
disable () { | ||
if (this.enabled) { | ||
this.enabled = false; | ||
// If this.enabled, the instance must be in storageList | ||
ArrayPrototypeSplice(storageList, | ||
ArrayPrototypeIndexOf(storageList, this), 1); | ||
if (storageList.length === 0) { | ||
storageHook.disable(); | ||
} | ||
} | ||
} | ||
|
||
_enable () { | ||
if (!this.enabled) { | ||
this.enabled = true; | ||
ArrayPrototypePush(storageList, this); | ||
storageHook.enable(); | ||
} | ||
} | ||
|
||
// Propagate the context from a parent resource to a child one | ||
_propagate (resource, triggerResource, type) { | ||
const store = triggerResource[this.kResourceStore]; | ||
if (this.enabled) { | ||
resource[this.kResourceStore] = store; | ||
} | ||
} | ||
|
||
enterWith (store) { | ||
this._enable(); | ||
const resource = executionAsyncResource(); | ||
resource[this.kResourceStore] = store; | ||
} | ||
|
||
run (store, callback, ...args) { | ||
// Avoid creation of an AsyncResource if store is already active | ||
if (ObjectIs(store, this.getStore())) { | ||
return ReflectApply(callback, null, args); | ||
} | ||
|
||
this._enable(); | ||
|
||
const resource = executionAsyncResource(); | ||
const oldStore = resource[this.kResourceStore]; | ||
|
||
resource[this.kResourceStore] = store; | ||
|
||
try { | ||
return ReflectApply(callback, null, args); | ||
} finally { | ||
resource[this.kResourceStore] = oldStore; | ||
} | ||
} | ||
|
||
exit (callback, ...args) { | ||
if (!this.enabled) { | ||
return ReflectApply(callback, null, args); | ||
} | ||
this.disable(); | ||
try { | ||
return ReflectApply(callback, null, args); | ||
} finally { | ||
this._enable(); | ||
} | ||
} | ||
|
||
getStore () { | ||
if (this.enabled) { | ||
const resource = executionAsyncResource(); | ||
return resource[this.kResourceStore]; | ||
} | ||
} | ||
} | ||
|
||
module.exports = AsyncLocalStorage; |
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,63 @@ | ||
'use strict'; | ||
|
||
const { | ||
FunctionPrototypeBind, | ||
ReflectApply | ||
} = primordials; | ||
|
||
const { validateFunction } = require('internal/validators'); | ||
|
||
const { AsyncContextFrame } = internalBinding('async_context_frame'); | ||
|
||
class AsyncLocalStorage { | ||
static bind (fn) { | ||
validateFunction(fn, 'fn'); | ||
const run = this.snapshot() | ||
return function bound (...args) { | ||
return run.call(this, fn, ...args); | ||
}; | ||
} | ||
|
||
static snapshot () { | ||
const frame = AsyncContextFrame.current(); | ||
return function runSnapshot (fn, ...args) { | ||
const bound = FunctionPrototypeBind(fn, this); | ||
const prior = AsyncContextFrame.exchange(frame); | ||
try { | ||
return ReflectApply(bound, undefined, args); | ||
} finally { | ||
AsyncContextFrame.exchange(prior); | ||
} | ||
}; | ||
} | ||
|
||
disable () { | ||
AsyncContextFrame.disable(this); | ||
} | ||
|
||
enterWith (data) { | ||
const frame = new AsyncContextFrame(this, data); | ||
AsyncContextFrame.exchange(frame); | ||
} | ||
|
||
run (data, fn, ...args) { | ||
const frame = new AsyncContextFrame(this, data); | ||
const prior = AsyncContextFrame.exchange(frame); | ||
|
||
try { | ||
return ReflectApply(fn, undefined, args); | ||
} finally { | ||
AsyncContextFrame.exchange(prior); | ||
} | ||
} | ||
|
||
exit (fn, ...args) { | ||
return ReflectApply(this.run, this, [undefined, fn, ...args]); | ||
} | ||
|
||
getStore () { | ||
return AsyncContextFrame.current()?.get(this); | ||
} | ||
} | ||
|
||
module.exports = AsyncLocalStorage; |
Oops, something went wrong.