celestial_eagle_99230 on Discord (v0.1.2670, Codex on auto/gpt-6-sol,
eight ChatGPT Plus accounts, reported several times): every account
showed "休息中 · 529 · 失败 · 10 分钟后恢复", with "它已连续失败 5 次:
休息 10 分钟,每次更久". The request log had the ChatGPT backend's
{"type":"service_unavailable_error","code":"server_is_overloaded",
"message":"Our servers are currently overloaded. Please try again
later."} on /backend-api/codex/responses.
restAfterMarked read the 529 as failOther: the account's own failure,
counted on the backoff ladder (1m, 2m, 4m, 8m, 10m cap). An overload is
the vendor's servers, which every account meets alike, so one spell of
it benched all eight accounts for growing periods and left nothing to
answer. b58669eb0 (v0.1.2721) changed only how the overload is told to
Codex; origin/main still rested and climbed this way.
Now an overload (vendorOverloaded: a 529, or a 5xx whose words say
overloaded, the existing saidOverloaded rule: ChatGPT's
server_is_overloaded, Anthropic's overloaded_error, Gemini's 503 "The
model is overloaded") rests only the model on that account
(restID), 30s (overloadRest), or as long as Retry-After asks up to a
minute (overloadRestMost), every time alike, and counts no failure. The
next account is still asked, the serve loop's patience still waits out
an overload on every member as before, and when all stay busy the
client is told the overload as b58669eb0 tells it (unchanged here). Quota,
credit, rate limits, 401/403 and a plain 5xx rest and climb as before.
The one choke point covers every route that rests (serve loop, group
decider, plugin calls). A provider set never to rest (RestCapOff) still
doesn't.
docs/subsystems/gateway-routing.md says it beside fallbackCooldown.
Tests (internal/gateway/overload_rest_test.go), from the reporter's
literal error body:
- TestOverloadRestsNoAccount: three Codex accounts, two answering 529
server_is_overloaded with Retry-After 7, six requests: no account rests
whole, the model rests <=7s by retry-after with 0 failures.
- TestOverloadRestIsShortAndFlat: six overloads in a row for each of
ChatGPT 529, ChatGPT stream error, Anthropic 529 overloaded_error,
Gemini 503: model rest <=30s, 0 failures each time; Retry-After 600
is held to a minute; then a 500 is the first ladder step (1 failure),
a 401 rests the account (2), a usage_limit_reached is quota.
Broken by hand: with origin/main's routing.go both tests go red, the
rests climbing 1m, 2m, 4m, 8m, 10m by backoff for the same overloads.
Run on mote at this head: gofmt -l clean; GOOS=linux and GOOS=windows
go vet ./internal/gateway ok; go test -count=1 -timeout 45m
./internal/gateway: every test ok except
TestCodexPoolSaysTheClientsVersion, the known #4807 (another test's Codex
identity reaches it in a full package run); it and the new tests pass
run alone at this head.
Not tried: a real ChatGPT overload (can't be caused on demand); the
full ./... suite (another session's full suite was running on mote);
GUI suite (no GUI change: the rest shows with the existing "retry-after"
and "cooldown" wording). A real 5xx met during an overload's 30s rest
isn't counted (backingOff sees a failOther rest), so it rests at the
current step rather than climbing one; that errs toward resting less.
126 lines
5.1 KiB
Go
126 lines
5.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// What celestial_eagle_99230's request log had from the ChatGPT backend on
|
|
// /backend-api/codex/responses (Discord, v0.1.2670, eight Plus accounts on
|
|
// gpt-6-sol): a 529 whose error is the server's overload.
|
|
const chatgptOverloaded = `{"error":{"type":"service_unavailable_error","code":"server_is_overloaded","message":"Our servers are currently overloaded. Please try again later."}}`
|
|
|
|
// An upstream overload is the vendor's moment, not the account's fault:
|
|
// every Codex account that met ChatGPT's 529 server_is_overloaded rests
|
|
// its model alone, briefly (a Retry-After honoured up to a minute), and
|
|
// counts no failure, however often it comes. Before, each overload counted
|
|
// as the account failing: "failed 5 times in a row", every one of the
|
|
// eight accounts resting up to the 10-minute cap, and nothing left to
|
|
// answer the user (celestial_eagle_99230 on Discord).
|
|
func TestOverloadRestsNoAccount(t *testing.T) {
|
|
codexSignedIn(t, "b@example.com", "c@example.com")
|
|
if err := provider.SetRouting("codex", provider.Ordered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var tried []string
|
|
chatgptRefusing(t, &tried, func(w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Retry-After", "7")
|
|
w.WriteHeader(529)
|
|
io.WriteString(w, chatgptOverloaded)
|
|
}, "acct-2")
|
|
s := New()
|
|
// every request of the spell: the first two accounts overloaded, the
|
|
// third answers
|
|
rested := 0
|
|
for range 6 {
|
|
restingUntil.Lock()
|
|
for k := range restingUntil.m {
|
|
restingUntil.m[k] = time.Now().Add(-time.Second)
|
|
}
|
|
restingUntil.Unlock()
|
|
if code, out := codexAskOn(s, `{"model":"gpt-6-sol","stream":true,"input":"ping"}`); code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
for _, who := range []string{"codex@me@example.com", "codex@b@example.com"} {
|
|
if r, ok := restOf(who); ok {
|
|
t.Fatalf("%s rests whole for an overload: %+v", who, r)
|
|
}
|
|
r, ok := restOf(who + "/gpt-6-sol")
|
|
if !ok {
|
|
continue
|
|
}
|
|
rested++
|
|
if r.Why != failOther || r.Failures != 0 || r.By != "retry-after" || r.Model != "gpt-6-sol" || time.Until(r.Until) > 7*time.Second {
|
|
t.Fatalf("%s rests %v by %s, %d failures in a row, for an overload", who, time.Until(r.Until).Round(time.Second), r.By, r.Failures)
|
|
}
|
|
}
|
|
}
|
|
if rested == 0 {
|
|
t.Fatalf("no account's model rests (tried %v): the test proves nothing", tried)
|
|
}
|
|
}
|
|
|
|
// The rest an overload takes is the same each time and counts nothing,
|
|
// for every vendor's way of saying it; a Retry-After is honoured up to a
|
|
// minute. The account's own failures, a 401 and a usage limit, rest it
|
|
// as before, and climb the ladder from where the overloads left it.
|
|
func TestOverloadRestIsShortAndFlat(t *testing.T) {
|
|
fresh(t)
|
|
s := New()
|
|
c := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}, model: "gpt-6-sol", rest: "codex@me@example.com"}
|
|
over := func() {
|
|
restingUntil.Lock()
|
|
for k := range restingUntil.m {
|
|
restingUntil.m[k] = time.Now().Add(-time.Second)
|
|
}
|
|
restingUntil.Unlock()
|
|
}
|
|
for _, x := range []struct {
|
|
name string
|
|
status int
|
|
body string
|
|
}{
|
|
{"chatgpt 529", 529, chatgptOverloaded},
|
|
{"chatgpt stream", 529, `{"error":{"code":"server_is_overloaded","message":"Our servers are currently overloaded. Please try again later."}}`},
|
|
{"anthropic 529", 529, `{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}`},
|
|
{"gemini 503", 503, `{"error":{"code":503,"message":"The model is overloaded. Please try again later.","status":"UNAVAILABLE"}}`},
|
|
} {
|
|
t.Run(x.name, func(t *testing.T) {
|
|
for i := range 6 {
|
|
over()
|
|
r := s.restAfter(c, x.status, http.Header{}, []byte(x.body))
|
|
if r.Why != failOther || r.Failures != 0 || r.Key != c.restID() || r.By != "cooldown" || time.Until(r.Until) > overloadRest {
|
|
t.Fatalf("overload %d: rests %s %v by %s, %d failures", i+1, r.Key, time.Until(r.Until).Round(time.Second), r.By, r.Failures)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
h := http.Header{}
|
|
h.Set("Retry-After", "600")
|
|
over()
|
|
if r := s.restAfter(c, 529, h, []byte(chatgptOverloaded)); r.By != "retry-after" || time.Until(r.Until) > overloadRestMost {
|
|
t.Fatalf("Retry-After 600: rests %v by %s", time.Until(r.Until).Round(time.Second), r.By)
|
|
}
|
|
|
|
// a plain 5xx is the account's failure, as before: the first step,
|
|
// not one past the overloads
|
|
over()
|
|
if r := s.restAfter(c, 500, http.Header{}, []byte(`{"error":{"message":"An error occurred while processing your request.","type":"server_error"}}`)); r.Failures != 1 || r.By != "backoff" {
|
|
t.Fatalf("a 500 after the overloads: %d failures by %s", r.Failures, r.By)
|
|
}
|
|
over()
|
|
if r := s.restAfter(c, 401, http.Header{}, []byte(`{"error":{"message":"Your authentication token has expired.","code":"token_expired"}}`)); r.Key != c.restKey() || r.Failures != 2 {
|
|
t.Fatalf("a 401 rests %s, %d failures", r.Key, r.Failures)
|
|
}
|
|
over()
|
|
if r := s.restAfter(c, 429, http.Header{}, []byte(`{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached"}}`)); r.Why != failQuota {
|
|
t.Fatalf("a usage limit rests as %s", r.Why)
|
|
}
|
|
}
|