Files
magpie/internal/gateway/rest_cap_test.go
T
yetone 22512cde53 provider, gateway: rest-cap off never rests a provider's keys after an unknown failure (Kismet Alan on Discord)
A relay or account pool fails now and then with errors that say nothing
of why (a 5xx, a dropped connection), and the next request often goes
through. Every such failure rested the key or account a minute, doubling
up to 10, and `rest-cap` could only move that cap (1 minute at least;
`off` read as `default`).

`magpie provider rest-cap <id> off` (provider.RestCapOff, restCap: -1 in
providers.json) now has a failure that says nothing of why (failOther)
rest nobody and not count toward the backoff: restAfterMarked returns a
Rest by "off" with no time left. What says why still rests as before: a
401 or 403, a model not served (restID), an account's window full, a
rate limit (magpie's own 30-minute cap; RestCapOr treats off as unset),
quota, credit, a restriction. `rest-cap <id> 30s` is turned away and names
1m and off (the first rest is a minute already). The Routing page tells a
try rested by "off" as not resting (one new string, zh/zh-TW/ja/de).
No "fail N times before switching" counter: a failed try already moves
the same request on to the next key or member, so a count would only
keep requests on a failing upstream longer.

Run:
- TestRestCapOff (gateway), TestRestCapCmd (cli), TestOwnRestCap: pass;
  each goes red with the change taken out by hand (the early return
  disabled: "#1 500: rests 59.99s by backoff"; restMinutes in place of
  restCapMinutes: "off: <nil> 0").
- routing-rest-off.test.cjs: 20/20 (chromium, webkit; 1100, 420; en zh
  zh-TW ja de); red 10/10 with the "off" case renamed.
  routing-wb-restricted 20/20. rest-try-now and routing-model-rest fail
  a varying few under load average 200-420 here, and fail more on
  origin/main 331c27703 run at the same time; neither reads "off".
- gofmt -l clean; go vet darwin (provider, gateway, cli, gui), GOOS=linux
  -tags nogui and GOOS=windows (provider, gateway, cli).
- go test ./... with pipefail, sandbox HOME: every package ok except
  agent, gateway and library, which hit the 10m timeout under load
  average ~200 (no test hung: each stack shows a different test seconds
  in). Rerun with a longer -timeout: library ok (603s), gateway ok
  (1572s) and agent ok (1457s).
Not tried: a real relay; the change is in the rest bookkeeping only.
2026-10-14 20:49:20 +08:00

112 lines
4.4 KiB
Go

package gateway
import (
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #2008 (Gaudi-XIX): a provider's own rest cap (`magpie provider rest-cap`)
// is where a key that keeps failing, or keeps being rate limited as soon
// as its rest is over, stops doubling: past the gateway's 10 and 30
// minutes for a free key that is down for hours, or short of them.
func TestOwnRestCap(t *testing.T) {
s := &Server{}
near := func(d, want time.Duration) bool { return d > want-5*time.Second && d <= want }
ladder := func(name string, capMin, status int, body string, want ...time.Duration) {
t.Helper()
c := candidate{p: provider.Provider{ID: "capped-" + name, Key: "k", RestCap: capMin}, model: "m", rest: "capped-" + name + "#k"}
id := c.restKey()
for i, w := range want {
r := s.restAfter(c, status, http.Header{}, []byte(body))
if !near(time.Until(r.Until), w) {
t.Fatalf("%s #%d: rests %v by %s (%s), want %v", name, i+1, time.Until(r.Until), r.By, r.Why, w)
}
restingUntil.Lock()
restingUntil.m[id] = time.Now().Add(-time.Second)
restingUntil.Unlock()
}
}
rate := `{"error":{"message":"Too many requests","type":"rate_limit_error"}}`
fail := `{"error":{"message":"upstream connect error","type":"api_error"}}`
m := time.Minute
ladder("rate-long", 240, 429, rate, m, 2*m, 4*m, 8*m, 16*m, 32*m, 64*m, 128*m, 240*m, 240*m)
ladder("rate-short", 3, 429, rate, m, 2*m, 3*m, 3*m)
ladder("rate-own", 0, 429, rate, m, 2*m, 4*m, 8*m, 16*m, 30*m, 30*m)
ladder("fail-long", 120, 502, fail, m, 2*m, 4*m, 8*m, 16*m, 32*m, 64*m, 120*m)
ladder("fail-own", 0, 502, fail, m, 2*m, 4*m, 8*m, 10*m, 10*m)
}
func TestRestCapOr(t *testing.T) {
if d := (provider.Provider{RestCap: 90}).RestCapOr(time.Minute); d != 90*time.Minute {
t.Error(d)
}
if d := (provider.Provider{}).RestCapOr(time.Minute); d != time.Minute {
t.Error(d)
}
if d := (provider.Provider{RestCap: 99999999}).RestCapOr(time.Minute); d != provider.MaxQuotaRest*time.Minute {
t.Error(d)
}
}
// Kismet Alan on Discord: a relay or account pool whose unknown errors
// come now and then, the next request often going through, is set never
// to rest after one (`magpie provider rest-cap <id> off`). Its 5xx and its
// dropped connection rest it no more, nor count toward a backoff; what
// says why still rests it as before: a rate limit (magpie's own 30
// minutes cap), out of quota, its key refused, its model not served.
func TestRestCapOff(t *testing.T) {
s := &Server{}
c := candidate{p: provider.Provider{ID: "nofail", Key: "k", RestCap: provider.RestCapOff}, model: "m", rest: "nofail#k"}
id := c.restKey()
t.Cleanup(func() {
clearRest(id)
clearRest(c.restID())
routed.Lock()
delete(routed.failures, id)
delete(routed.failures, c.restID())
routed.Unlock()
})
fail := `{"error":{"message":"upstream connect error","type":"api_error"}}`
for i, status := range []int{500, 502, 503, 504, 520} {
r := s.restAfter(c, status, http.Header{}, []byte(fail))
if r.Why != failOther || r.By != "off" || r.Until.After(time.Now()) {
t.Fatalf("#%d %d: rests %v by %s (%s), want no rest", i+1, status, time.Until(r.Until), r.By, r.Why)
}
if resting(c) {
t.Fatalf("#%d %d: set aside", i+1, status)
}
}
routed.Lock()
n := routed.failures[id]
routed.Unlock()
if n != 0 {
t.Fatalf("%d failures counted", n)
}
// what says why rests it still
for _, k := range []struct {
status int
body string
why string
}{
{401, `{"error":{"message":"invalid api key","type":"api_error"}}`, failOther},
{403, `{"error":{"message":"forbidden","type":"api_error"}}`, failOther},
{429, `{"error":{"message":"Too many requests","type":"rate_limit_error"}}`, failRate},
{429, `{"error":{"message":"You exceeded your current quota, please check your plan","type":"insufficient_quota"}}`, failCredit},
{404, `{"error":{"message":"The model m does not exist or you do not have access to it","type":"invalid_request_error"}}`, failOther},
} {
clearRest(id)
clearRest(c.restID())
r := s.restAfter(c, k.status, http.Header{}, []byte(k.body))
if r.Why != k.why || r.By == "off" || !r.Until.After(time.Now()) || !resting(c) {
t.Fatalf("%d %s: rests %v by %s (%s), want a rest as %s", k.status, k.body, time.Until(r.Until), r.By, r.Why, k.why)
}
}
// a rate limit's cap is magpie's own 30 minutes, not none
if d := c.p.RestCapOr(longestRateRest); d != longestRateRest {
t.Fatalf("rate cap %v", d)
}
}