retry capacity-style upstream errors

This commit is contained in:
2026-06-29 15:54:36 +08:00
parent 778068af42
commit 283c14b6b4
8 changed files with 384 additions and 24 deletions
+48 -4
View File
@@ -123,6 +123,20 @@ function startFakeUpstream(port) {
]);
return;
}
if (parsed.test_capacity_error) {
createJsonResponse(
res,
parsed.test_capacity_status ?? 503,
{
error: {
message: parsed.test_capacity_message || "Selected model is at capacity. Please try a different model.",
type: "server_error",
},
},
{ "x-upstream-test": "capacity-error" },
);
return;
}
if (parsed.stream) {
createSseResponse(res, [
'data: {"type":"response.output_text.delta","delta":"hello"}\n\n',
@@ -273,11 +287,13 @@ async function run() {
listen_port: gatewayPort,
upstream_base_url: `http://127.0.0.1:${upstreamPort}`,
request_body_limit_bytes: 10 * 1024 * 1024,
endpoints: ["/responses", "/chat/completions", "/v1/responses", "/v1/chat/completions"],
reasoning_equals: [516],
non_stream_status_code: 502,
endpoints: ["/responses", "/chat/completions", "/v1/responses", "/v1/chat/completions"],
reasoning_equals: [516],
retryable_status_codes: [429, 503],
retryable_error_messages: ["Selected model is at capacity. Please try a different model."],
non_stream_status_code: 502,
stream_action: "strict_502",
log_match: true,
log_match: true,
health_path: "/__codex_retry_gateway/health",
};
@@ -349,6 +365,34 @@ async function run() {
`请求体大小记录异常: ${recoveredEntry?.request_body_bytes}`,
);
const capacityResponse = await fetch(`http://127.0.0.1:${gatewayPort}/responses`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ test_capacity_error: true }),
});
const capacityBody = await capacityResponse.json();
assert(capacityResponse.status === 502, `capacity error 未返回 502: ${capacityResponse.status}`);
assert(
capacityBody?.error?.code === "upstream_error_retry_triggered",
"capacity error 返回体未标记 retry trigger",
);
assert(
capacityBody?.error?.upstream_status_code === 503,
"capacity error 返回体未保留 upstream status",
);
const streamCapacityResponse = await fetch(`http://127.0.0.1:${gatewayPort}/responses`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ stream: true, test_capacity_error: true }),
});
const streamCapacityBody = await streamCapacityResponse.json();
assert(streamCapacityResponse.status === 502, `stream+capacity error 未返回 502: ${streamCapacityResponse.status}`);
assert(
streamCapacityBody?.error?.code === "upstream_error_retry_triggered",
"stream+capacity error 返回体未标记 retry trigger",
);
for (const streamPath of [
"/responses",
"/v1/responses",