providers: a key or account with no limit on requests at once shows how many run (chentuoyan on Discord)

The account and key rows' lane pill said only "No limit at once" when no
concurrency limit was set, never how many requests were out. Now, with no
limit and requests out, it says "{n} running" (zh: "{n} 运行中") and is
shown without a hover, in the same pill style (no full-lane or queue
colour). Idle with no limit it says "No limit at once" again and goes back
to hover-only. Rows with a limit are unchanged.

Backend: /api/lanes (Server.Lanes, /v1/magpie/concurrency's data) only
listed lanes with a limit, because lanes.take took nothing for limit 0.
take now counts a request with no limit in its lane (Limit 0, never
waits; grant still lets any waiter go at once when a limit is lifted) and
gives it back through the same once-only releaser, so Lanes says Busy for
it. Cost: the same one mutex take every request already paid, plus one
small lane and closure; /api/lanes, read every 2s, copies a map of only
the lanes with requests out. A limit set while unlimited requests are out
now counts them (the vendor does see them). The only caller of take
(serve) always calls release (deferred after attempt, or on an RPM
failure). Strings in zh, zh-TW, ja and de. gateway-routing.md says it.

Tests:
- TestLanesCountWithNoLimit (new, internal/gateway): three held streams on
  a provider with no MaxConcurrency read Lane{Busy:3}; all gone after.
  Red on origin/main's concurrency.go: "lane = {Busy:0 Waiting:0 Limit:0},
  want 3 out, none waiting, no limit".
- TestLanesLimitLifted now releases its limit-0 acquire (it was a no-op
  before).
- lane-limit.test.cjs "with no limit, what runs is shown, and none running
  says no limit again" (Chromium and WebKit, en and zh), red on
  origin/main's app.js: expected '3 running', actual 'No limit at once'
  (zh: '3 运行中' vs '并发不限'). lane-limit.test.cjs whole: 16/16 ok on mote.

Run on mote at the tree rebased on 655f8dbd1: gofmt -l clean; go vet
./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...
clean; go test ./internal/gateway ok (593s); ./internal/gui all ok,
TestCoreScriptBudget included (app.js 1,311,665 <= 1,311,681, no budget
raised), except TestMainWindowCorners/macOS_26_SDK, #3949, red the same
way on a clean origin/main 655f8dbd1 run on mote.
Rebased again on e5e2aa58e (its commits change gateway fallback/wallets,
routes and gateway-routing.md's Limits row, merged by hand; none of the
lane code). At this head on mote: gofmt -l clean, go vet ./internal/gateway
./internal/gui clean, the gateway's lane/concurrency/busy tests and
TestCoreScriptBudget ok, lane-limit and i18n-keys in WebKit 12/12 ok.
Whole GUI suite (node --test, both engines, every language): NOT
finished. A run at the 655f8dbd1 tree was ~25 min in (load 60-100) when
my own later mote-run into the same copy deleted its log, so its result
is lost; it is run again at this head after the push. Reds seen before
the log went: account-alias.test.cjs's Usage-page #usageMask click
timeouts (chromium, every language), a page this change doesn't reach;
not yet checked on origin/main.

Not tried: a real gateway with real accounts, and the real Linux/Windows
apps (the pill is the same element and CSS as before). The full go test
./... was not run (only the two affected packages).
This commit is contained in:
yetone
2026-10-29 01:19:08 +08:00
parent 72ca3d593b
commit 53913be7aa
6 changed files with 99 additions and 26 deletions
File diff suppressed because one or more lines are too long
+16 -13
View File
@@ -25,7 +25,8 @@ import (
// for — the vendor sees no more than that many — and what is waited is
// told in the route's try (Try.Queued).
// lanes are the slots of each key or account with a limit.
// lanes are the slots of each key or account with a limit, and the count
// of those out on one without (limit 0), which take no slot and never wait.
type lanes struct {
mu sync.Mutex
m map[string]*lane
@@ -67,7 +68,7 @@ var (
// acquire waits for one of who's limit slots, in turn, with no bound on
// the queue or the wait. It answers the release, to call once the request
// is done with the vendor, and false with no slot taken when ctx ended
// first. A limit of 0 takes no slot.
// first. A limit of 0 never waits; the request is only counted.
func (l *lanes) acquire(ctx context.Context, who string, limit int) (release func(), ok bool) {
release, _, err := l.take(ctx, who, limit, 0, 0)
return release, err == nil
@@ -84,19 +85,20 @@ func (l *lanes) take(ctx context.Context, who string, limit, queue int, wait tim
l.m = map[string]*lane{}
}
ln := l.m[who]
if limit <= 0 {
if ln != nil {
// the limit was lifted: whoever waits goes now
ln.limit = 0
ln.grant()
}
l.mu.Unlock()
return func() {}, 0, nil
}
if ln == nil {
ln = &lane{}
l.m[who] = ln
}
if limit <= 0 {
// the limit was lifted: whoever waits goes now. With none, the
// request is still counted while it is out, for the pill that
// says how many run (chentuoyan on Discord)
ln.limit = 0
ln.grant()
ln.busy++
l.mu.Unlock()
return l.releaser(who, ln), 0, nil
}
ln.limit = limit
ln.grant() // a limit raised lets more of those waiting go
if ln.busy < limit && len(ln.queue) == 0 {
@@ -263,8 +265,9 @@ func (s *Server) concurrency(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"object": "concurrency", "data": lanes, "requests": b.Requests, "waiting": waiting, "tools": b.Tools, "active": s.Active()})
}
// Lanes is how each key or account with requests out or waiting under a
// limit stands, by its who (provider, provider#key, provider@account).
// Lanes is how each key or account with requests out or waiting stands,
// limit or none (Limit 0), by its who (provider, provider#key,
// provider@account).
func (s *Server) Lanes() map[string]Lane {
s.lanes.mu.Lock()
defer s.lanes.mu.Unlock()
+44 -1
View File
@@ -227,7 +227,8 @@ func TestLanesLimitLifted(t *testing.T) {
defer l.mu.Unlock()
return l.m["p"] != nil && len(l.m["p"].queue) == 1
})
if _, ok := l.acquire(context.Background(), "p", 0); !ok {
r3, ok := l.acquire(context.Background(), "p", 0)
if !ok {
t.Fatal("no limit refused")
}
select {
@@ -238,6 +239,7 @@ func TestLanesLimitLifted(t *testing.T) {
}
r1()
r1() // twice is once
r3()
l.mu.Lock()
defer l.mu.Unlock()
if len(l.m) != 0 {
@@ -245,6 +247,47 @@ func TestLanesLimitLifted(t *testing.T) {
}
}
// A key with no limit on requests at once still has its requests out
// counted (chentuoyan on Discord: the Providers page's pill says how many
// run), as a Lane with Limit 0 and nobody waiting, gone once they are done.
func TestLanesCountWithNoLimit(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)
}
s := New()
gw := httptest.NewServer(s.Handler())
t.Cleanup(gw.Close)
t.Cleanup(func() { close(v.quit) })
lane := func() Lane { return s.Lanes()["slow#"+provider.KeyID("k")] }
replies := make(chan error, 3)
for _, tag := range []string{"a", "b", "c"} {
body := `{"model":"slow/m","stream":true,"messages":[{"role":"user","content":"` + tag + `"}]}`
go func() {
res, err := http.Post(gw.URL+"/v1/chat/completions", "application/json", strings.NewReader(body))
if err == nil {
_, err = io.ReadAll(res.Body)
res.Body.Close()
}
replies <- err
}()
}
within(t, "three at the vendor", func() bool { return len(v.seen()) == 3 })
if l := lane(); l != (Lane{Busy: 3}) {
t.Fatalf("lane = %+v, want 3 out, none waiting, no limit", l)
}
for _, tag := range []string{"a", "b", "c"} {
close(v.gate(tag))
if err := <-replies; err != nil {
t.Fatal(err)
}
}
within(t, "every count given back", func() bool { return len(s.Lanes()) == 0 })
}
// Concurrency is the user's setting, else none; 0 set is none, and a
// negative one saved is 0.
func TestProviderConcurrency(t *testing.T) {
+7 -8
View File
@@ -13829,9 +13829,10 @@ function accountCapPill(p, user, cap, direct) {
}
// An account's or key's limit on requests at once (#892): its own, else
// the provider's Concurrency. The pill says it, and how many run and wait
// (the lanes, read every 2s while one is in sight: refreshLanes), and opens
// the app's menu: the provider's, none, a number, or the user's own.
// the provider's Concurrency. The pill says it, and how many run and wait,
// with no limit too (chentuoyan) (the lanes, read every 2s while one is in
// sight: refreshLanes), and opens the app's menu: the provider's, none, a
// number, or the user's own.
const LANE_LIMITS = [1, 2, 3, 5, 10];
let lanesNow = {}, lanesRead = false, lanesTick;
function laneOwnOf(p, ref) {
@@ -13849,13 +13850,11 @@ function laneText(pill) {
const own = laneOwnOf(p, pill.dataset.ref), limit = laneLimitOf(p, pill.dataset.ref);
const lane = (lanesRead ? lanesNow : providers?.gateway?.lanes || {})[pill.dataset.who];
const busy = lane?.busy || 0, waiting = lane?.waiting || 0;
let text;
if (limit && (busy || waiting)) text = waiting ? t("{n}/{max} running · {q} queued", { n: busy, max: limit, q: waiting }) : t("{n}/{max} running", { n: busy, max: limit });
else if (limit) text = t("{n} at once", { n: limit });
else text = t("No limit at once");
const v = { n: busy, max: limit, q: waiting };
const text = !limit ? t(busy ? "{n} running" : "No limit at once", v) : waiting ? t("{n}/{max} running · {q} queued", v) : t(busy ? "{n}/{max} running" : "{n} at once", busy ? v : { n: limit });
if (pill.textContent !== text) pill.textContent = text;
lanesTick ||= setInterval(refreshLanes, 2e3);
pill.classList.toggle("set", own != null || limit > 0);
pill.classList.toggle("set", own != null || limit + busy > 0);
pill.classList.toggle("busy-lane", !!(limit && busy >= limit));
pill.classList.toggle("queued", !!waiting);
const whose = own != null ? t("its own") : t("the provider's");
+4
View File
@@ -2137,6 +2137,7 @@ const I18N = {
"Requests per minute: a whole number from 0 to 10000": "每分钟请求数:0 到 10000 的整数",
"{n}/{max} running · {q} queued": "{n}/{max} 运行中 · {q} 排队",
"{n}/{max} running": "{n}/{max} 运行中",
"{n} running": "{n} 运行中",
"{n} at once": "并发 {n}",
"No limit at once": "并发不限",
"its own": "单独设置",
@@ -6177,6 +6178,7 @@ const I18N = {
"Requests per minute: a whole number from 0 to 10000": "每分鐘請求數:0 到 10000 的整數",
"{n}/{max} running · {q} queued": "{n}/{max} 執行中 · {q} 排隊",
"{n}/{max} running": "{n}/{max} 執行中",
"{n} running": "{n} 執行中",
"{n} at once": "並行 {n}",
"No limit at once": "並行不限",
"its own": "單獨設定",
@@ -10196,6 +10198,7 @@ const I18N = {
"Requests per minute: a whole number from 0 to 10000": "1 分間のリクエスト数:0〜10000 の整数",
"{n}/{max} running · {q} queued": "{n}/{max} 実行中 · {q} 待ち",
"{n}/{max} running": "{n}/{max} 実行中",
"{n} running": "{n} 実行中",
"{n} at once": "同時 {n}",
"No limit at once": "同時数 無制限",
"its own": "個別設定",
@@ -14245,6 +14248,7 @@ const I18N = {
"Requests per minute: a whole number from 0 to 10000": "Anfragen pro Minute: eine ganze Zahl von 0 bis 10000",
"{n}/{max} running · {q} queued": "{n}/{max} laufen · {q} warten",
"{n}/{max} running": "{n}/{max} laufen",
"{n} running": "{n} laufen",
"{n} at once": "{n} gleichzeitig",
"No limit at once": "Gleichzeitig unbegrenzt",
"its own": "eigene Einstellung",
+27 -3
View File
@@ -72,12 +72,12 @@ function serve(lang, posts, lanes) {
const words = {
en: {
busy: "1/1 running · 2 queued", one: "1 at once", two: "2 at once", three: "3 at once", four: "4 at once", seven: "7 at once", none: "No limit at once",
busy: "1/1 running · 2 queued", running: "3 running", one: "1 at once", two: "2 at once", three: "3 at once", four: "4 at once", seven: "7 at once", none: "No limit at once",
menu: "Requests at once", def: "Provider's", other: "Other…", save: "Save", queue: "Queue size", wait: "Queue wait",
badQueue: "Queue size: a whole number from 0 to 10000",
},
zh: {
busy: "1/1 运行中 · 2 排队", one: "并发 1", two: "并发 2", three: "并发 3", four: "并发 4", seven: "并发 7", none: "并发不限",
busy: "1/1 运行中 · 2 排队", running: "3 运行中", one: "并发 1", two: "并发 2", three: "并发 3", four: "并发 4", seven: "并发 7", none: "并发不限",
menu: "同时请求数", def: "沿用提供商", other: "其他…", save: "保存", queue: "排队上限", wait: "排队等待",
badQueue: "排队上限:0 到 10000 的整数",
},
@@ -209,6 +209,30 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: with no limit, what runs is shown, and none running says no limit again`, async (t) => {
// chentuoyan on Discord: an account with no limit said only "no
// limit", never how many of its requests were out
const lanes = { now: { "codex@me@example.com": { busy: 3, waiting: 0, limit: 0 } }, reads: 0 };
const { page, errors } = await open(t, "Codex", lanes);
const me = pill(page, "me@example.com");
for (let i = 0; i < 80 && (await me.textContent()) !== w.running; i++) await page.waitForTimeout(50);
assert.equal(await me.textContent(), w.running);
await page.mouse.move(0, 0);
await page.waitForTimeout(250);
assert.equal(await opacity(me), "1", "running: shown without a hover");
// the rest of the row's pill look, no colour of a full lane
assert.equal(await me.evaluate((e) => e.classList.contains("busy-lane") || e.classList.contains("queued")), false);
const el = await me.elementHandle();
lanes.now = {};
for (let i = 0; i < 80 && (await me.textContent()) !== w.none; i++) await page.waitForTimeout(50);
assert.equal(await me.textContent(), w.none);
assert.equal(await el.evaluate((e) => e.isConnected), true, "the same pill, rewritten in place");
await page.waitForTimeout(250);
assert.equal(await opacity(me), "0", "idle with no limit: on hover only again");
assert.equal(await pill(page, "Spare@Example.com").textContent(), w.four);
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: the queue's length and wait are saved with the editor, and refused out of range`, async (t) => {
const lanes = { now: {}, reads: 0 };
const { page, errors, posts } = await open(t, "Relay", lanes);
@@ -247,7 +271,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
"How many requests may wait for each key or account; one more is turned away at once. 0 or empty is no bound",
"Seconds a request waits for a free slot before it is turned away. 0 or empty waits as long as it takes",
"Queue size: a whole number from 0 to 10000", "Queue wait: a whole number of seconds from 0 to 3600",
"{n}/{max} running · {q} queued", "{n}/{max} running", "{n} at once", "No limit at once", "its own", "the provider's",
"{n}/{max} running · {q} queued", "{n}/{max} running", "{n} running", "{n} at once", "No limit at once", "its own", "the provider's",
"At most {n} requests out at once on this one, {whose}; more wait in a queue and go in order. Counted across every model, routing group and agent",
"No limit on requests at once on this one, {whose}", "{n} running, {q} queued now", "Click to change",
"{who} takes the provider's limit", "{who}: at most {n} requests at once", "{who}: no limit on requests at once",