mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
async_hooks: introduce async-context API
Adding AsyncLocalStorage class to async_hooks module. This API provide a simple CLS-like set of features. Co-authored-by: Andrey Pechkurov <apechkurov@gmail.com> PR-URL: https://github.com/nodejs/node/pull/26540 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
72b6cea25d
commit
9c702922cd
13 changed files with 667 additions and 3 deletions
|
@ -1,9 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const {
|
||||
Map,
|
||||
NumberIsSafeInteger,
|
||||
ReflectApply,
|
||||
Symbol,
|
||||
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
|
@ -209,11 +211,102 @@ class AsyncResource {
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
class AsyncLocalStorage {
|
||||
constructor() {
|
||||
this.kResourceStore = Symbol('kResourceStore');
|
||||
this.enabled = false;
|
||||
}
|
||||
|
||||
disable() {
|
||||
if (this.enabled) {
|
||||
this.enabled = false;
|
||||
// If this.enabled, the instance must be in storageList
|
||||
storageList.splice(storageList.indexOf(this), 1);
|
||||
if (storageList.length === 0) {
|
||||
storageHook.disable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Propagate the context from a parent resource to a child one
|
||||
_propagate(resource, triggerResource) {
|
||||
const store = triggerResource[this.kResourceStore];
|
||||
if (this.enabled) {
|
||||
resource[this.kResourceStore] = store;
|
||||
}
|
||||
}
|
||||
|
||||
_enter() {
|
||||
if (!this.enabled) {
|
||||
this.enabled = true;
|
||||
storageList.push(this);
|
||||
storageHook.enable();
|
||||
}
|
||||
const resource = executionAsyncResource();
|
||||
resource[this.kResourceStore] = new Map();
|
||||
}
|
||||
|
||||
_exit() {
|
||||
const resource = executionAsyncResource();
|
||||
if (resource) {
|
||||
resource[this.kResourceStore] = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
runSyncAndReturn(callback, ...args) {
|
||||
this._enter();
|
||||
try {
|
||||
return callback(...args);
|
||||
} finally {
|
||||
this._exit();
|
||||
}
|
||||
}
|
||||
|
||||
exitSyncAndReturn(callback, ...args) {
|
||||
this.enabled = false;
|
||||
try {
|
||||
return callback(...args);
|
||||
} finally {
|
||||
this.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
getStore() {
|
||||
const resource = executionAsyncResource();
|
||||
if (this.enabled) {
|
||||
return resource[this.kResourceStore];
|
||||
}
|
||||
}
|
||||
|
||||
run(callback, ...args) {
|
||||
this._enter();
|
||||
process.nextTick(callback, ...args);
|
||||
this._exit();
|
||||
}
|
||||
|
||||
exit(callback, ...args) {
|
||||
this.enabled = false;
|
||||
process.nextTick(callback, ...args);
|
||||
this.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Placing all exports down here because the exported classes won't export
|
||||
// otherwise.
|
||||
module.exports = {
|
||||
// Public API
|
||||
AsyncLocalStorage,
|
||||
createHook,
|
||||
executionAsyncId,
|
||||
triggerAsyncId,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue