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.constals=newAsyncLocalStorage({tag: 'foo'});console.log(als.run(123,()=>als.getStore('foo'));// 123console.log(als.run(321,()=>als.getStore());// undefined, doesn't matchconsole.log(als.run(321,()=>als.getStore('bar'));// undefined, doesn't matchThe 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
The
onPropagateoption 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,
onPropagateattaches a callback function to theAsyncLocalStorageinstance that is invoked whenever a newAsyncResourceis 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:
The
taghere 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