Files
magpie/internal/gateway/queued_test.go
T
yetoneandggbdpq 859b292d91 gateway: the Qoder plugin's queue answer ends a group's patience rounds (#5346)
#5346, hardy20260000: a Qoder route of 217 accounts (routing=order)
asked one request up to 656 times, 20 GB of re-uploaded bodies a day.
When magpie has another candidate, the Qoder plugin answers Qoder's
queue code 10605 at once with a 503 + Retry-After (#4049). busy() took
that for a moment's trouble, so once every member had given way the
group's patience asked all of them again, one whole round after
another, re-sending the body on every account each round. Now busy()
leaves the plugin's queue answer alone: each member is asked once, and
the last one, asked untold, has the plugin wait the queue out as Qoder's
own client does (polling its queue status, not re-sending the body),
then gets its same-one retries.

Changed from ggbdpq's patch: it matched `\bqueue|排队` in any
provider's 5xx. A relay's "Request queue is full" or "当前排队人数较多"
is the overload a pause mends, and would have lost its patience. The
match is the plugin's own words now ("queued the request", "this
plan's queue:"), and the tests use the plugin's real errorResponse
body for both its forms (isQueued, and the service out as in
testdata/qoder_queued_10605.json) instead of an invented message.

Not changed: the walk itself. With patience=off (the reporter's
setting now) a request still asks each account once until one answers,
~20x on this route; that is the routing group finding a free account,
and a per-request attempt budget is a separate decision.

Run (mote, -tags nogui, sandboxed by the package's TestMain):
- TestQueuedGroupAsksEachMemberOnce/{queued,busy}: 5 asks, 503 told.
- TestOverloadedGroupStillReplans/{overloaded,queue full,排队}: ok.
- Broken by hand: without `!queued(body)` both queued cases FAIL
  "a queued group asked 39 times, want 5 at most"; with the patch's
  broad regex, "queue full" and 排队 FAIL "asked 5 times, want more
  than 5". Restored, green.
- -run Queued|Overloaded|Plugin|Qoder|Patience|Replan|Single|Busy|
  Fallback|Nested|Affinity|Codex busy|Shared|Retry: ok.
- gofmt -l clean; go vet -tags nogui, GOOS=linux -tags nogui and
  GOOS=windows vet of internal/gateway: clean.
Not tried: a real Qoder queue on a many-account route; the full suite.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 09:59:13 +08:00

88 lines
3.5 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// askQueuedGroup sends one request to a provider of keys accounts, in order,
// whose upstream answers every try with code, Retry-After and body, and
// says how many times the upstream was asked and what the agent was told.
func askQueuedGroup(t *testing.T, keys, code int, body string) (asked, told int) {
t.Helper()
u := &busyUpstream{replies: []busyReply{{code: code, retryAfter: "0.04", body: body}}}
fresh(t)
up := httptest.NewServer(u)
t.Cleanup(up.Close)
p := provider.Provider{ID: "a", Name: "A", Key: "k0", Models: []string{"m"}, Chat: up.URL + "/v1", Routing: "order", Patience: provider.PatienceDefault}
for i := 1; i < keys; i++ {
p.Keys = append(p.Keys, provider.KeyAccount{Key: string(rune('0' + i))})
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
s := New()
scaled(t, 3*time.Second)
gw := httptest.NewServer(s.Handler())
t.Cleanup(gw.Close)
res, err := http.Post(gw.URL+"/v1/chat/completions", "application/json", strings.NewReader(singleAsk(false)))
if err != nil {
t.Fatal(err)
}
io.Copy(io.Discard, res.Body)
res.Body.Close()
return u.asked(), res.StatusCode
}
// The Qoder plugin (0.2.40, #4049) answers a request Qoder queued at once
// with a 503 + Retry-After that names the queue, when magpie has another
// candidate to ask. Asked again as a group's patience asks, every member
// only meets the queue again, the body sent again on every account, one
// whole round after another (#5346: 217 accounts, tries up to 656 for one
// request, 20 GB of re-uploaded bodies a day). Each member is asked once,
// and the last one left only gets its same-one retries.
func TestQueuedGroupAsksEachMemberOnce(t *testing.T) {
// the plugin's errorResponse, with the words it says for code 10605
// (packages/qoder/index.mjs), isQueued true and the service out
for name, body := range map[string]string{
"queued": `{"error":{"message":"Qoder queued the request for efficient (this plan's queue: p3, 3 ahead); try again in 30s","type":"qoder_error","code":503}}`,
"busy": `{"error":{"message":"Qoder is busy for efficient (this plan's queue: p3); try again in 30s","type":"qoder_error","code":503}}`,
} {
t.Run(name, func(t *testing.T) {
const keys = 3
n, told := askQueuedGroup(t, keys, 503, body)
if told != 503 {
t.Fatalf("the agent was told %d, want the 503", told)
}
if n > keys+lastRetries {
t.Fatalf("a queued group asked %d times, want %d at most (each key once, the last one's same-one retries)", n, keys+lastRetries)
}
})
}
}
// A 5xx that is not the plugin's queue answer is still one a group's
// patience waits out, the members asked again in turn (#1418), a relay's
// own words for its queue included: the plugin's words, not the 503 nor
// the word "queue", are what end the rounds.
func TestOverloadedGroupStillReplans(t *testing.T) {
for name, body := range map[string]string{
"overloaded": `{"error":{"message":"upstream overloaded, please try again","type":"api_error"}}`,
"queue full": `{"error":{"message":"Request queue is full, please retry later","type":"server_error"}}`,
"排队": `{"error":{"message":"当前排队人数较多,请稍后重试","type":"new_api_error"}}`,
} {
t.Run(name, func(t *testing.T) {
const keys = 3
if n, _ := askQueuedGroup(t, keys, 503, body); n <= keys+lastRetries {
t.Fatalf("an overloaded group asked %d times, want more than %d: its patience asks the members again", n, keys+lastRetries)
}
})
}
}