Skip to content

Commit 4f1834f

Browse files
fix fastapi, nginx, and entrypoint for sse
1 parent 5945903 commit 4f1834f

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

backend/app/routers/generate.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import re
1616
import json
1717
import asyncio
18+
from fastapi.responses import JSONResponse
1819

1920
# from app.services.claude_service import ClaudeService
2021
# from app.core.limiter import limiter
@@ -276,6 +277,12 @@ def replace_path(match):
276277

277278
@router.post("/stream")
278279
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+
279286
try:
280287
# Initial validation checks
281288
if len(body.instructions) > 1000:
@@ -415,6 +422,8 @@ async def event_generator():
415422
except Exception as e:
416423
yield f"data: {json.dumps({'error': str(e)})}\n\n"
417424

418-
return StreamingResponse(event_generator(), media_type="text/event-stream")
425+
return StreamingResponse(
426+
event_generator(), media_type="text/event-stream", headers=headers
427+
)
419428
except Exception as e:
420-
return {"error": str(e)}
429+
return JSONResponse(status_code=500, content={"error": str(e)})

backend/entrypoint.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ if [ "$ENVIRONMENT" = "development" ]; then
77
exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
88
elif [ "$ENVIRONMENT" = "production" ]; then
99
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
1118
else
1219
echo "ENVIRONMENT must be set to either 'development' or 'production'"
1320
exit 1

backend/nginx/api.conf

+12-11
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@ server {
1616
include proxy_params;
1717
proxy_redirect off;
1818

19-
# Add these new settings
19+
# SSE specific settings
2020
proxy_buffering off;
21+
proxy_cache off;
2122
proxy_http_version 1.1;
2223
proxy_set_header Connection '';
2324
proxy_set_header X-Real-IP $remote_addr;
2425
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2526
proxy_set_header Host $host;
2627

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;
3233

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;
3637

3738
# Keep connection alive
38-
keepalive_timeout 300;
39-
keepalive_requests 100;
39+
keepalive_timeout 300s;
40+
keepalive_requests 1000;
4041
}
4142

4243
# Return 444 for everything else (no response, just close connection)

0 commit comments

Comments
 (0)