The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
test(integration): wait for every worker to report the websocket session cap instead of sleeping (#47412)
yuneng-jiangb67003162a6f
diff --git a/tests/integration/_support/runtime.py b/tests/integration/_support/runtime.pyindex a657ce4dff0..c53c23d42b3 100644--- a/tests/integration/_support/runtime.py+++ b/tests/integration/_support/runtime.py@@ -33,8 +33,17 @@ async def worker_deployment_ids(model_name: str) -> JSONResponse:return JSONResponse(content={"model_ids": model_ids}, headers={"x-integration-worker-pid": str(os.getpid())})+async def worker_general_setting(name: str) -> JSONResponse:+ from litellm.proxy.proxy_server import proxy_config++ return JSONResponse(+ content={"value": proxy_config.settings.get(name)}, headers={"x-integration-worker-pid": str(os.getpid())}+ )++def configure_readiness() -> None:from litellm.proxy.proxy_server import appapp.add_api_route("/health/integration-readiness", worker_readiness, include_in_schema=False)app.add_api_route("/health/integration-deployment-ids", worker_deployment_ids, include_in_schema=False)+ app.add_api_route("/health/integration-general-setting", worker_general_setting, include_in_schema=False)diff --git a/tests/integration/providers/test_responses_websocket_session_limit.py b/tests/integration/providers/test_responses_websocket_session_limit.pyindex f2b3a48c6d1..4369a84e1cc 100644--- a/tests/integration/providers/test_responses_websocket_session_limit.py+++ b/tests/integration/providers/test_responses_websocket_session_limit.py@@ -9,6 +9,7 @@ import threadingimport timeimport uuidfrom collections.abc import Generator, Iterator, Mapping+from concurrent.futures import ThreadPoolExecutorfrom contextlib import contextmanagerfrom dataclasses import dataclassfrom pathlib import Path@@ -53,7 +54,7 @@ JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue])SESSION_LIMIT_FIELD: Final = "responses_websocket_session_limit_seconds"LIMIT_CLOSE_REASON: Final = "Session duration limit reached"RELOAD_INTERVAL_SECONDS: Final = 3-WORKER_SYNC_SECONDS: Final = RELOAD_INTERVAL_SECONDS + 7+WORKER_SYNC_DEADLINE_SECONDS: Final = 60CAP_SECONDS: Final = 60CAP_CLOSE_SLACK_SECONDS: Final = 15DEAF_CLOSE_SLACK_SECONDS: Final = 3@@ -839,11 +840,28 @@ def _holding_worker(workers: frozenset[int], client_port: int) -> int | None:return next((pid for pid in sorted(workers) if holds(pid)), None)-def _settled_on_every_worker(written_at: float) -> None:+def _session_limit_on_a_worker(candidate: Gateway) -> tuple[int, JsonValue]:+ response: Final = candidate.request(+ "GET",+ "/health/integration-general-setting",+ params={"name": SESSION_LIMIT_FIELD},+ headers={"connection": "close"},+ )+ assert response.status_code == 200, f"Session limit on a worker: {response.status_code} {response.text}"+ return int(response.headers["x-integration-worker-pid"]), JSON_OBJECT.validate_json(response.content)["value"]+++def _session_limits_by_worker(candidate: Gateway) -> tuple[tuple[int, JsonValue], ...]:+ with ThreadPoolExecutor(max_workers=8) as pool:+ probes: Final = tuple(pool.submit(_session_limit_on_a_worker, candidate) for _ in range(16))+ return tuple(probe.result() for probe in probes)+++def _settled_on_every_worker(candidate: Gateway, workers: frozenset[int], limit: JsonValue) -> None:eventually(- lambda: time.monotonic() - written_at,- lambda elapsed: elapsed >= WORKER_SYNC_SECONDS,- seconds=WORKER_SYNC_SECONDS + 5,+ lambda: _session_limits_by_worker(candidate),+ lambda answers: frozenset(pid for pid, _ in answers) >= workers and all(value == limit for _, value in answers),+ seconds=WORKER_SYNC_DEADLINE_SECONDS,)@@ -939,16 +957,14 @@ async def _override_workload(owned: OwnedProxy, key: str, model: str, database_u) as earlier:accepted_at: Final = time.monotonic()updates: Final = await _update_sequence(candidate, (7200, CAP_SECONDS, CAP_SECONDS))- written_at: Final = time.monotonic()stored_after_updates: Final = await asyncio.to_thread(_stored_limit, database_url)- await asyncio.to_thread(_settled_on_every_worker, written_at)+ await asyncio.to_thread(_settled_on_every_worker, candidate, workers, CAP_SECONDS)opened: Final = await _sockets_on_every_worker(proxy, key, workers)capped: Final = await asyncio.gather(*tuple(_capped_close(socket) for socket in opened))earlier_result: Final = await _held_turn(earlier, model, accepted_at)delete: Final = await asyncio.to_thread(_delete_limit, candidate)- deleted_at: Final = time.monotonic()stored_after_delete: Final = await asyncio.to_thread(_general_settings_row, database_url)- await asyncio.to_thread(_settled_on_every_worker, deleted_at)+ await asyncio.to_thread(_settled_on_every_worker, candidate, workers, None)restored: Final = await _idle_turn(proxy,key,