fix: use shared httpx client to prevent premature closure in SSE streaming

The proxy was creating a temporary httpx.AsyncClient within an async context manager.
When returning StreamingResponse for SSE endpoints, the client was being closed before
the streaming generator could use it, causing "client has been closed" errors.

This change introduces a shared httpx.AsyncClient that persists for the application
lifecycle, properly cleaned up during shutdown. This also improves performance by
reusing TCP connections across requests.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hetaoBackend
2026-01-19 16:52:30 +08:00
parent 3a4149c437
commit ffb9ed3198
2 changed files with 75 additions and 47 deletions

View File

@@ -19,6 +19,8 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
logger.info(f"Proxying to LangGraph server at {config.langgraph_url}")
yield
logger.info("Shutting down API Gateway")
# Close the shared HTTP client
await proxy.close_http_client()
def create_app() -> FastAPI: