mirror of https://github.com/tiangolo/fastapi.git
Merge 6240dc105a into 0127069d47
This commit is contained in:
commit
3ebace66a5
|
|
@ -4,6 +4,7 @@ import functools
|
||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
import types
|
import types
|
||||||
|
import weakref
|
||||||
from collections.abc import (
|
from collections.abc import (
|
||||||
AsyncIterator,
|
AsyncIterator,
|
||||||
Awaitable,
|
Awaitable,
|
||||||
|
|
@ -247,16 +248,19 @@ class _DefaultLifespan:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
# Cache for endpoint context to avoid re-extracting on every request
|
# Cache for endpoint context to avoid re-extracting on every request.
|
||||||
_endpoint_context_cache: dict[int, EndpointContext] = {}
|
# Uses WeakKeyDictionary so entries are automatically evicted when the endpoint
|
||||||
|
# function is garbage collected (e.g., when a dynamically created FastAPI app is
|
||||||
|
# destroyed), preventing both memory leaks and stale-ID lookups.
|
||||||
|
_endpoint_context_cache: weakref.WeakKeyDictionary[
|
||||||
|
Callable[..., Any], EndpointContext
|
||||||
|
] = weakref.WeakKeyDictionary()
|
||||||
|
|
||||||
|
|
||||||
def _extract_endpoint_context(func: Any) -> EndpointContext:
|
def _extract_endpoint_context(func: Any) -> EndpointContext:
|
||||||
"""Extract endpoint context with caching to avoid repeated file I/O."""
|
"""Extract endpoint context with caching to avoid repeated file I/O."""
|
||||||
func_id = id(func)
|
if func in _endpoint_context_cache:
|
||||||
|
return _endpoint_context_cache[func]
|
||||||
if func_id in _endpoint_context_cache:
|
|
||||||
return _endpoint_context_cache[func_id]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ctx: EndpointContext = {}
|
ctx: EndpointContext = {}
|
||||||
|
|
@ -270,7 +274,7 @@ def _extract_endpoint_context(func: Any) -> EndpointContext:
|
||||||
except Exception:
|
except Exception:
|
||||||
ctx = EndpointContext()
|
ctx = EndpointContext()
|
||||||
|
|
||||||
_endpoint_context_cache[func_id] = ctx
|
_endpoint_context_cache[func] = ctx
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue