Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncLocalStorage supporting in callback & promise & plugin #10233

Closed
wants to merge 6 commits into from

Conversation

wujjpp
Copy link

@wujjpp wujjpp commented May 12, 2021

Mongoose doesn't support async hooks, this PR enable this feature.

Features

  1. You can get aysncLocalStorage store in mongoose action callback or promise
  2. You can get asyncLocalStorage store in mongoose plugin

Special Case:

[IMPORTANT] plugin for aggregate should be individual processing

const pluginAggregate = (schema) => {
  schema.pre(["aggregate"], function () {
    // Special Case: aggregate should keep store
    const contextData = callContext.get();
    this.__asyncLocalStore = contextData;
  });

  schema.post(["aggregate"], function () {
    const contextData = this.__asyncLocalStore;
    // now you can use contextData
  });
};

Test case:

$ cd examples/asyncLocalStorage
$ npm install && node index.js

NOTE: This PR also fix #10020

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed PR, but I don't think this functionality belongs in Mongoose because async hooks are still an experimental API for Node.js and not supported in all the Node.js versions that Mongoose supports.

I think this change would be better suited for our @mongoosejs/async-hooks plugin, which is meant to provide async hooks integration for Mongoose.

Your example doesn't seem like it works as expected with @mongoosejs/async-hooks - would you be willing to move this code over to that repo?

[OK] [static-hooks] [pre] [save]
[OK] [static-hooks] [post] [save]
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] cac4238f-7f84-43b9-af51-76d8a4db9e49 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] 6368d57d-5943-4d7f-958b-06c3181179dd 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] 5f4984db-b900-4068-99ec-521d9f7bf7c4 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] 5328903c-4a86-4a7e-8be8-69fa855ba79c 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] 9e9bb6b8-0626-4376-a6d9-ad60fe17c109 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] 5c7dc699-9e46-42c1-8321-afd48d4f092d 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] a489d4fd-3ac8-40f5-8c6c-d69f8c42ed9a 24010a65-3c28-4047-b988-0f7f6f872498
[OK] [static-hooks] [pre] [save]
[ERROR] [static-hooks] [post] [save] ca05f783-871a-42b1-8602-93f4e4e6d6c6 24010a65-3c28-4047-b988-0f7f6f872498

@wujjpp
Copy link
Author

wujjpp commented May 17, 2021

@vkarpov15 Thanks for your reply

For eslint issue I have convert async/await style to promise style in test case, and I have test in windows, mac and linux with nodejs > v13.10.0, all platforms are working as expetced. Can you help to confirm which version of nodejs did you used?

This PR contains AsyncLocalStorage feature detection. if not support(nodejs < 13.0.0), the feature was auto disabled.

I will try to test @mongoose/async-hooks plugin later and try to move this code over to that repo

@wujjpp wujjpp requested a review from vkarpov15 May 17, 2021 06:31
@hasezoey
Copy link
Collaborator

closing this PR because it didnt see any updates in quite some time and Node discourages the use of async_hooks (which this PR uses) and instead encourages the use of AsyncLocalStorage

@hasezoey hasezoey closed this Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Post-hooks do not work with AsyncLocalStorage
3 participants