fix(control-plane): chat completions keeps Retry-After on a memory_pressure 503; gate test covers chat and messages

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-18 02:35:27 +02:00
co-authored by Claude Sonnet 5.5
parent 0b10800141
commit e4f4e1e9a9
2 changed files with 22 additions and 3 deletions
+7 -1
View File
@@ -490,6 +490,8 @@ pub async fn create(
return stream_response(response.into_body(), include_usage);
}
let status = response.status();
// a refusal that tells the client when to come back (503 memory_pressure) keeps its Retry-After through the re-wrapping below
let retry_after = response.headers().get(axum::http::header::RETRY_AFTER).cloned();
let bytes = match to_bytes(response.into_body(), MAX_EVENT).await {
Ok(b) => b,
Err(_) => {
@@ -522,7 +524,11 @@ pub async fn create(
};
value["error"]["param"] = json!(mapped);
}
return (status, Json(value)).into_response();
let mut refusal = (status, Json(value)).into_response();
if let Some(retry_after) = retry_after {
refusal.headers_mut().insert(axum::http::header::RETRY_AFTER, retry_after);
}
return refusal;
}
Json(completion(&value)).into_response()
}
+15 -2
View File
@@ -147,6 +147,19 @@ async fn a_reject_report_closes_the_gate_with_503_and_retry_after_and_shed_still
let body: Value = serde_json::from_str(&refused.text().await.unwrap()).unwrap();
assert_eq!(body["error"]["code"], "memory_pressure", "{body}");
assert!(body["error"]["message"].as_str().unwrap().contains("hard floor"));
// the Chat Completions and Messages dialects get the same 503 with the same Retry-After
for (path, body) in [
("/v1/chat/completions", json!({"model": "fixture", "messages": [{"role": "user", "content": "hi"}]})),
("/v1/chat/completions", json!({"model": "fixture", "stream": true, "messages": [{"role": "user", "content": "hi"}]})),
("/v1/messages", json!({"model": "fixture", "max_tokens": 16, "messages": [{"role": "user", "content": "hi"}]})),
] {
let r = stack.client.post(format!("{}{path}", stack.base)).header("content-type", "application/json").header("anthropic-version", "2023-06-01").body(body.to_string()).send().await.unwrap();
let status = r.status();
let retry = r.headers().get("retry-after").map(|v| v.to_str().unwrap().to_owned());
let text = r.text().await.unwrap_or_default();
assert_eq!(status, 503, "{path}: {text}");
assert_eq!(retry.as_deref(), Some("7"), "{path} keeps Retry-After: {text}");
}
// streaming requests are refused up front too: a proper 503, not a 200 SSE that fails inside
let refused = stack.post(&request(true)).await;
assert_eq!(refused.status(), 503);
@@ -161,9 +174,9 @@ async fn a_reject_report_closes_the_gate_with_503_and_retry_after_and_shed_still
let status: Value = serde_json::from_str(&stack.client.get(format!("{}/v1/status", stack.base)).send().await.unwrap().text().await.unwrap()).unwrap();
assert_eq!(status["memory"]["gate"]["level"], "normal");
assert_eq!(status["memory"]["gate_rejections"], 2);
assert_eq!(status["memory"]["gate_rejections"], 5);
let metrics: Value = serde_json::from_str(&stack.client.get(format!("{}/metrics", stack.base)).send().await.unwrap().text().await.unwrap()).unwrap();
assert_eq!(metrics["memory"]["gate_rejections"], 2);
assert_eq!(metrics["memory"]["gate_rejections"], 5);
stack.finish().await;
}