Files
magpie/account_concurrency_cli_test.go
T
yetone 39af4adda5 gateway, gui, cli: each account or key can have its own limit on requests at once, with a bounded queue that turns requests away with a 429 (#892, margbug01) — a provider's Concurrency applied to every key and account alike, the queue past it had no bound and no wait, and nothing showed what ran or waited. A key or account now may have its own limit over the provider's (Provider.AccountConcurrency, by lowercased account or KeyID; 0 none; unset takes the provider's, as before), counted in one lane across every model, routing group and agent. A provider's Queue size bounds how many wait for each of its lanes (one more is turned away at once) and Queue wait how many seconds one waits (then it leaves the queue); both 0 by default, as before. A request turned away goes on to the next key, account or group member without resting anything, else gets a 429 in its API's own shape (Anthropic rate_limit_error, OpenAI's, Gemini's) with Retry-After: 1, saying the limit and the queue. A slot is given back when the reply is read to the end, fails or the agent goes away; a waiter that goes away leaves the queue, and one granted as it gave up hands the slot on. In a routing group, or among a provider's keys and accounts, a full one gives way once to another with a free slot now, so a group spreads before anyone waits; a fallback provider is never asked for want of a slot, and with all full the request waits for the one routing chose. The Claude subscription path and plugin providers take their slot the same way, around the attempt; a parked Claude Code tool turn has already answered and holds none. GUI: each account and key row has a pill — "1/1 running · 2 queued" while requests are out (GET /api/lanes, read every two seconds and written into the pill in place), else its limit; none set shows on hover only — opening the app's menu (provider's, no limit, 1/2/3/5/10, Other… typed in place), posting provider/accountconcurrency; the editor has Queue size and Queue wait under Concurrency. CLI: magpie provider account-concurrency <id> [account|key [n|off|default]] and magpie provider queue <id> [size [seconds]]. en/zh/ja/de. TestAccountLimitSharedAcrossGroups, TestQueueFullAndWaitedOut, TestLaneReleasedOnFailure and TestFullMemberGivesWay (pass with -race, with TestMaxConcurrency and TestLanesLimitLifted), TestLaneLimitRoutes and TestAccountConcurrencyCmd are new and fail without the change; lane-limit.test.cjs (WebKit, en and zh: a key's busy/queued pill updated in place from /api/lanes, set from the menu, provider's posts null, Other… typed, an account's own limit, none on hover only, queue fields saved and refused out of range, labels on one line, every string in zh/ja/de, no select, no scroll, no left border) fails without the change. key-weight-fit, account-cap, provider-concurrency, click-scroll, account-models and api-key-usage pass in WebKit; the other WebKit failures fail the same on main. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 18:53:16 +08:00

86 lines
2.7 KiB
Go

package main
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider account-concurrency gives a key its own limit on
// requests at once (by its name or id), off for none, default for the
// provider's again; magpie provider queue bounds the queue past it. A
// key the provider hasn't, or a number out of range, is an error.
func TestAccountConcurrencyCmd(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("DSH_HOME", "")
five := 5
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k1", Chat: "http://127.0.0.1:1/v1", MaxConcurrency: &five}); err != nil {
t.Fatal(err)
}
limit := func() int {
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
return p.LaneLimit()
}
if n := limit(); n != 5 {
t.Fatalf("before: %d, want the provider's 5", n)
}
id := provider.KeyID("k1")
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, "2"}); err != nil {
t.Fatal(err)
}
if n := limit(); n != 2 {
t.Fatalf("own: %d, want 2", n)
}
for _, args := range [][]string{{"provider", "account-concurrency", "relay"}, {"provider", "account-concurrency", "relay", id}} {
if err := providerCmd(args); err != nil {
t.Fatal(err)
}
}
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, "off"}); err != nil {
t.Fatal(err)
}
if n := limit(); n != 0 {
t.Fatalf("off: %d, want none", n)
}
for _, bad := range []string{"-1", "1001", "lots"} {
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, bad}); err == nil {
t.Fatalf("%s taken", bad)
}
}
if err := providerCmd([]string{"provider", "account-concurrency", "relay", "nokey", "3"}); err == nil {
t.Fatal("a key it hasn't was limited")
}
if err := providerCmd([]string{"provider", "account-concurrency", "relay", id, "default"}); err != nil {
t.Fatal(err)
}
if n := limit(); n != 5 {
t.Fatalf("default: %d, want the provider's 5", n)
}
if err := providerCmd([]string{"provider", "queue", "relay", "4", "30"}); err != nil {
t.Fatal(err)
}
p, _ := provider.Find("relay")
if p.QueueLimit != 4 || p.QueueWait != 30 {
t.Fatalf("queue %d, wait %d", p.QueueLimit, p.QueueWait)
}
if err := providerCmd([]string{"provider", "queue", "relay", "off"}); err != nil {
t.Fatal(err)
}
if p, _ = provider.Find("relay"); p.QueueLimit != 0 || p.QueueWait != 30 {
t.Fatalf("off: queue %d, wait %d", p.QueueLimit, p.QueueWait)
}
for _, bad := range [][]string{{"-3"}, {"20000"}, {"2", "4000"}, {"x"}} {
if err := providerCmd(append([]string{"provider", "queue", "relay"}, bad...)); err == nil {
t.Fatalf("%v taken", bad)
}
}
}