Proxy

Proxy Retry

IN THIS DOCUMENT

Crimson marks the heading currently in view; every entry is one of the document's own headings.

Transient network failures to the upstream LLM API don’t fail the entire request.

A bounded retry loop with exponential backoff and jitter avoids both immediate failure and thundering-herd retry storms - but only failures that happen before the request reaches the upstream are retried, so a request the upstream may have accepted is never re-sent.

Algorithm

for attempt in 1..=3:
    build_request()
    match send():
        Ok(response) → return response
        Err(error):
            if attempt < 3 && error.is_connect():
                sleep(backoff)
                continue
            else:
                fail fast → 502 BAD_GATEWAY

Backoff Formula

base_ms = 100 × 2^(attempt - 1)
jitter  = random(0.75 .. 1.25)
sleep_ms = base_ms × jitter
AttemptBase (ms)Range (ms)
110075 - 125
2200150 - 250
3(not retried, final attempt)-

As a Rust struct:

let base_ms = 100 * 2u64.pow(attempt - 1);
let jitter = rand::random::<f64>() * 0.5 + 0.75; // 0.75x to 1.25x
let ms = (base_ms as f64 * jitter) as u64;

Retry Scope

Only connect-phase failures are retried - errors reqwest classifies as is_connect(), which means the request never left and resending is safe: connection refused, DNS resolution failure, TLS handshake error, and connect timeouts.

Everything else fails fast on the first attempt.

Why the narrow scope: a post-send request timeout (e.is_timeout() after the body was already transmitted) may have been accepted by the upstream.

Blindly retrying a non-idempotent POST /v1/chat/completions risks double token billing, and combined with the 300s per-attempt timeout, 3 blind retries could hold a client for ~15 minutes.

Non-connect errors now fail fast on the first attempt instead.

Retried (connect-phase)Not Retried (fail fast)
Connection refusedHTTP 4xx (tracked as upstream_errors_4xx)
DNS resolution failureHTTP 5xx (tracked as upstream_errors_5xx)
TLS handshake errorPost-send request timeouts
Connect timeoutMid-body connection reset (request was sent)
Any other transport error

Final Failure

After the retry budget is exhausted (or a non-connect error fails on the first attempt):

  • Increment upstream_timeouts when the final error is a timeout, otherwise increment upstream_connect_errors - the two counters are disjoint.
  • Record the specific error in the last_errors ring buffer (max 100), visible via /stats.
  • Return 502 BAD_GATEWAY with a deliberately generic JSON error body:
{ "error": "upstream request failed" }

The specific transport error is never sent to the client - reqwest::Error’s Display can embed the upstream URL/host, which would leak the configured api_url to whoever hit the proxy.

The detail lives server-side in last_errors//stats only.

Upstream Timeout

Separate from retry: the HTTP client has a global timeout that applies to each individual attempt.

A single slow request can consume up to timeout seconds before the retry mechanism (for connect-phase failures only) kicks in.

.timeout(Duration::from_secs(cli.timeout))  // default 300s, max 600s

Timeout clamping:

let t = cfg.timeout.unwrap_or(300);
if t > 600 {
    tracing::warn!("timeout {}s exceeds maximum 600s, clamping", t);
    600
} else { t }

Streaming exemption: "stream": true requests go out on a separate client with no total timeout - reqwest’s client-level .timeout() bounds the whole request including the response body stream, which used to cut off legitimately slow but progressing SSE streams mid-answer.

Hang protection for streams comes from connect_timeout + tcp_keepalive instead.

See Architecture: Streaming (SSE).