Skip to content

Commit

Permalink
tenant context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
byewokko committed Feb 27, 2025
1 parent 861d4a8 commit 2eeefe5
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions asab/contextvars.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextvars
import contextlib

# Contains tenant ID string
Tenant = contextvars.ContextVar("Tenant")
Expand All @@ -8,3 +9,12 @@

# Contains aiohttp.web.Request
Request = contextvars.ContextVar("Request")


@contextlib.contextmanager
def tenant_context(tenant):
tenant_ctx = Tenant.set(tenant)
try:
yield tenant
finally:
Tenant.reset(tenant_ctx)

0 comments on commit 2eeefe5

Please sign in to comment.