gateway: another magpie's rate-limited model sinks alone, not the whole remote (#2167)
restAfterMarked sank a rate-limited candidate by its rest key, so one
of a remote magpie's models answering 429 with quota left sent the
remote's other models, never limited, to the back of every Sink order
too, while the rest itself was the model's alone (remoteModelRest).
The sink now keeps the rest's scope: such a model sinks by restID, and
sunkAt reads a candidate's restID before its restKey.
Patch by ggbdpq (#2167): the routing.go change and
TestRemoteMagpieRateLimitSinksTheModel are theirs. Changed from it:
- their sunkAt read restID only for the Remote Magpie preset, while
remoteModelRest also holds for a magpie added by hand as a plain
endpoint (its reply names the provider, answeredThereHeader,
4e5b2ad81). Such a model sank under restID and was then never found,
so it sank nowhere. sunkAt now reads restID for every candidate.
- UnrestAll lifts a key's rest and each of its models' but unsank only
the key, so a model sunk alone stayed at the back after Try now.
It calls unsinkAll (the key and key/…).
Doc: the Sink row in gateway-routing.md.
Run (rebased on origin/main):
- TestRemoteMagpieRateLimitSinksTheModel: red on origin/main's
routing.go and sink.go ("the remote was asked [], other [ko]"),
green now.
- TestMagpieAddedByHandRateLimitSinksTheModel (new): red on main (kimi
sank with glm), red with the patch's preset-only sunkAt put back by
hand ("glm didn't sink ... the remote was asked [glm/glm-5]"), green
now.
- TestUnrestAllUnsinksTheModels (new): red with UnrestAll calling
unsink(key) ("office/glm/glm-5 still sunk"), green now.
- Sink|Sunk|Unrest|TryNow|RemoteMagpie|AnotherMagpie ok; the whole
internal/gateway package (-tags nogui -count=1 -timeout 45m) ok in 502s
at 56ea00d9c.
- gofmt -l clean; go vet ./internal/gateway for darwin, GOOS=windows,
GOOS=linux -tags nogui clean.
Not tried: two real magpies with Sink on and a real vendor 429.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -6,7 +6,9 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
@@ -79,3 +81,146 @@ func TestRemoteMagpieQuotaRestsTheModel(t *testing.T) {
|
||||
t.Fatalf("the group: %d %s; the remote was asked %v, other %v", code, body, asked, other.tried)
|
||||
}
|
||||
}
|
||||
|
||||
// One of a remote magpie's models rate limited with quota left sinks only
|
||||
// its own rest (restID, as its rest is keyed): sinking the remote's whole
|
||||
// key sends its other models, which were never limited, behind the next
|
||||
// provider too, the same mistake resting the whole remote was.
|
||||
func TestRemoteMagpieRateLimitSinksTheModel(t *testing.T) {
|
||||
fresh(t)
|
||||
forgetSinks(t)
|
||||
var mu sync.Mutex
|
||||
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)
|
||||
mu.Lock()
|
||||
asked = append(asked, req.Model)
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
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{"glm/glm-5", "kimi/kimi-k3"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := &keyed{}
|
||||
serveOn(t, "other", "ko", []string{"m"}, other)
|
||||
// glm's rate limit, with quota left, sinks it (the group's Sink puts
|
||||
// what sank behind the others)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "R", Members: []string{"office/glm/glm-5", "office/kimi/kimi-k3"}, Routing: provider.Ordered, Sink: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
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)
|
||||
}
|
||||
// the watched group: the remote's kimi, never limited, keeps its place
|
||||
// ahead of a provider that never served this conversation
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"office/kimi/kimi-k3", "other/m"}, Routing: provider.Ordered, Sink: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
asked = nil
|
||||
mu.Unlock()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// The same for a magpie added by hand, as a plain OpenAI-compatible
|
||||
// endpoint: its reply names the provider there (providerHeader), so its
|
||||
// model's 429 rests that model alone (remoteModelRest) and sinks it
|
||||
// alone, and it is that model, not the endpoint's others, that the next
|
||||
// Sink order sends to the back. Read at the model's own sink key, not only
|
||||
// for the remote magpie preset, or it sinks nothing at all.
|
||||
func TestMagpieAddedByHandRateLimitSinksTheModel(t *testing.T) {
|
||||
fresh(t)
|
||||
forgetSinks(t)
|
||||
var mu sync.Mutex
|
||||
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)
|
||||
mu.Lock()
|
||||
asked = append(asked, req.Model)
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if req.Model == "glm/glm-5" {
|
||||
w.Header().Set(providerHeader, "glm")
|
||||
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: "rm", Name: "Remote Magpie", Key: "k",
|
||||
Chat: remote.URL + "/v1", Models: []string{"glm/glm-5", "kimi/kimi-k3"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := &keyed{}
|
||||
serveOn(t, "other", "ko", []string{"m"}, other)
|
||||
s := New()
|
||||
group := func(name string, members ...string) string {
|
||||
t.Helper()
|
||||
if err := provider.SaveGroup(provider.Group{Name: name, Members: members, Routing: provider.Ordered, Sink: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
asked = nil
|
||||
mu.Unlock()
|
||||
other.tried = nil
|
||||
code, body := postAs(t, s, "", `{"model":"group/`+strings.ToLower(name)+`","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("group %s: %d %s", name, code, body)
|
||||
}
|
||||
return body
|
||||
}
|
||||
if body := group("R", "rm/glm/glm-5", "rm/kimi/kimi-k3"); !strings.Contains(body, "from kimi/kimi-k3") {
|
||||
t.Fatalf("glm there: %s", body)
|
||||
}
|
||||
// kimi there, never limited, keeps its place
|
||||
if body := group("K", "rm/kimi/kimi-k3", "other/m"); !strings.Contains(body, "from kimi/kimi-k3") || len(other.tried) != 0 {
|
||||
t.Errorf("kimi sank with glm: %s; other asked %v", body, other.tried)
|
||||
}
|
||||
// glm, limited there, went to the back: its rest over, it is still
|
||||
// behind other
|
||||
restingUntil.Lock()
|
||||
delete(restingUntil.m, "rm/glm/glm-5")
|
||||
delete(restingUntil.note, "rm/glm/glm-5")
|
||||
restingUntil.Unlock()
|
||||
if body := group("L", "rm/glm/glm-5", "other/m"); !strings.Contains(body, "from ko") || len(asked) != 0 {
|
||||
t.Errorf("glm didn't sink: %s; the remote was asked %v", body, asked)
|
||||
}
|
||||
}
|
||||
|
||||
// UnrestAll lifts a key's own rest and each of its models', and with them
|
||||
// each one's sink: a model that sank alone comes back to the front too.
|
||||
func TestUnrestAllUnsinksTheModels(t *testing.T) {
|
||||
forgetSinks(t)
|
||||
now := time.Now()
|
||||
sink("office/glm/glm-5", now)
|
||||
sink("office", now)
|
||||
sink("officer/glm/glm-5", now)
|
||||
New().UnrestAll("office")
|
||||
for _, k := range []string{"office/glm/glm-5", "office"} {
|
||||
if _, ok := sunk.m[k]; ok {
|
||||
t.Errorf("%s still sunk after UnrestAll", k)
|
||||
}
|
||||
}
|
||||
if _, ok := sunk.m["officer/glm/glm-5"]; !ok {
|
||||
t.Errorf("another key's sink was lifted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -687,7 +687,7 @@ func (s *Server) UnrestAll(key string) int {
|
||||
}
|
||||
liftedLocked(key, now)
|
||||
restingUntil.Unlock()
|
||||
unsink(key)
|
||||
unsinkAll(key)
|
||||
s.trace.lifted()
|
||||
return n
|
||||
}
|
||||
@@ -1114,7 +1114,15 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
d, r.By, r.Failures = s.rateRest(c, header, body, sharedPool, now)
|
||||
// rate limited with quota left: to the back, where the order sinks
|
||||
if c.full(now).IsZero() && !(c.isOpenRouterFree() && sharedPool) {
|
||||
sink(c.restKey(), now)
|
||||
// a remote magpie's rest is the model's alone (caa53673, the
|
||||
// key's own limit apart): the sink keeps the same scope, or
|
||||
// one model's 429 would send the remote's other models, never
|
||||
// limited, behind the others too
|
||||
if remoteModelRest(c, failRate, header) {
|
||||
sink(c.restID(), now)
|
||||
} else {
|
||||
sink(c.restKey(), now)
|
||||
}
|
||||
}
|
||||
case failRestricted:
|
||||
// out until the vendor lifts it: an appeal, not a wait (#1849)
|
||||
|
||||
@@ -21,6 +21,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -55,10 +56,27 @@ func unsink(key string) {
|
||||
sunk.Unlock()
|
||||
}
|
||||
|
||||
// sunkAt is when a candidate sank, if it did.
|
||||
// unsinkAll is unsink for key and each of its models (restID, key/model):
|
||||
// UnrestAll's lift of them all.
|
||||
func unsinkAll(key string) {
|
||||
sunk.Lock()
|
||||
for k := range sunk.m {
|
||||
if k == key || strings.HasPrefix(k, key+"/") {
|
||||
delete(sunk.m, k)
|
||||
}
|
||||
}
|
||||
sunk.Unlock()
|
||||
}
|
||||
|
||||
// sunkAt is when a candidate sank, if it did: its model alone, as
|
||||
// another magpie's model rate limited there sinks (remoteModelRest,
|
||||
// #2167), or its whole key or account.
|
||||
func sunkAt(c candidate) (sinking, bool) {
|
||||
sunk.Lock()
|
||||
defer sunk.Unlock()
|
||||
if s, ok := sunk.m[c.restID()]; ok {
|
||||
return s, true
|
||||
}
|
||||
s, ok := sunk.m[c.restKey()]
|
||||
return s, ok
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user