This commit is contained in:
Charis Nikolaidis 2026-02-17 10:03:33 +00:00 committed by GitHub
commit 13d617c86b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 7 deletions

View File

@ -232,16 +232,16 @@ class _DefaultLifespan:
return self
# Cache for endpoint context to avoid re-extracting on every request
_endpoint_context_cache: dict[int, EndpointContext] = {}
# Cache for endpoint context to avoid re-extracting on every request.
# Keyed on the function object itself (not id()) so that entries remain valid
# even if a function is garbage collected and its id is reused by a new object.
_endpoint_context_cache: dict[Callable[..., Any], EndpointContext] = {}
def _extract_endpoint_context(func: Any) -> EndpointContext:
"""Extract endpoint context with caching to avoid repeated file I/O."""
func_id = id(func)
if func_id in _endpoint_context_cache:
return _endpoint_context_cache[func_id]
if func in _endpoint_context_cache:
return _endpoint_context_cache[func]
try:
ctx: EndpointContext = {}
@ -255,7 +255,7 @@ def _extract_endpoint_context(func: Any) -> EndpointContext:
except Exception:
ctx = EndpointContext()
_endpoint_context_cache[func_id] = ctx
_endpoint_context_cache[func] = ctx
return ctx