mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-09 22:31:41 +00:00
perf: add request.state caching to _safe_get_request_headers
Cache the dict(request.headers) result on request.state._cached_headers so subsequent calls within the same request return the cached dict instead of re-creating it each time.
This commit is contained in:
parent
7a2c889ec2
commit
e7175a5212
1 changed files with 17 additions and 5 deletions
|
|
@ -135,17 +135,29 @@ def _safe_set_request_parsed_body(
|
||||||
|
|
||||||
def _safe_get_request_headers(request: Optional[Request]) -> dict:
|
def _safe_get_request_headers(request: Optional[Request]) -> dict:
|
||||||
"""
|
"""
|
||||||
[Non-Blocking] Safely get the request headers
|
[Non-Blocking] Safely get the request headers.
|
||||||
|
Caches the result on request.state to avoid re-creating dict(request.headers) per call.
|
||||||
|
|
||||||
|
Warning: Callers must NOT mutate the returned dict — it is shared across
|
||||||
|
all callers within the same request via the cache.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
if request is None:
|
if request is None:
|
||||||
return {}
|
return {}
|
||||||
return dict(request.headers)
|
cached = getattr(request.state, "_cached_headers", None)
|
||||||
|
if cached is not None:
|
||||||
|
return cached
|
||||||
|
try:
|
||||||
|
headers = dict(request.headers)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
verbose_proxy_logger.debug(
|
verbose_proxy_logger.debug(
|
||||||
"Unexpected error reading request headers - {}".format(e)
|
"Unexpected error reading request headers - {}".format(e)
|
||||||
)
|
)
|
||||||
return {}
|
headers = {}
|
||||||
|
try:
|
||||||
|
request.state._cached_headers = headers
|
||||||
|
except Exception:
|
||||||
|
pass # request.state may not be available in all contexts
|
||||||
|
return headers
|
||||||
|
|
||||||
|
|
||||||
def check_file_size_under_limit(
|
def check_file_size_under_limit(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue