Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/vllm_router/routers/routing_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,16 @@ async def route_request(
"model": endpoints[0].model_names[0],
"prompt": request_json.get("prompt", ""),
}
body = requests.post(
remote_url, headers=headers, json=data, timeout=10
).json()
token_ids = body["tokens"]
# Run the blocking HTTP call in an executor so it does not
# stall the event loop (mirrors tokenize_prompt fallback).
loop = asyncio.get_running_loop()
response = await loop.run_in_executor(
None,
lambda: requests.post(
remote_url, headers=headers, json=data, timeout=10
),
)
token_ids = response.json()["tokens"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since route_request is an asynchronous function and has access to the request object, we can leverage the shared aiohttp client session from request.app.state.aiohttp_client_wrapper() instead of using requests.post in an executor.

Using requests.post without a session creates a new TCP connection for every fallback request, which is highly inefficient under load and can lead to socket exhaustion. Utilizing the shared aiohttp client session enables connection reuse and avoids the overhead of thread-pool execution.

            client = request.app.state.aiohttp_client_wrapper()
            async with client.post(
                remote_url, headers=headers, json=data, timeout=10
            ) as response:
                response_json = await response.json()
                token_ids = response_json["tokens"]


event_id = "Lookup" + str(uuid.uuid4())
msg = LookupMsg(tokens=token_ids, event_id=event_id)
Expand Down