INNER CODE UNIT · Python
add_process_time_header
cubist38/mlx-openai-server · app/server.py:560
async def add_process_time_header(request: Request, call_next):
"""Middleware to add processing time header and run cleanup.
Measures request processing time, appends an ``X-Process-Time``
header, and increments a simple request counter used to trigger
periodic memory cleanup for long-running processes.
"""
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
# Periodic memory cleanup for long-running processes
if hasattr(request.app.state, "request_count"):
request.app.state.request_count += 1
else:
request.app.state.request_count = 1