This commit is contained in:
2026-08-13 12:59:53 +02:00
parent 45ad8e79ed
commit b97b5a7854
5 changed files with 42 additions and 32 deletions
+18 -21
View File
@@ -198,34 +198,31 @@ class GatewayRuntime:
self.in_flight += 1
if self.in_flight > self.peak_in_flight:
self.peak_in_flight = self.in_flight
wait_start = time.monotonic()
connect_holder = {"ms": 0.0}
attempts = 1
resp = None
exc = None
try:
async with sem:
timing["queue_wait_ms"] = round(
(time.monotonic() - wait_start) * 1000, 3
)
async def do_call():
request = client.build_request(
method, url, headers=headers, json=json_body, content=content
)
request.extensions["trace"] = _connect_tracer(connect_holder)
return await client.send(request)
async def do_call():
request = client.build_request(
method, url, headers=headers, json=json_body, content=content
)
request.extensions["trace"] = _connect_tracer(connect_holder)
return await client.send(request)
send_start = time.monotonic()
resp, exc, attempts = await retry_send(
do_call,
cfg["gateway_max_retries"],
cfg["gateway_retry_backoff_ms"],
log,
)
timing["upstream_latency_ms"] = round(
(time.monotonic() - send_start) * 1000, 3
)
send_start = time.monotonic()
resp, exc, attempts, queue_wait_ms = await retry_send(
do_call,
sem,
cfg["gateway_max_retries"],
cfg["gateway_retry_backoff_ms"],
log,
)
timing["upstream_latency_ms"] = round(
(time.monotonic() - send_start) * 1000, 3
)
timing["queue_wait_ms"] = round(queue_wait_ms, 3)
finally:
self.in_flight -= 1
timing["connect_ms"] = round(connect_holder["ms"], 3)
@@ -66,21 +66,30 @@ async def _backoff(backoff_ms: int, attempt: int) -> None:
async def retry_send(
do_call: Callable[[], Awaitable[httpx.Response]],
sem: asyncio.Semaphore,
max_retries: int,
backoff_ms: int,
log: Optional[Callable[[str], None]] = None,
) -> tuple[Optional[httpx.Response], Optional[Exception], int]:
) -> tuple[Optional[httpx.Response], Optional[Exception], int, float]:
log = log or (lambda message: None)
attempts = 0
last_exc: Optional[Exception] = None
queue_wait_ms = 0.0
while attempts <= max_retries:
attempts += 1
try:
resp = await do_call()
except httpx.RequestError as exc:
wait_start = time.monotonic()
async with sem:
queue_wait_ms += (time.monotonic() - wait_start) * 1000
try:
resp = await do_call()
exc = None
except httpx.RequestError as e:
resp = None
exc = e
if exc is not None:
last_exc = exc
if attempts > max_retries:
return None, exc, attempts
return None, exc, attempts, queue_wait_ms
log(
f"upstream connection failed, retrying ({attempts}/{max_retries}): {exc}"
)
@@ -90,5 +99,5 @@ async def retry_send(
log(f"upstream {resp.status_code}, retrying ({attempts}/{max_retries})")
await _backoff(backoff_ms, attempts)
continue
return resp, None, attempts
return None, last_exc, attempts
return resp, None, attempts, queue_wait_ms
return None, last_exc, attempts, queue_wait_ms