async_hooks: add AsyncLocal class
This introduces a new API to provide asynchronous storage via a new AsyncLocal class. Besides setting a value which is passed along asynchronous invocations it allows to register a callback to get informed about changes of the current value.
The implementation is based on async_hooks but it doesn't expose internals like execution Ids, resources or the hooks itself.
Naming and implementation is inspired by .NET AsyncLocal.
There is already #26540 adding similar functionality then this one and we should for sure not add both. I'm not sure if creating a new PR in parallel is the best way to discuss this but I think it's the easiest variant to allow anyone to give both a try.
Main differences between #26540 and this one:
- #26540 follows trigger path of resources wheres this on follows execution path
- This one has a copy on write semantic for the async value
- This one allows to install a callback to get notified if value changes
As prove of concept I tried locally to port domains to use AsyncLocal
instead
async_hooks. I was able to get all tests green except 3 base on native
addons using a domain
property on the resource which seems to be deprecated
meanwhile so I stopped there. The notification callback was needed to get this far.
Refs: https://docs.microsoft.com/en-us/dotnet/api/system.threading.asynclocal-1?view=netframework-4.7.2 Refs: #26540
fyi @nodejs/diagnostics @watson @vdeturckheim
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
documentation is changed or added -
commit message follows commit guidelines