File tree 3 files changed +31
-14
lines changed
3 files changed +31
-14
lines changed Original file line number Diff line number Diff line change 15
15
import re
16
16
import json
17
17
import asyncio
18
+ from fastapi .responses import JSONResponse
18
19
19
20
# from app.services.claude_service import ClaudeService
20
21
# from app.core.limiter import limiter
@@ -276,6 +277,12 @@ def replace_path(match):
276
277
277
278
@router .post ("/stream" )
278
279
async def generate_stream (request : Request , body : ApiRequest ):
280
+ headers = {
281
+ "Cache-Control" : "no-cache" ,
282
+ "Connection" : "keep-alive" ,
283
+ "X-Accel-Buffering" : "no" , # Important for Nginx
284
+ }
285
+
279
286
try :
280
287
# Initial validation checks
281
288
if len (body .instructions ) > 1000 :
@@ -415,6 +422,8 @@ async def event_generator():
415
422
except Exception as e :
416
423
yield f"data: { json .dumps ({'error' : str (e )})} \n \n "
417
424
418
- return StreamingResponse (event_generator (), media_type = "text/event-stream" )
425
+ return StreamingResponse (
426
+ event_generator (), media_type = "text/event-stream" , headers = headers
427
+ )
419
428
except Exception as e :
420
- return {"error" : str (e )}
429
+ return JSONResponse ( status_code = 500 , content = {"error" : str (e )})
Original file line number Diff line number Diff line change @@ -7,7 +7,14 @@ if [ "$ENVIRONMENT" = "development" ]; then
7
7
exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
8
8
elif [ " $ENVIRONMENT " = " production" ]; then
9
9
echo " Starting in production mode with multiple workers..."
10
- exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --timeout-keep-alive 300 --workers 2
10
+ exec uvicorn app.main:app \
11
+ --host 0.0.0.0 \
12
+ --port 8000 \
13
+ --timeout-keep-alive 300 \
14
+ --workers 2 \
15
+ --backlog 2048 \
16
+ --limit-concurrency 500 \
17
+ --no-access-log
11
18
else
12
19
echo " ENVIRONMENT must be set to either 'development' or 'production'"
13
20
exit 1
Original file line number Diff line number Diff line change @@ -16,27 +16,28 @@ server {
16
16
include proxy_params;
17
17
proxy_redirect off;
18
18
19
- # Add these new settings
19
+ # SSE specific settings
20
20
proxy_buffering off;
21
+ proxy_cache off;
21
22
proxy_http_version 1.1;
22
23
proxy_set_header Connection '';
23
24
proxy_set_header X-Real-IP $remote_addr;
24
25
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
25
26
proxy_set_header Host $host;
26
27
27
- # Increase timeouts
28
- proxy_connect_timeout 300 ;
29
- proxy_send_timeout 300 ;
30
- proxy_read_timeout 300 ;
31
- send_timeout 300 ;
28
+ # Increase timeouts significantly
29
+ proxy_connect_timeout 300s ;
30
+ proxy_send_timeout 300s ;
31
+ proxy_read_timeout 300s ;
32
+ send_timeout 300s ;
32
33
33
- # Add error handling
34
- proxy_next_upstream error timeout http_500 http_502 http_503 http_504 ;
35
- proxy_next_upstream_tries 2 ;
34
+ # Important for SSE
35
+ chunked_transfer_encoding on ;
36
+ proxy_set_header X-Accel-Buffering no ;
36
37
37
38
# Keep connection alive
38
- keepalive_timeout 300 ;
39
- keepalive_requests 100 ;
39
+ keepalive_timeout 300s ;
40
+ keepalive_requests 1000 ;
40
41
}
41
42
42
43
# Return 444 for everything else (no response, just close connection)
You can’t perform that action at this time.
0 commit comments