young02036 on Discord: a provider can be given a concurrency, so why
can't a gateway key? A key now has an optional MaxConcurrency, from 1
to 1000. Left unset (every key today), nothing changes.
- access: Key and Change get MaxConcurrency, and Identity carries it, the
issuer's cap included. The concurrency-key action refuses a value
below 0 or above 1000.
- gateway (key_slots.go): every route that asks a model is held to the
key's cap, however the request came (loopback, LAN, a Remote magpie, a
Responses socket's turn). One request too many waits in the key's
queue in arrival order. It shows as "slot" in Active, and its wait goes
into the try's Queued. A request whose agent goes away while it waits
leaves the queue and sends nothing. A stream holds its slot until it
ends. serve's routes (chat, responses, messages, ollama, gemini, the
Codex backend) wait inside the try, after keepQueued, so a streaming
agent gets keepalives while it waits. The other model routes (images,
embeddings, rerank, audio, videos, search, decisions, native) wait
before their handler runs. forward and codexUpstreamOn take the slot
too, so nothing reaches a vendor without one. A key issued under
another waits for its own slot, then for its issuer's.
/v1/magpie/concurrency lists each capped key under "keys", and counts
its waiters in "waiting". A key's limit (key_limit) reports the cap.
Not held, as a provider's MaxConcurrency doesn't hold them: GET,
HEAD, OPTIONS, count_tokens, :countTokens and MCP.
TestKeySlotEveryRoute fails when a route is judged neither held nor
free.
- cli: magpie gateway-key concurrency <id> [<n>|off]. It refuses 0, a
negative, over 1000, a fraction, a word, --help or an extra argument
with its usage.
- gui: the key row gets a badge in the provider concurrency badge's
style. It opens the app's menu with No limit, 1/2/3/5/10 and Other…,
and Other… takes a number typed in place. A value that isn't a whole
number up to 1000 is refused and nothing is sent. Browser and
read-only keys have no badge. Strings are in en, zh, zh-TW, ja and
de. An unset badge, which a hover shows, gives way before the masked
key (flex-shrink 8). Without that, at 420px in en and de, the extra
badge squeezed the key until its copy button slid under the pointer
that came to drag the row, and gateway-key-arrange went red.
- docs/subsystems/gateway-routing.md describes it.
Run (on mote, at this tree on origin/main 0f8c3d867; rebased onto 627fd1a3b,
which touches none of these files, and the budgets were run again):
- go test -timeout 45m ./internal/access ./internal/gateway ./internal/cli
./internal/gui: access, gateway (839s) and cli ok. gui had one red,
TestGatewayModeStatsNameTheGatewaySessions, run at 00:40 PST: its
record an hour back falls on yesterday in a day's first hour. It
passes from 01:09, so it is unrelated; filed as #5690. The Budget/Size
tests were run again after the CSS change: ok.
- gofmt -l: clean. go vet for darwin, plus GOOS=linux and GOOS=windows
(-tags nogui), on those packages: ok.
- GUI (node --test, Chromium and WebKit, 1100 and 420 wide, en, zh,
zh-TW, ja and de): every file that loads the gateway keys (59) plus
i18n-keys and i18n-en-no-cjk. The reds left are the same on
origin/main and already filed: gateway-key-models en (#5233, #4081),
i18n-keys "a plugin's words are its own" (#5332), phone-connect-menu
at 420 (#4706, #4707). After the flex-shrink fix:
gateway-connect-keys, gateway-key-arrange, -concurrency, -mcp,
-own-rotate and -quotas passed 104/104 in both engines, against
84/84 on base.
- The tests:
- TestKeyConcurrency, over LAN with cap 2: 2 of 3 requests reach the
vendor and the third is shown as "slot". Another key isn't held. A
cancelled waiter leaves the queue and never reaches the vendor. The
third goes once one ends, every lane is given back, and its Queued
is at least 40ms.
- TestKeyConcurrencyKeepsStreamAlive: a waiting stream gets 200 and
keepalives.
- TestKeyConcurrencyEager: on embeddings an issued key waits on its
parent's lane, a cancelled waiter leaves, never more than 1 runs at
once, and a GET takes no slot.
- A real `serve` in a sandbox HOME (built from this tree, MAGPIE_ADDR
127.0.0.1:38612) against a fake slow SSE upstream. Steps: `provider
add fake url=…`, `gateway-key add`, `gateway-key concurrency <id> 1`,
then three concurrent keyed streams. /v1/magpie/concurrency showed the
key at busy 1, waiting 2, limit 1 (two in "slot"). The upstream's
most at once was 1, and it saw 3 requests one after another (200s at
1.9s, 3.7s and 5.5s, each with [DONE]). Afterwards "keys" was empty.
`concurrency <id> 2000` exited 1 with its usage.
Broken by hand (each went red, then restored):
- keySlot returns nil: TestKeyConcurrency ("timed out waiting for c
queued") and KeepsStreamAlive.
- The issuer's lane skipped: TestKeyConcurrencyEager.
- The eager take skipped: TestKeyConcurrencyEager.
- The CLI's bounds check removed: "accepted [concurrency id 0]".
- The GUI's bounds check removed: gateway-key-concurrency.
- Without the flex-shrink line: gateway-key-arrange chromium/webkit en
and de 420px.
Not tried / not done:
- Full suite not run at push (neither `go test ./...` nor the whole GUI
suite).
- Not tried with a real vendor account or a second computer on the LAN.
The LAN path is covered by TestKeyConcurrency's lanServer.
- The key's badge shows the cap, not live running or waiting counts.
/v1/magpie/concurrency has those.
Reported-by: young02036 (Discord)
314 lines
12 KiB
Go
314 lines
12 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// keySlotFree are the routes, other than a GET's or an OPTIONS' (never
|
|
// held), that a key's MaxConcurrency doesn't hold: magpie's own, MCP (a
|
|
// tool call of a turn already held) and count_tokens. A route in neither
|
|
// this nor keySlotRoutes fails TestKeySlotEveryRoute, so a new one is
|
|
// judged.
|
|
var keySlotFree = []string{
|
|
"POST /api/show", "POST /v1/magpie/usage/requests", "POST " + provider.RemoteRefreshPath,
|
|
"POST " + provider.RemoteCodexResetPath, "POST " + StatuslinePath, "POST " + KeysPath,
|
|
"DELETE " + KeysPath + "/{id}", "POST " + OIDCKeysPath, "POST " + provider.RemoteProvePath,
|
|
"POST " + RestartPath, "POST " + provider.RemoteUpdatePath, "POST /v1/messages/count_tokens",
|
|
"POST " + claudeMCPPath + "{token}", "/mcp/{name}", "/mcp/run/{name}", SearchMCPPath, VisionMCPPath,
|
|
ImageMCPPath, BrowserMCPPath, ImageMCPFiles + "{name}", ToolsMCPPath, ToolsMCPOne + "{plugin}",
|
|
HubMCPPath + "{agent}", HubMCPAll, "/",
|
|
}
|
|
|
|
// Every route is judged for a key's MaxConcurrency: held (keySlotRoutes)
|
|
// or not (keySlotFree, or a GET's), never both, and keySlotRoutes names
|
|
// only routes the router has.
|
|
func TestKeySlotEveryRoute(t *testing.T) {
|
|
fresh(t)
|
|
mux := New().routes()
|
|
for _, p := range mux.patterns {
|
|
_, held := keySlotRoutes[p]
|
|
free := slices.Contains(keySlotFree, p) || strings.HasPrefix(p, "GET ") || strings.HasPrefix(p, "OPTIONS ")
|
|
if held == free {
|
|
t.Errorf("route %q is in neither keySlotRoutes nor keySlotFree, or in both: say whether a gateway key's MaxConcurrency holds it", p)
|
|
}
|
|
}
|
|
for p := range keySlotRoutes {
|
|
if !slices.Contains(mux.patterns, p) {
|
|
t.Errorf("keySlotRoutes names %q, which the router doesn't have", p)
|
|
}
|
|
}
|
|
for _, p := range keySlotFree {
|
|
if !slices.Contains(mux.patterns, p) {
|
|
t.Errorf("keySlotFree names %q, which the router doesn't have", p)
|
|
}
|
|
}
|
|
}
|
|
|
|
// lanServer serves s as it is to another computer on the LAN.
|
|
func lanServer(t *testing.T, s *Server) *httptest.Server {
|
|
h := lanGuard(s.Handler())
|
|
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.RemoteAddr = "192.168.1.9:5000"
|
|
h.ServeHTTP(w, r)
|
|
}))
|
|
t.Cleanup(gw.Close)
|
|
return gw
|
|
}
|
|
|
|
// keyCap gives the key id a MaxConcurrency of n.
|
|
func keyCap(t *testing.T, id string, n int) {
|
|
t.Helper()
|
|
if _, err := access.Update("concurrency-key", access.Change{Key: id, MaxConcurrency: n}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// young02036 on Discord: a gateway key with a MaxConcurrency of 2 has no
|
|
// more than two requests at the vendor at once, from another computer on
|
|
// the LAN. A third waits in the key's queue, shown as waiting for a slot,
|
|
// and goes once one before it is read to its end; one whose agent hangs
|
|
// up while it waits leaves the queue and never reaches the vendor; another
|
|
// key isn't held by it; and the wait is the try's Queued.
|
|
func TestKeyConcurrency(t *testing.T) {
|
|
fresh(t)
|
|
v := &slowVendor{release: map[string]chan struct{}{}, quit: make(chan struct{})}
|
|
up := httptest.NewServer(v)
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, secrets := newCaller(t, "Team", "Other")
|
|
team, other := keys[0], keys[1]
|
|
keyCap(t, team.ID, 2)
|
|
s := New()
|
|
gw := lanServer(t, s)
|
|
t.Cleanup(func() { close(v.quit) })
|
|
|
|
lane := func() Lane { return s.KeyLanes()[team.ID] }
|
|
type reply struct {
|
|
tag, body string
|
|
err error
|
|
}
|
|
replies := make(chan reply, 10)
|
|
send := func(ctx context.Context, secret, tag string) {
|
|
body := `{"model":"slow/m","stream":true,"messages":[{"role":"user","content":"` + tag + `"}]}`
|
|
go func() {
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", gw.URL+"/v1/chat/completions", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+secret)
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
replies <- reply{tag: tag, err: err}
|
|
return
|
|
}
|
|
b, err := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
replies <- reply{tag, string(b), err}
|
|
}()
|
|
}
|
|
|
|
send(context.Background(), secrets[0], "a")
|
|
within(t, "a at the vendor", func() bool { return len(v.seen()) == 1 })
|
|
send(context.Background(), secrets[0], "b")
|
|
within(t, "b at the vendor", func() bool { return len(v.seen()) == 2 })
|
|
send(context.Background(), secrets[0], "c")
|
|
within(t, "c queued", func() bool { return lane().Waiting == 1 })
|
|
if l := lane(); l.Busy != 2 || l.Limit != 2 {
|
|
t.Fatalf("key lane = %+v, want 2 out, 1 waiting", l)
|
|
}
|
|
if !slices.ContainsFunc(s.Active(), func(a Active) bool { return a.Phase == "slot" }) {
|
|
t.Fatalf("no request shows waiting for a slot: %+v", s.Active())
|
|
}
|
|
|
|
// another key isn't held by this one's
|
|
send(context.Background(), secrets[1], "other")
|
|
within(t, "the other key's at the vendor", func() bool { return slices.Contains(v.seen(), "other") })
|
|
close(v.gate("other"))
|
|
if r := <-replies; r.tag != "other" || !strings.Contains(r.body, "[DONE]") {
|
|
t.Fatalf("the other key's reply = %+v", r)
|
|
}
|
|
if _, ok := s.KeyLanes()[other.ID]; ok {
|
|
t.Fatal("a key with no cap has a lane")
|
|
}
|
|
|
|
// gone while it waits: out of the queue, never sent
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
send(ctx, secrets[0], "gone")
|
|
within(t, "gone queued", func() bool { return lane().Waiting == 2 })
|
|
cancel()
|
|
within(t, "gone out of the queue", func() bool { return lane().Waiting == 1 })
|
|
if r := <-replies; r.tag != "gone" || r.err == nil {
|
|
t.Fatalf("the canceled request answered: %+v", r)
|
|
}
|
|
|
|
// c goes only once a is read to its end, and then the slot of the
|
|
// stream it holds is the key's until it ends
|
|
time.Sleep(50 * time.Millisecond)
|
|
if slices.Contains(v.seen(), "c") {
|
|
t.Fatal("c reached the vendor with two out")
|
|
}
|
|
close(v.gate("a"))
|
|
if r := <-replies; r.tag != "a" || !strings.Contains(r.body, "[DONE]") {
|
|
t.Fatalf("a = %+v", r)
|
|
}
|
|
within(t, "c at the vendor", func() bool { return slices.Contains(v.seen(), "c") })
|
|
if l := lane(); l.Busy != 2 || l.Waiting != 0 {
|
|
t.Fatalf("key lane after a = %+v, want 2 out", l)
|
|
}
|
|
for _, tag := range []string{"b", "c"} {
|
|
close(v.gate(tag))
|
|
if r := <-replies; r.err != nil || !strings.Contains(r.body, "[DONE]") {
|
|
t.Fatalf("%s = %+v", tag, r)
|
|
}
|
|
}
|
|
if got := v.seen(); slices.Contains(got, "gone") {
|
|
t.Fatalf("the canceled request reached the vendor: %v", got)
|
|
}
|
|
within(t, "every key slot given back", func() bool { return len(s.KeyLanes()) == 0 })
|
|
|
|
// c's wait is its try's Queued
|
|
var queued int64
|
|
for _, rt := range s.Trace(context.Background(), 0, 0).Routes {
|
|
for _, tr := range rt.Tries {
|
|
queued = max(queued, tr.Queued)
|
|
}
|
|
}
|
|
if queued < 40 {
|
|
t.Fatalf("the queued try's Queued = %dms, want its wait", queued)
|
|
}
|
|
}
|
|
|
|
// A stream waiting for its key's slot past keepHeldAfter is sent its 200
|
|
// and SSE comments meanwhile, as one waiting for a provider's slot is
|
|
// (keepQueued): its agent's timeout for headers doesn't end it.
|
|
func TestKeyConcurrencyKeepsStreamAlive(t *testing.T) {
|
|
fresh(t)
|
|
k, e := keepHeldAfter, watchEvery
|
|
keepHeldAfter, watchEvery = 10*time.Millisecond, 10*time.Millisecond
|
|
t.Cleanup(func() { keepHeldAfter, watchEvery = k, e })
|
|
v := &slowVendor{release: map[string]chan struct{}{}, quit: make(chan struct{})}
|
|
up := httptest.NewServer(v)
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, secrets := newCaller(t, "Team")
|
|
keyCap(t, keys[0].ID, 1)
|
|
s := New()
|
|
gw := lanServer(t, s)
|
|
t.Cleanup(func() { close(v.quit) })
|
|
post := func(tag string) *http.Response {
|
|
body := `{"model":"slow/m","stream":true,"messages":[{"role":"user","content":"` + tag + `"}]}`
|
|
req, _ := http.NewRequest("POST", gw.URL+"/v1/chat/completions", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer "+secrets[0])
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { res.Body.Close() })
|
|
return res
|
|
}
|
|
first := post("first")
|
|
if line, _ := bufio.NewReader(first.Body).ReadString('\n'); !strings.Contains(line, "from first") {
|
|
t.Fatalf("first = %q", line)
|
|
}
|
|
// the second has its headers while it waits, nothing sent upstream
|
|
second := post("second")
|
|
if second.StatusCode != 200 || !strings.Contains(second.Header.Get("Content-Type"), "event-stream") {
|
|
t.Fatalf("waiting stream: %d %s", second.StatusCode, second.Header.Get("Content-Type"))
|
|
}
|
|
if slices.Contains(v.seen(), "second") {
|
|
t.Fatal("second reached the vendor with first out")
|
|
}
|
|
close(v.gate("first"))
|
|
close(v.gate("second"))
|
|
b, _ := io.ReadAll(second.Body)
|
|
if !strings.Contains(string(b), "from second") || !strings.Contains(string(b), "[DONE]") {
|
|
t.Fatalf("second's stream = %.300s", b)
|
|
}
|
|
}
|
|
|
|
// The routes that wait before their handler (keySlotEager) hold a key to
|
|
// its cap as well: with 1, the second waits for the first's handler to
|
|
// return, one gone while waiting never runs, and a key issued under it
|
|
// waits for a slot of its issuer's too.
|
|
func TestKeyConcurrencyEager(t *testing.T) {
|
|
fresh(t)
|
|
var in, most atomic.Int32
|
|
var mu sync.Mutex
|
|
ran := []string{}
|
|
gates := map[string]chan struct{}{}
|
|
gate := func(tag string) chan struct{} {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if gates[tag] == nil {
|
|
gates[tag] = make(chan struct{})
|
|
}
|
|
return gates[tag]
|
|
}
|
|
mux := &routeMux{ServeMux: http.NewServeMux()}
|
|
mux.HandleFunc("POST /v1/embeddings", func(w http.ResponseWriter, r *http.Request) {
|
|
n := in.Add(1)
|
|
defer in.Add(-1)
|
|
for m := most.Load(); n > m && !most.CompareAndSwap(m, n); m = most.Load() {
|
|
}
|
|
tag := r.URL.Query().Get("t")
|
|
mu.Lock()
|
|
ran = append(ran, tag)
|
|
mu.Unlock()
|
|
<-gate(tag)
|
|
})
|
|
s := New()
|
|
h := s.keySlots(mux, mux)
|
|
parent := access.Identity{KeyID: "p", KeyName: "Parent", MaxConcurrency: 1}
|
|
child := access.Identity{KeyID: "c", KeyName: "Child", Issuer: &parent}
|
|
done := make(chan string, 8)
|
|
send := func(ctx context.Context, who access.Identity, tag string) {
|
|
go func() {
|
|
r := httptest.NewRequestWithContext(access.WithIdentity(ctx, who), "POST", "/v1/embeddings?t="+tag, strings.NewReader("{}"))
|
|
h.ServeHTTP(httptest.NewRecorder(), r)
|
|
done <- tag
|
|
}()
|
|
}
|
|
seen := func() []string { mu.Lock(); defer mu.Unlock(); return slices.Clone(ran) }
|
|
send(context.Background(), parent, "one")
|
|
within(t, "one runs", func() bool { return len(seen()) == 1 })
|
|
send(context.Background(), child, "issued")
|
|
within(t, "the issued key's waits for its issuer's slot", func() bool { return s.KeyLanes()["p"].Waiting == 1 })
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
send(ctx, parent, "gone")
|
|
within(t, "gone queued", func() bool { return s.KeyLanes()["p"].Waiting == 2 })
|
|
cancel()
|
|
if tag := <-done; tag != "gone" {
|
|
t.Fatalf("%s returned first", tag)
|
|
}
|
|
close(gate("one"))
|
|
within(t, "the issued key's runs", func() bool { return slices.Contains(seen(), "issued") })
|
|
close(gate("issued"))
|
|
<-done
|
|
<-done
|
|
if got := seen(); !slices.Equal(got, []string{"one", "issued"}) || most.Load() != 1 {
|
|
t.Fatalf("ran %v, %d at once", got, most.Load())
|
|
}
|
|
within(t, "every key slot given back", func() bool { return len(s.KeyLanes()) == 0 })
|
|
// a GET, or a key with no cap, is never held
|
|
r := httptest.NewRequest("GET", "/v1/embeddings", nil)
|
|
h.ServeHTTP(httptest.NewRecorder(), r.WithContext(access.WithIdentity(r.Context(), access.Identity{KeyID: "p", MaxConcurrency: 1})))
|
|
if len(s.KeyLanes()) != 0 {
|
|
t.Fatal("a GET took a slot")
|
|
}
|
|
}
|