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

Async compatible SQLPanel #1993

Merged
merged 22 commits into from
Sep 2, 2024
Merged

Conversation

salty-ivy
Copy link
Member

@salty-ivy salty-ivy commented Aug 20, 2024

Description

By instrumenting the SQLPanel in sync_to_async we can monkey patch all the connection in a dedicated thread and store those connections there meanwhile all cross compatible views and operations in async context will take place in that threaded thus they will use those patched db connections which will help us store all async and sync operations.

more explanation of sync_to_async: https://docs.djangoproject.com/en/4.2/topics/async/#sync-to-async

Fixes #1430
Fixes #932

Checklist:

  • I have added the relevant tests for this change.
  • I have added an item to the Pending section of docs/changes.rst.

@salty-ivy salty-ivy marked this pull request as ready for review August 25, 2024 11:24
@salty-ivy
Copy link
Member Author

@tim-schilling I think this is ready as per the discussion tests have been covered by #1835

Comment on lines 115 to 116
if panel.panel_id == "SQLPanel":
await sync_to_async(panel.enable_instrumentation)()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to do this within the SQLPanel.enable_instrumentation itself?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit odd to see specific logic for a single panel in the middleware. It'll be easier to understand the middleware if we don't have specific panel logic within it. It's also easier to explain why we do that in the panel and have it be in the place a person is most likely to be looking for it.

Copy link
Contributor

@elineda elineda Aug 27, 2024

Choose a reason for hiding this comment

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

if think for future panel or update the "panel.panel_id == "SQLPanel"" is not great
i would put something like this

if hasttr(panel, "aenable_instrumentation"):
    panel.aenable_instrumentation()
else:
    panel.enable_instrumentation()

This is more generic and if there is a "afunction" you can put in the afunction comment why it's needed

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I like that.

Copy link
Member Author

Choose a reason for hiding this comment

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

we only have one panel as of now that would utilise this pattern, though it seems really intuitive.

Copy link
Contributor

@elineda elineda Aug 27, 2024

Choose a reason for hiding this comment

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

yep but if a people create a third party panel he need to change the code of the core for make work. That's not a great thing.
Also you may finish with a thing like if panel.panel_id in [very long and boring array].... Not a beautiful code in my opinion.

@tim-schilling tim-schilling mentioned this pull request Aug 27, 2024
2 tasks
docs/spelling_wordlist.txt Outdated Show resolved Hide resolved
docs/spelling_wordlist.txt Outdated Show resolved Hide resolved
@@ -154,6 +154,9 @@ def enable_instrumentation(self):

Unless the toolbar or this panel is disabled, this method will be
called early in ``DebugToolbarMiddleware``. It should be idempotent.

Add aenable_instrumentation method to the panel class to support async
instrumentation logic.
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe an "if needed" ? it's not mandatory for async to have a aenable ?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup, although async instrumentation logic itself means that await is required in that case aenable_instrumentation would be the only choice?

@salty-ivy
Copy link
Member Author

@tim-schilling Apparently instrumentation is causing the lint fail, even if I have added it to word list.

@tim-schilling tim-schilling merged commit f95b40d into jazzband:main Sep 2, 2024
25 checks passed
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.

gather information from async functions It doesnot count query while making query from another thread
3 participants