gateway: a remote magpie's quota, credit or rate refusal rests only that model, not the whole remote (bigsteven on Discord)
A Remote magpie provider (LAN share) serves every model through one key. When it answered 429 "usage limit … resets in 3 hours" for one model, the key rested whole for those 3 hours. A group then never moved on to the remote's other models that still had quota, and the user had to ask again by hand. The remote magpie rests its own providers' models itself, so one model out of quota there says nothing of its others. remoteModelRest: on a Remote magpie provider, failQuota, failCredit and failRate rest c.restID() (the model) in restAfterMarked and rateRest. A refused key or an unreachable computer still rests the whole remote. Run: - TestRemoteMagpieQuotaRestsTheModel, against a fake remote whose codex model answers 429 quota, glm 429 rate, and kimi OK. A direct request, then two groups. It fails with remoteModelRest taken out. - go test -tags nogui -count=1 -timeout 30m ./internal/gateway: ok. - gofmt -l is clean. go vet passes on darwin, GOOS=linux -tags nogui and GOOS=windows. Not tried: two real computers on a LAN share.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,81 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// bigsteven on Discord: another computer's magpie (LAN share) serves each
|
||||
// of its models from its own providers, and rests them itself. When one of
|
||||
// them was out of quota there, its 429 rested the whole remote magpie here
|
||||
// for the 3 hours it named, so a group whose member was another of its
|
||||
// models, with quota left, went to its next member, or had none. Such a
|
||||
// refusal rests only the model it was about.
|
||||
func TestRemoteMagpieQuotaRestsTheModel(t *testing.T) {
|
||||
fresh(t)
|
||||
var asked []string
|
||||
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
json.NewDecoder(r.Body).Decode(&req)
|
||||
asked = append(asked, req.Model)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if req.Model == "codex/gpt-5.5" {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
io.WriteString(w, `{"error":{"message":"You've hit your usage limit. Your quota resets in 3 hours.","type":"usage_limit_reached"}}`)
|
||||
return
|
||||
}
|
||||
if req.Model == "glm/glm-5" {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
io.WriteString(w, `{"error":{"message":"rate limit exceeded, slow down"}}`)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, `{"id":"x","choices":[{"index":0,"message":{"role":"assistant","content":"from `+req.Model+`"},"finish_reason":"stop"}]}`)
|
||||
}))
|
||||
t.Cleanup(remote.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Key: "k",
|
||||
Chat: remote.URL + "/v1", Models: []string{"codex/gpt-5.5", "glm/glm-5", "kimi/kimi-k3"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := &keyed{}
|
||||
serveOn(t, "other", "ko", []string{"m"}, other)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"office/kimi/kimi-k3", "other/m"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
if code, body := postAs(t, s, "", `{"model":"office/codex/gpt-5.5","messages":[{"role":"user","content":"hi"}]}`); code != 429 {
|
||||
t.Fatalf("codex on the remote: %d %s", code, body)
|
||||
}
|
||||
if r, ok := restOf("office"); ok {
|
||||
t.Errorf("the whole remote magpie rests for one model's quota: %+v", r)
|
||||
}
|
||||
if _, ok := restOf("office/codex/gpt-5.5"); !ok {
|
||||
t.Errorf("codex/gpt-5.5 doesn't rest: %v", rests())
|
||||
}
|
||||
// and a rate limit on one of its models, the same: the group's next
|
||||
// member on that computer answers
|
||||
if err := provider.SaveGroup(provider.Group{Name: "R", Members: []string{"office/glm/glm-5", "office/kimi/kimi-k3"}, Routing: provider.Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, body := postAs(t, s, "", `{"model":"group/r","messages":[{"role":"user","content":"hi"}]}`); code != 200 || !strings.Contains(body, "from kimi/kimi-k3") {
|
||||
t.Fatalf("glm on the remote: %d %s", code, body)
|
||||
}
|
||||
if r, ok := restOf("office"); ok {
|
||||
t.Errorf("the whole remote magpie rests for one model's rate limit: %+v", r)
|
||||
}
|
||||
if r, ok := restOf("office/glm/glm-5"); !ok || r.Why != failRate {
|
||||
t.Errorf("glm/glm-5 doesn't rest as rate limited: %+v %v", r, rests())
|
||||
}
|
||||
asked = nil
|
||||
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 || !strings.Contains(body, "from kimi/kimi-k3") || len(other.tried) != 0 {
|
||||
t.Fatalf("the group: %d %s; the remote was asked %v, other %v", code, body, asked, other.tried)
|
||||
}
|
||||
}
|
||||
@@ -755,6 +755,9 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
if (why == failCredit || why == failQuota) && c.p.PaidBesideFree(c.model) {
|
||||
id = c.restID()
|
||||
}
|
||||
if remoteModelRest(c, why) {
|
||||
id = c.restID()
|
||||
}
|
||||
r.Key = id
|
||||
if id == c.restID() {
|
||||
r.Model = c.model
|
||||
@@ -770,6 +773,21 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
return r
|
||||
}
|
||||
|
||||
// remoteModelRest says a refusal for want of quota, credit or rate rests
|
||||
// only the model it was about, when it came from another computer's magpie
|
||||
// (a Remote magpie provider, LAN share): that magpie serves each of its
|
||||
// models from its own providers and rests them itself, so one of them out
|
||||
// of quota there says nothing of the others (bigsteven on Discord: the
|
||||
// remote's 429 for one model rested every model on it for the 3 hours it
|
||||
// named). Its key refused, or the computer unreachable, still rests it
|
||||
// whole.
|
||||
func remoteModelRest(c candidate, why string) bool {
|
||||
if !c.p.IsRemoteMagpie() {
|
||||
return false
|
||||
}
|
||||
return why == failQuota || why == failCredit || why == failRate
|
||||
}
|
||||
|
||||
// rateRest is how long a rate-limited candidate sits out: as long as the
|
||||
// vendor says (Retry-After, a reset header or a reset time in its error),
|
||||
// and a minute when it says nothing. One limited again as soon as its rest
|
||||
@@ -781,7 +799,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
// count where it is, so one request's own retries don't stretch it.
|
||||
func (s *Server) rateRest(c candidate, header http.Header, body []byte, sharedPool bool, now time.Time) (time.Duration, string, int) {
|
||||
id := c.restKey()
|
||||
if c.isOpenRouterFree() && sharedPool {
|
||||
if c.isOpenRouterFree() && sharedPool || remoteModelRest(c, failRate) {
|
||||
id = c.restID()
|
||||
}
|
||||
n := 1
|
||||
|
||||
Reference in New Issue
Block a user