nest
nest copied to clipboard
feature() add async_hooks module (async storage) [BLOCKED]
PR Checklist
Please check if your PR fulfills the following requirements:
- [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
- [x] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
PR Type
What kind of change does this PR introduce?
[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:
What is the current behavior?
Issue Number: N/A
What is the new behavior?
AsyncHooksModule which uses async_hooks module capabilities (share async state, for example, between HTTP requests). Blocked due to memory leaks - async_hooks is still in experimental stage.
Does this PR introduce a breaking change?
[ ] Yes
[x] No
Other information
Pull Request Test Coverage Report for Build 1344
- 32 of 56 (57.14%) changed or added relevant lines in 7 files are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage decreased (-0.6%) to 92.516%
| Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
|---|---|---|---|
| packages/common/hooks/async-hooks-middleware.ts | 3 | 5 | 60.0% |
| packages/common/hooks/async-hooks-helper.ts | 3 | 6 | 50.0% |
| packages/common/hooks/async-hooks-storage.ts | 6 | 11 | 54.55% |
| packages/common/hooks/async-context.ts | 13 | 27 | 48.15% |
| <!-- | Total: | 32 | 56 |
| Totals | |
|---|---|
| Change from base Build 1336: | -0.6% |
| Covered Lines: | 3118 |
| Relevant Lines: | 3288 |
💛 - Coveralls
Pull Request Test Coverage Report for Build 1346
- 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage increased (+0.002%) to 93.068%
| Totals | |
|---|---|
| Change from base Build 1336: | 0.002% |
| Covered Lines: | 3087 |
| Relevant Lines: | 3233 |
💛 - Coveralls
It's been 2 years, async_hooks are still experimental but maybe we could get this feature behind the experimental flag?
We adopted the code from this PR in @narando/nest-xray to implement async storage. So far we have not encountered any bugs.
https://github.com/narando/nest-xray/tree/master/lib/async-hooks
Instead of using async_hooks directly, we should explore using AsyncLocalStorage to provide this functionality. It was released in Node.js 14 and is part of the experimental async_hooks module.
Why not try to reactivate it? Two years later the technology has been improved.
Instead of using
async_hooksdirectly, we should explore usingAsyncLocalStorageto provide this functionality. It was released in Node.js 14 and is part of the experimentalasync_hooksmodule.
it has even been back ported to Node.js 12 ( the latest EOL release :-) )
I was also looking for this functionality, so I spent some time researching and testing and the result is this package: https://github.com/Papooch/nestjs-cls It would be great if Nest provided some 'request hooks' so we could wrap the execution context of the whole request somehow instead of relying on a middleware or an 'enhancer' (though I don't know if that's even possible)
Any updates? This issue is basically 3 years old, and it seems like the node AsyncLocalStorage API has matured quite a bit in that time. I think it would make a huge impact on NestJS- specifically as a mechanism to pass req/ctx while avoiding the performance penalty of request scoped providers.
specifically as a mechanism to pass req/ctx while avoiding the performance penalty of request scoped providers.
I wouldn't be so sure that using AsyncLocalStorage atm would be better (performance-wise) than properly designed request-scoped providers.
Any updates? This issue is basically 3 years old
According to the official Node.js docs, async_hooks are still experimental https://nodejs.org/api/async_hooks.html and this PR won't get merged till that changes.
async_hooks are still experimental but if anyone wants to use this feature now, check out this library https://github.com/Papooch/nestjs-cls