Skip to content

Revisit the AsyncLocalStorage onPropagate option #46374

Closed
@jasnell

Description

@jasnell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions