Skip to content

Commit 5359ad2

Browse files
ClaydeCodeMagicMock/mock.effective_git_name/139641991453328
authored andcommitted
Fix #38: enable streaming proxy with async chunk iteration
Pass stream=True to requests.request in signed_call.py so the response body is not buffered before being returned. Replace iter_content() in call_backend.py with an async generator that reads 8192-byte chunks and offloads each blocking socket read to a thread via asyncio.to_thread, eliminating ~28s of unnecessary latency.
1 parent c231fad commit 5359ad2

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

shard_core/service/signed_call.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ async def signed_request(
1515
*args, identity: Identity = None, **kwargs
1616
) -> requests.Response:
1717
auth = get_signature_auth(identity)
18-
response = await asyncio.to_thread(requests.request, *args, auth=auth, **kwargs)
18+
response = await asyncio.to_thread(
19+
requests.request, *args, auth=auth, stream=True, **kwargs
20+
)
1921
return response
2022

2123

shard_core/web/internal/call_backend.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import logging
23
from fastapi import APIRouter, Request
34

@@ -11,6 +12,15 @@
1112
router = APIRouter()
1213

1314

15+
async def async_iter_content(response, chunk_size=8192):
16+
iterator = response.iter_content(chunk_size=chunk_size)
17+
while True:
18+
chunk = await asyncio.to_thread(next, iterator, None)
19+
if chunk is None:
20+
break
21+
yield chunk
22+
23+
1424
@router.api_route("/call_backend/{rest:path}", methods=ALL_HTTP_METHODS)
1525
async def call_backend(rest: str, request: Request):
1626
base_url = settings().freeshard_controller.base_url
@@ -26,5 +36,5 @@ async def call_backend(rest: str, request: Request):
2636
return StreamingResponse(
2737
status_code=response.status_code,
2838
headers=response.headers,
29-
content=response.iter_content(),
39+
content=async_iter_content(response, chunk_size=8192),
3040
)

0 commit comments

Comments
 (0)