-
-
Notifications
You must be signed in to change notification settings - Fork 480
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
Use sync/async pattern for middleware since Django 3.1 #1209
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1209 +/- ##
==========================================
- Coverage 97.38% 97.08% -0.30%
==========================================
Files 23 23
Lines 1263 1271 +8
Branches 204 208 +4
==========================================
+ Hits 1230 1234 +4
- Misses 16 19 +3
- Partials 17 18 +1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@john-parton Nice! What do you think about rewriting it so that I'm planning on making it class-based myself if I don't receive any response within a week or so 🙂 |
It's surprisingly difficult to properly subclass a middleware that offers both sync and async functionality. I think it probably just presents a foot-gun. I can't think of any use-case which subclasses such a simple middleware. Any customization I think would be done by layering a different middleware before or after it. If you give me some actual use-case, I can refactor it, otherwise you're welcome to do so. |
Hm yeah, good points. I also happened to find this example, illustrating some additional downsides. |
#1188 has already received an approval and will probably be merged within a week or so, which will lead to conflicts in this PR. I can post a comment here when it's merged, if you'd like to resolve those conflicts; otherwise, I can just do it myself (keeping the middleware function-based) and then merge 🙂 |
Yeah, let me know when it's merged. I can take a crack at it. |
@john-parton It's been merged now :) |
for more information, see https://pre-commit.ci
@ddabble Updated. I originally just had the logic copy/pasted in the sync and async versions, but that was nasty, so I encapsulated the setting and deleting of the property with a helper. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you! 😊
Reworks the middleware so that it uses the recommended pattern in django docs for async/sync middleware.
Description
Changed middleware to use pattern from docs: https://docs.djangoproject.com/en/4.2/topics/http/middleware/#asynchronous-support
Related Issue
Motivation and Context
The current middleware is synchronous only. As Django is moving towards a more async capable stack, this puts the project inline with Django's goals.
How Has This Been Tested?
Didn't.
Screenshots (if appropriate):
Types of changes
Checklist:
pre-commit run
command to format and lint.AUTHORS.rst
CHANGES.rst