Skip to content

Commit f4a062a

Browse files
committed
Moved from a thread local to contextvar context for tracking asgi route handler args
1 parent 0164bf8 commit f4a062a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

ellar/asgi_args.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import typing as t
2+
3+
from ellar.types import TReceive, TScope, TSend
4+
5+
if t.TYPE_CHECKING:
6+
from ellar.di.providers import Provider
7+
8+
9+
class ASGIArgs:
10+
__slots__ = ("scope", "receive", "send", "context")
11+
12+
def __init__(self, scope: TScope, receive: TReceive, send: TSend) -> None:
13+
self.scope = scope
14+
self.receive = receive
15+
self.send = send
16+
self.context: t.Dict[t.Type, "Provider"] = {}
17+
18+
def get_args(self) -> t.Tuple[TScope, TReceive, TSend]:
19+
return self.scope, self.receive, self.send

ellar/constants.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import contextvars
12
import logging
23
from enum import Enum
3-
from typing import Any, Dict, List, no_type_check
4+
from typing import Any, Dict, List, Optional, no_type_check
45

56
from pydantic.fields import (
67
SHAPE_LIST,
@@ -10,6 +11,13 @@
1011
SHAPE_TUPLE_ELLIPSIS,
1112
)
1213

14+
from .asgi_args import ASGIArgs
15+
16+
ASGI_CONTEXT_VAR: contextvars.ContextVar[Optional[ASGIArgs]] = contextvars.ContextVar(
17+
"ASGI-CONTEXT-VAR"
18+
)
19+
ASGI_CONTEXT_VAR.set(None)
20+
1321

1422
class _AnnotationToValue(type):
1523
keys: List[str]

0 commit comments

Comments
 (0)