Files
lasuite-docs/src/backend/core/middleware.py
T
Manuel Raynaud 0b8808f9a7 🐛(backend) skip session creation for the readiness probe
The readiness probe should also not create a new session. A new session
will live in redis and increase the number of keys inside it for
nothing. The readiness path is isgnored in the ForceSessionMiddleware
2026-09-08 14:44:57 +02:00

40 lines
1.3 KiB
Python

"""Force session creation for all requests."""
from django.utils.deprecation import MiddlewareMixin
# Paths that must never touch the session store. Liveness and readiness
# probes should never create a new session in redis. Also, the liveness probe
# should never reach redis before returning its answer.
SESSION_EXEMPT_PATHS = (
"/__lbheartbeat__",
"/__heartbeat__",
)
class ForceSessionMiddleware(MiddlewareMixin):
"""
Force session creation for unauthenticated users.
Must be used after Authentication middleware.
"""
def process_request(self, request):
"""Force session creation for unauthenticated users."""
# Check the path before touching `request.user`: evaluating it loads
# the session from the cache backend.
if request.path.rstrip("/") in SESSION_EXEMPT_PATHS:
return
if not request.user.is_authenticated and request.session.session_key is None:
request.session.create()
class SaveRawBodyMiddleware(MiddlewareMixin):
"""
Save the raw request body to use it later.
"""
def process_request(self, request):
"""Save the raw request body in the request to use it later."""
if request.path.endswith(("/ai-proxy/", "/ai-proxy")):
request.raw_body = request.body