-
Notifications
You must be signed in to change notification settings - Fork 357
feat(router): use aiohttp session for health checks #776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from typing import Any, AsyncGenerator, Callable | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import aiohttp | ||
| import pytest | ||
| from aiohttp import web | ||
| from fastapi import FastAPI | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def mock_app() -> AsyncGenerator[FastAPI]: | ||
| mock_app = MagicMock() | ||
| async with aiohttp.ClientSession() as session: | ||
| mock_app.state.aiohttp_client_wrapper = MagicMock(return_value=session) | ||
| yield mock_app | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def make_mock_engine(aiohttp_client: Any) -> Callable[[dict[str, Callable]], str]: | ||
| async def _make_mock_engine(routes: dict[str, Callable]) -> str: | ||
| app = web.Application() | ||
| for path, handler in routes.items(): | ||
| app.router.add_post(path, handler) | ||
|
|
||
| client = await aiohttp_client(app) | ||
| return str(client.make_url("")) | ||
|
|
||
| return _make_mock_engine | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import wave | ||
| from typing import Optional | ||
|
|
||
| import requests | ||
| import aiohttp | ||
| from fastapi.requests import Request | ||
| from starlette.datastructures import MutableHeaders | ||
|
|
||
|
|
@@ -222,36 +222,44 @@ def update_content_length(request: Request, request_body: str): | |
| request._headers = headers | ||
|
|
||
|
|
||
| def is_model_healthy(url: str, model: str, model_type: str) -> bool: | ||
| async def is_model_healthy( | ||
| session: aiohttp.ClientSession, url: str, model: str, model_type: str | ||
| ): | ||
| model_url = ModelType.get_url(model_type) | ||
|
|
||
| try: | ||
| if model_type == "transcription": | ||
| # for transcription, the backend expects multipart/form-data with a file | ||
| # we will use pre-generated silent wav bytes | ||
| response = requests.post( | ||
| f"{url}{model_url}", | ||
| files=ModelType.get_test_payload(model_type), # multipart/form-data | ||
| data={"model": model}, | ||
| timeout=10, | ||
| test_payload = ModelType.get_test_payload(model_type) | ||
| form_data = aiohttp.FormData() | ||
| form_data.add_field( | ||
| "file", | ||
| test_payload["file"][1], | ||
| filename=test_payload["file"][0], | ||
| content_type=test_payload["file"][2], | ||
| ) | ||
| form_data.add_field("model", model) | ||
|
|
||
| async with session.post( | ||
| f"{url}{model_url}", | ||
| data=form_data, | ||
| timeout=aiohttp.ClientTimeout(total=10), | ||
| ) as response: | ||
| response.raise_for_status() | ||
| return True | ||
| else: | ||
| # for other model types (chat, completion, etc.) | ||
| response = requests.post( | ||
| async with session.post( | ||
| f"{url}{model_url}", | ||
| headers={"Content-Type": "application/json"}, | ||
| json={"model": model} | ModelType.get_test_payload(model_type), | ||
| timeout=10, | ||
| ) | ||
|
|
||
| response.raise_for_status() | ||
|
|
||
| if model_type == "transcription": | ||
| return True | ||
| else: | ||
| response.json() # verify it's valid json for other model types | ||
| return True # validation passed | ||
| timeout=aiohttp.ClientTimeout(total=10), | ||
| ) as response: | ||
| response.raise_for_status() | ||
| await response.json() # verify it's valid json for other model types | ||
| return True # validation passed | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| except aiohttp.ClientError as e: | ||
| logger.debug(f"{model_type} Model {model} at {url} is not healthy: {e}") | ||
| return False | ||
|
Comment on lines
+225
to
265
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is missing a return type hint. Additionally, the async def is_model_healthy(
session: aiohttp.ClientSession, url: str, model: str, model_type: str
) -> bool:
model_url = ModelType.get_url(model_type)
try:
post_kwargs = {
"timeout": aiohttp.ClientTimeout(total=10),
}
if model_type == "transcription":
# for transcription, the backend expects multipart/form-data with a file
# we will use pre-generated silent wav bytes
test_payload = ModelType.get_test_payload(model_type)
form_data = aiohttp.FormData()
form_data.add_field(
"file",
test_payload["file"][1],
filename=test_payload["file"][0],
content_type=test_payload["file"][2],
)
form_data.add_field("model", model)
post_kwargs["data"] = form_data
else:
# for other model types (chat, completion, etc.)
post_kwargs["headers"] = {"Content-Type": "application/json"}
post_kwargs["json"] = {"model": model} | ModelType.get_test_payload(model_type)
async with session.post(f"{url}{model_url}", **post_kwargs) as response:
response.raise_for_status()
if model_type != "transcription":
await response.json() # verify it's valid json for other model types
return True # validation passed
except aiohttp.ClientError as e:
logger.debug(f"{model_type} Model {model} at {url} is not healthy: {e}")
return False |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This follows the https://docs.pytest.org/en/stable/how-to/fixtures.html#factories-as-fixtures pattern.