Skip to content

Commit 22db34c

Browse files
Qardtargos
authored andcommitted
async_hooks: add sync enterWith to ALS
This allows transitioning the entire following sync and async execution sub-tree to the given async storage context. With this one can be sure the context binding will remain for any following sync activity and all descending async execution whereas the `run*(...)` methods must wrap everything that is intended to exist within the context. This is helpful for scenarios such as prepending a `'connection'` event to an http server which binds everything that occurs within each request to the given context. This is helpful for APMs to minimize the need for patching and especially adding closures. PR-URL: #31945 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 06d607d commit 22db34c

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

doc/api/async_hooks.md

+42
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,48 @@ If this method is called outside of an asynchronous context initialized by
957957
calling `asyncLocalStorage.run` or `asyncLocalStorage.runAndReturn`, it will
958958
return `undefined`.
959959

960+
### `asyncLocalStorage.enterWith(store)`
961+
<!-- YAML
962+
added: REPLACEME
963+
-->
964+
965+
* `store` {any}
966+
967+
Calling `asyncLocalStorage.enterWith(store)` will transition into the context
968+
for the remainder of the current synchronous execution and will persist
969+
through any following asynchronous calls.
970+
971+
Example:
972+
973+
```js
974+
const store = { id: 1 };
975+
asyncLocalStorage.enterWith(store);
976+
asyncLocalStorage.getStore(); // Returns the store object
977+
someAsyncOperation(() => {
978+
asyncLocalStorage.getStore(); // Returns the same object
979+
});
980+
```
981+
982+
This transition will continue for the _entire_ synchronous execution.
983+
This means that if, for example, the context is entered within an event
984+
handler subsequent event handlers will also run within that context unless
985+
specifically bound to another context with an `AsyncResource`.
986+
987+
```js
988+
const store = { id: 1 };
989+
990+
emitter.on('my-event', () => {
991+
asyncLocalStorage.enterWith(store);
992+
});
993+
emitter.on('my-event', () => {
994+
asyncLocalStorage.getStore(); // Returns the same object
995+
});
996+
997+
asyncLocalStorage.getStore(); // Returns undefined
998+
emitter.emit('my-event');
999+
asyncLocalStorage.getStore(); // Returns the same object
1000+
```
1001+
9601002
### `asyncLocalStorage.run(store, callback[, ...args])`
9611003
<!-- YAML
9621004
added: REPLACEME

lib/async_hooks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class AsyncLocalStorage {
246246
}
247247
}
248248

249-
_enter(store) {
249+
enterWith(store) {
250250
if (!this.enabled) {
251251
this.enabled = true;
252252
storageList.push(this);
@@ -259,7 +259,7 @@ class AsyncLocalStorage {
259259
runSyncAndReturn(store, callback, ...args) {
260260
const resource = executionAsyncResource();
261261
const outerStore = resource[this.kResourceStore];
262-
this._enter(store);
262+
this.enterWith(store);
263263
try {
264264
return callback(...args);
265265
} finally {
@@ -289,7 +289,7 @@ class AsyncLocalStorage {
289289
run(store, callback, ...args) {
290290
const resource = executionAsyncResource();
291291
const outerStore = resource[this.kResourceStore];
292-
this._enter(store);
292+
this.enterWith(store);
293293
process.nextTick(callback, ...args);
294294
resource[this.kResourceStore] = outerStore;
295295
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { AsyncLocalStorage } = require('async_hooks');
5+
6+
const asyncLocalStorage = new AsyncLocalStorage();
7+
8+
setImmediate(() => {
9+
const store = { foo: 'bar' };
10+
asyncLocalStorage.enterWith(store);
11+
12+
assert.strictEqual(asyncLocalStorage.getStore(), store);
13+
setTimeout(() => {
14+
assert.strictEqual(asyncLocalStorage.getStore(), store);
15+
}, 10);
16+
});
17+
18+
setTimeout(() => {
19+
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
20+
}, 10);

0 commit comments

Comments
 (0)