6
6
from fastapi .responses import JSONResponse , StreamingResponse
7
7
from helpers .errors import ModelLoadFailed , ModelNotReady
8
8
from httpx import URL , ConnectError , RemoteProtocolError
9
- from starlette .requests import Request
9
+ from starlette .requests import ClientDisconnect , Request
10
10
from starlette .responses import Response
11
11
from tenacity import Retrying , retry_if_exception_type , stop_after_attempt , wait_fixed
12
12
@@ -28,15 +28,20 @@ async def proxy(request: Request):
28
28
client : httpx .AsyncClient = request .app .state .proxy_client
29
29
url = URL (path = request .url .path , query = request .url .query .encode ("utf-8" ))
30
30
31
- # 5 mins request and 2 min connect timeouts
32
- # Large values; we don't want requests to fail due to timeout on the proxy
33
- timeout = httpx .Timeout (5 * 60.0 , connect = 2 * 60.0 )
31
+ # 2 min connect timeouts, no timeout for requests.
32
+ # We don't want requests to fail due to timeout on the proxy
33
+ timeout = httpx .Timeout (None , connect = 2 * 60.0 )
34
+ try :
35
+ request_body = await request .body ()
36
+ except ClientDisconnect :
37
+ # If the client disconnects, we don't need to proxy the request
38
+ return Response (status_code = 499 )
34
39
35
40
inf_serv_req = client .build_request (
36
41
request .method ,
37
42
url ,
38
43
headers = request .headers .raw ,
39
- content = await request . body () ,
44
+ content = request_body ,
40
45
timeout = timeout ,
41
46
)
42
47
0 commit comments