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

Use RATELIMIT_EXCEPTION_CLASS for configuration #247

Merged
merged 1 commit into from Dec 3, 2022

Conversation

Andrew-Chen-Wang
Copy link
Contributor

Fixes #242

A concern I have is performance. This seems like it is importing every time the view is called. I've implemented this like the rest of the package. However, I feel like this would be better if we instead made a "proxy" or global variable that we saved on first load of the class. The only problem I suppose would be dev reload.

cls = getattr(
settings, 'RATELIMIT_EXCEPTION_CLASS', Ratelimited)
raise (import_string(cls) if isinstance(cls, str) else cls)()
Copy link
Owner

Choose a reason for hiding this comment

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

Agree with doing this in general, as it makes testing far easier, but I also hear you on the performance concern. One option would be to move this into the top-level of the decorator function, which gets called far less often—though I admit I'm not positive that settings would always be initialized already, depending when it's executed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think making a global variable makes the most sense where the initial variable is set to None. On dev reload, a new thread is created by the web server, so a django setting "reload" would essentially be a new web server and thus the global variable will be reset to None.

The only concern would be when this package needs to support async caching and ORM. But that's a problem for another time.

I've also got classes tomorrow, so probably won't be able to get to this until next week.

docs/settings.rst Outdated Show resolved Hide resolved
@jsocol jsocol force-pushed the patch-2 branch 2 times, most recently from f0a4a78 to bf69fef Compare December 3, 2022 20:42
@jsocol
Copy link
Owner

jsocol commented Dec 3, 2022

Thanks for this @Andrew-Chen-Wang!

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.

Add DRF Middleware-like support with block=True
2 participants