Skip to content

Commit 3d77a1c

Browse files
authored
Fix high memory usage when using BaseHTTPMiddleware middleware classes and streaming responses (#1018)
* BaseHTTPMiddleware add maxsize arg to Queue constructor - Limit queue size to 1 to prevent loading entire streaming response into memory
1 parent 1db8010 commit 3d77a1c

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

starlette/middleware/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from starlette.requests import Request
55
from starlette.responses import Response, StreamingResponse
6-
from starlette.types import ASGIApp, Receive, Scope, Send
6+
from starlette.types import ASGIApp, Message, Receive, Scope, Send
77

88
RequestResponseEndpoint = typing.Callable[[Request], typing.Awaitable[Response]]
99
DispatchFunction = typing.Callable[
@@ -27,7 +27,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
2727

2828
async def call_next(self, request: Request) -> Response:
2929
loop = asyncio.get_event_loop()
30-
queue = asyncio.Queue() # type: asyncio.Queue
30+
queue: "asyncio.Queue[typing.Optional[Message]]" = asyncio.Queue(maxsize=1)
3131

3232
scope = request.scope
3333
receive = request.receive

0 commit comments

Comments
 (0)