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
Try to prevent millions of queries #25
Conversation
Thanks for contributing this. It seems like a good idea. I need to think about the implications a bit. |
If get_tenant_perms is a cached_property, what is the benefit in making is_staff and is_superuser cached_property? |
If the current tenant gets switched while an instance of a user exists this cached property will end up reflecting the permissions of the wrong tenant. It needs to be reset/cleared if the tenant is changed. See https://docs.djangoproject.com/en/2.2/ref/utils/#django.utils.functional.cached_property where they use del to reset the cached property. Thoughts? |
Probably none. We should get rid of this and let |
I think that we could drop using What do you think? |
Yeah I agree. I think implementing our own caching logic that's tenant aware would solve this. |
@@ -18,59 +19,60 @@ class Meta: | |||
# permissions matching the current schema, which means that this | |||
# user has no authorization, so we catch this exception and return | |||
# the appropriate False or empty set | |||
@cached_property | |||
def _get_tenant_perms(self): |
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.
nit but a property should not have a get
prefix imo
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.
Sure, thanks for pointing that out
So I was looking into the source code of
My approach is the check if the current schema is present in If the current schema is present then check if method that If both the schema value and the name of the decorated function is present then just return the stored value. Below is an example:
Here is the output:
The problem with above approach is that when you try to do I would appreciate any inputs on this. Let me know what you guys think of it. |
Looks good to me, but i'm not in the position to test it this week. My proposal is to to turn the keys Give it a go and write a pull-request with a testcase like you did with the print statements. Then we have a nice caching solution! |
@sshahbaj @foarsitter Thanks for the contributions. This looks like a good potential solution that addresses the underlying tenant switching / caching problem identified above that prevented this merge originally. @ivissani @philippbosch Do you have time to weigh in on these ideas / review / test? |
@foarsitter That's the catch actually. The
But in our case we need that |
@Viatrak I think this is the right approach. I created a new PR #69 based on the work of @Dresdn and @sshahbaj which IMHO is ready to merge. I'm running the code in production already, and the performance enhancements are way bigger than expected. I always thought my own code was slow and started adding caching here and there with little effect. Now with the caching in django-tenant-users I removed all my own caching, and my app is blazing fast. 🎉 |
Permission check was slowing down django admin to the point that it was unusable.
I transformed
_get_tenant_perms
into a@cached_property
to try to prevent literally thousands of repeated queries.Let me know what you think. I didn't give too much thought to whether this can introduce any problems somewhere else.