Description
The onPropagate
option was landed as experimental in v19.2... I'd like to propose that we revisit that and take a different approach.
https://nodejs.org/dist/latest-v19.x/docs/api/async_context.html#new-asynclocalstorageoptions
As background, onPropagate
attaches a callback function to the AsyncLocalStorage
instance that is invoked whenever a new AsyncResource
is created. This is problematic primarily because it incurs a significant additional performance overhead and assumes a model that is based on the underlying async_hooks API model (which I think there's consensus we want to move away from #46265).
Alternatively, I'd like to propose a "tag' model like in the following example:
// Defines the ALS instance such that a matching tag must be
// passed in to `getStore()` to successfully retrieve the store.
const als = new AsyncLocalStorage({ tag: 'foo' });
console.log(als.run(123, () => als.getStore('foo')); // 123
console.log(als.run(321, () => als.getStore()); // undefined, doesn't match
console.log(als.run(321, () => als.getStore('bar')); // undefined, doesn't match
The tag
here effectively becomes a component of the storage key. This is much more efficient design that will not incur a significant additional performance penalty.
/cc @legendecas @littledan @nodejs/async_hooks