mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
async_hooks: use resource stack for AsyncLocalStorage run
PR-URL: https://github.com/nodejs/node/pull/39890 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
This commit is contained in:
parent
508890d795
commit
926152a38c
3 changed files with 33 additions and 38 deletions
|
@ -263,7 +263,6 @@ const storageHook = createHook({
|
|||
}
|
||||
});
|
||||
|
||||
const defaultAlsResourceOpts = { requireManualDestroy: true };
|
||||
class AsyncLocalStorage {
|
||||
constructor() {
|
||||
this.kResourceStore = Symbol('kResourceStore');
|
||||
|
@ -309,14 +308,19 @@ class AsyncLocalStorage {
|
|||
if (ObjectIs(store, this.getStore())) {
|
||||
return ReflectApply(callback, null, args);
|
||||
}
|
||||
const resource = new AsyncResource('AsyncLocalStorage',
|
||||
defaultAlsResourceOpts);
|
||||
// Calling emitDestroy before runInAsyncScope avoids a try/finally
|
||||
// It is ok because emitDestroy only schedules calling the hook
|
||||
return resource.emitDestroy().runInAsyncScope(() => {
|
||||
this.enterWith(store);
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue