Skip to content

Commit fce759e

Browse files
grillazzCopilot
andauthored
Update app/middleware/profiler.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 473990b commit fce759e

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

app/middleware/profiler.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22

33
from fastapi import Request
44
from pyinstrument import Profiler
5-
from starlette.middleware.base import (
6-
BaseHTTPMiddleware,
7-
RequestResponseEndpoint,
8-
)
95
from starlette.responses import HTMLResponse, Response
106

117

12-
class ProfilingMiddleware(BaseHTTPMiddleware):
13-
async def dispatch(
14-
self, request: Request, call_next: RequestResponseEndpoint
15-
) -> Response:
8+
class ProfilingMiddleware:
9+
def __init__(self, app):
10+
self.app = app
11+
12+
async def __call__(self, scope, receive, send) -> Response:
13+
# Only handle HTTP requests; pass through other scope types (e.g. WebSocket).
14+
if scope.get("type") != "http":
15+
return await self.app(scope, receive, send)
16+
17+
request = Request(scope, receive=receive)
18+
1619
if request.query_params.get("pyprofile") == "true":
1720
profiler = Profiler(interval=0.001, async_mode="enabled")
1821
profiler.start()
1922

20-
await call_next(request)
23+
async def _inner_send(message):
24+
# Swallow downstream response messages; we replace the response
25+
# entirely with the profiler HTML output, matching the previous behavior.
26+
return
27+
28+
await self.app(scope, receive, _inner_send)
2129

2230
profiler.stop()
23-
return HTMLResponse(
31+
response = HTMLResponse(
2432
profiler.output_html(),
25-
headers={"Content-Disposition": "attachment; filename=profile.html"},
33+
headers={
34+
"Content-Disposition": "attachment; filename=profile.html"
35+
},
2636
)
37+
return await response(scope, receive, send)
2738

28-
return await call_next(request)
39+
return await self.app(scope, receive, send)

0 commit comments

Comments
 (0)