citusdata / django-multitenant

Python/Django support for distributed multi-tenant databases like Postgres+Citus
MIT License
730 stars 118 forks source link

Support for async-safety through asgiref's Local #176

Open aprams opened 1 year ago

aprams commented 1 year ago

We are looking to switch to ASGI soon and are freshly using django-multitenant as well. As the threading.local is being used by django-multitenant, I'm afraid of cross-request tenant leakages. I therefore suggest an approach similar to django-simple-history:

try:
    from asgiref.local import Local as LocalContext
except ImportError:
    from threading import local as LocalContext

This should use asgiref's Local as an asgi-safe alternative to threading.locals as per my understanding. As asgiref.local is meant as a drop-in replacement, the change should be straightforward.

arpanpreneur commented 1 year ago

Yes, even I wanted to understand if django-multitenant is compatible with ASGI and Channels server. I am new to Channels and ASGI, so need some advice before I port my current Django app which uses this library into an ASGI mode for some of the new features.

arpanpreneur commented 1 year ago

@darwing1210 Do we have an eta when this can be released?

darwing1210 commented 1 year ago

@darwing1210 Do we have an eta when this can be released?

I'm not sure, as it was never reviewed by the repo owners

gurkanindibay commented 1 year ago

Hey @darwing1210 @arpanpreneur I'm reviewing right now. Thanks for your contributions

aprams commented 7 months ago

@gurkanindibay This can be closed as per https://github.com/citusdata/django-multitenant/commit/fd84aeee9f71f66775a5f4256102f9010418a940 right?

Thanks for the contributions - amazing project, thanks a lot! :)