多服务 SSE 管道
端到端 SSE 管道设计:Python→Go→前端的事件透传、各层缓冲配置、超时链路、错误传播。
#type / concept
#status / growing
#tech / architecture
[!info] related notes
多服务 SSE 管道
数据流
Python AI 服务 (yield SSE events)
↓ http response
Go 后端 (bufio.Scanner + Flush)
↓ http response
nginx/Caddy (proxy_buffering off)
↓ http response
前端 (fetch + ReadableStream)
各层关键配置
Python(生产者)
- yield SSE 事件,每个事件是
event:xxx\ndata:{...}\n\n
Go(代理)
X-Accel-Buffering: no禁用 nginx 缓冲- 逐行
Flush() - 检测
c.Request.Context().Err()处理客户端断开
nginx
proxy_buffering offproxy_read_timeout 300s
Caddy
- 默认不缓冲 SSE
- 设置合理的
read_timeout
前端
decoder.decode(value, { stream: true })处理跨 chunk 中文- AbortController 中断
超时链路
前端 AbortController: 用户可随时中止
Go http.Client: 5 分钟
nginx proxy_read_timeout: 300s
Python LLM: 取决于模型
每层的超时要大于下一层,否则中间层会先超时。
常见问题
事件丢失
原因:缓冲区没 flush。 解决方案:每行数据后立即 Flush。
连接挂起
超时设置太长或没有超时。
中文乱码
decoder.decode(value, { stream: true }) 保留跨 chunk 字符。