fix(router): unify HTTP client singletons to prevent socket exhaustion#53
Conversation
There was a problem hiding this comment.
Sorry @sheepdestroyer, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
|
Warning Review limit reached
More reviews will be available in 26 minutes and 48 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request refactors router/main.py to use a shared HTTP client via get_http_client() instead of creating inline httpx.AsyncClient instances, while preserving the respective request timeouts. Feedback on these changes highlights two main issues: first, the shared global client's default connection limit of 100 may be easily exhausted under moderate load, so configuring custom limits is recommended; second, the proxy_models endpoint is vulnerable to a KeyError if the upstream LiteLLM service returns an error response without a 'data' key, which should be resolved by safely checking the response status and structure before modifying the payload.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| litellm_master_key = os.getenv("LITELLM_MASTER_KEY", "") | ||
| max_wait = 180 | ||
| logger.info(f"⏳ Waiting for LiteLLM on {LITELLM_URL} (max {max_wait}s)...") | ||
| client = get_http_client() |
There was a problem hiding this comment.
The shared global httpx.AsyncClient is initialized with default limits (max_connections=100). Since this client is now shared across all endpoints and background tasks (including long-lived streaming proxy requests), a limit of 100 concurrent connections can easily be exhausted under moderate load, leading to httpx.PoolTimeout errors.
Consider configuring the global client with custom limits to support higher concurrency, for example:
_http_client = httpx.AsyncClient(
timeout=3600.0,
limits=httpx.Limits(max_connections=1000, max_keepalive_connections=500)
)14aa555 to
fff00c0
Compare
Replaced all localized `httpx.AsyncClient` context managers in `router/main.py` with the shared global client from `get_http_client()`. Specific timeouts are now passed directly to request methods. This change improves connection pooling and reduces the risk of socket leaks (TIME_WAIT accumulation) under heavy concurrency. Functions updated: - sync_adaptive_router_roster - _register_ollama_models_in_db - lifespan - check_http_endpoint - classify_request - get_llamacpp_metrics - get_best_free_model - proxy_memory - proxy_models Co-authored-by: sheepdestroyer <1377479+sheepdestroyer@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…y to proxy_models
fff00c0 to
e2badb8
Compare
Description
This pull request unifies all HTTP client instances in
router/main.pyto use a shared globalhttpx.AsyncClientsingleton. Re-creatingAsyncClienton every request/endpoint call can lead to socket exhaustion and adds unnecessary TCP handshake overhead.Key Changes
get_http_client()which lazily instantiates a shared globalAsyncClientwith a high timeout default.master(incorporating dynamic URL environment variables from PR Extract hardcoded microservice endpoint bindings #47). Integratedget_http_client()inside:get_llamacpp_metrics()proxy_models()lifespan()wait-readiness loopIndentationErrorin theproxy_memoryresponse block.await _http_client.aclose()) when the application shuts down.Verification & Testing
python -m py_compile router/main.pypytest test_antigravity.py test_circuit_breaker.py(9/9 passed)Supersedes #50.