chore: httpx retry transport + healthcheck stdlib + mypy config
- mcp_common/http.py: nuovo helper async_client() con AsyncHTTPTransport(retries=3) per gestire connection error transient + call_with_retry() generic async retry decorator. Sostituite 25 occorrenze httpx.AsyncClient(...) in deribit/hyperliquid/sentiment/ macro client. 5 nuovi test. - Dockerfile healthcheck: passato da python+httpx subprocess a stdlib urllib.request.urlopen() su tutti i 6 servizi MCP. Zero dipendenze esterne nel runtime check, timeout esplicito 3s, image leggermente più snella. - pyproject.toml: aggiunto [tool.mypy] python_version=3.13 con mypy_path multi-package + override ignore_missing_imports per i vendor SDK (pybit, alpaca, hyperliquid, pythonjsonlogger). mypy 1.20 in dev deps; ruff pinned 0.5.x. mcp_common passa mypy clean; 44 errori tipo pre-esistenti nei servizi affiorati ma non bloccanti — fix da pianificare separatamente. - 455 test verdi. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""HTTP client factory con retry/backoff su errori transient.
|
||||
|
||||
Wrap leggero attorno a httpx.AsyncClient: aggiunge AsyncHTTPTransport
|
||||
con retries=N per gestire connection errors / DNS / refused. Per retry
|
||||
su 5xx HTTP response usa `request_with_retry()` (decoratore separato).
|
||||
|
||||
Usage standard:
|
||||
|
||||
async with async_client(timeout=15) as http:
|
||||
resp = await http.get(url)
|
||||
|
||||
Equivalente a httpx.AsyncClient(timeout=15) ma con retry transport su
|
||||
errori di livello connessione.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any, TypeVar
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
DEFAULT_RETRIES = 3
|
||||
DEFAULT_TIMEOUT = 15.0
|
||||
|
||||
|
||||
def async_client(
|
||||
*,
|
||||
timeout: float = DEFAULT_TIMEOUT,
|
||||
retries: int = DEFAULT_RETRIES,
|
||||
follow_redirects: bool = False,
|
||||
**kwargs: Any,
|
||||
) -> httpx.AsyncClient:
|
||||
"""httpx.AsyncClient con AsyncHTTPTransport(retries=N) di default.
|
||||
retries gestisce connection errors / refused / DNS — non 5xx HTTP.
|
||||
"""
|
||||
transport = httpx.AsyncHTTPTransport(retries=retries)
|
||||
return httpx.AsyncClient(
|
||||
timeout=timeout,
|
||||
transport=transport,
|
||||
follow_redirects=follow_redirects,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
async def call_with_retry(
|
||||
fn: Callable[[], Awaitable[T]],
|
||||
*,
|
||||
max_attempts: int = 3,
|
||||
base_delay: float = 0.5,
|
||||
max_delay: float = 8.0,
|
||||
retry_on: tuple[type[BaseException], ...] = (httpx.TransportError, httpx.TimeoutException),
|
||||
) -> T:
|
||||
"""Retry generico async con exponential backoff.
|
||||
|
||||
Ritenta `fn()` se solleva una delle exception in `retry_on`. Backoff
|
||||
raddoppia (0.5, 1, 2, 4, ...) clipped a max_delay. Solleva l'ultima
|
||||
exception se max_attempts raggiunto.
|
||||
|
||||
Usabile su SDK sincroni avvolti in asyncio.to_thread (pybit, alpaca):
|
||||
|
||||
result = await call_with_retry(lambda: client._run(self._http.get_tickers, ...))
|
||||
"""
|
||||
delay = base_delay
|
||||
last_exc: BaseException | None = None
|
||||
for attempt in range(1, max_attempts + 1):
|
||||
try:
|
||||
return await fn()
|
||||
except retry_on as e:
|
||||
last_exc = e
|
||||
if attempt == max_attempts:
|
||||
break
|
||||
logger.warning(
|
||||
"transient error, retrying (%d/%d) in %.1fs: %s",
|
||||
attempt, max_attempts, delay, type(e).__name__,
|
||||
)
|
||||
await asyncio.sleep(delay)
|
||||
delay = min(delay * 2, max_delay)
|
||||
assert last_exc is not None
|
||||
raise last_exc
|
||||
@@ -7,6 +7,7 @@ import uuid
|
||||
from collections.abc import Callable
|
||||
from contextlib import AbstractAsyncContextManager
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
@@ -28,7 +29,7 @@ def _error_envelope(
|
||||
details: dict | None = None,
|
||||
request_id: str | None = None,
|
||||
) -> dict:
|
||||
env = {
|
||||
env: dict[str, Any] = {
|
||||
"error": {
|
||||
"type": type_,
|
||||
"code": code,
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from mcp_common.http import async_client, call_with_retry
|
||||
|
||||
|
||||
def test_async_client_uses_retry_transport():
|
||||
c = async_client(retries=5)
|
||||
assert isinstance(c._transport, httpx.AsyncHTTPTransport)
|
||||
# internal _retries on transport
|
||||
assert c._transport._pool._retries == 5
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_call_with_retry_succeeds_first_try():
|
||||
calls = 0
|
||||
|
||||
async def fn():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
return "ok"
|
||||
|
||||
result = await call_with_retry(fn)
|
||||
assert result == "ok"
|
||||
assert calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_call_with_retry_recovers_after_transient(monkeypatch):
|
||||
monkeypatch.setattr(asyncio, "sleep", asyncio.coroutine(lambda *_: None) if False else _no_sleep)
|
||||
calls = 0
|
||||
|
||||
async def fn():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
if calls < 3:
|
||||
raise httpx.ConnectError("boom")
|
||||
return "ok"
|
||||
|
||||
result = await call_with_retry(fn, max_attempts=5, base_delay=0.0)
|
||||
assert result == "ok"
|
||||
assert calls == 3
|
||||
|
||||
|
||||
async def _no_sleep(_):
|
||||
return None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_call_with_retry_gives_up_after_max():
|
||||
calls = 0
|
||||
|
||||
async def fn():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
raise httpx.TimeoutException("slow")
|
||||
|
||||
with pytest.raises(httpx.TimeoutException):
|
||||
await call_with_retry(fn, max_attempts=3, base_delay=0.0)
|
||||
assert calls == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_call_with_retry_does_not_catch_unexpected():
|
||||
async def fn():
|
||||
raise ValueError("not transient")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await call_with_retry(fn, max_attempts=5, base_delay=0.0)
|
||||
Reference in New Issue
Block a user