Files
magpie/internal/gui/many_keys_test.go
T
yetone 0bbb2f3ca4 provider, gui, gateway: a thousand keys are listed once and read eight at a time (@NetRozee on X)
@NetRozee on X: "when I add a thousand keys for a custom endpoint magpie
just froze or crash by itself".

Cause. The Providers page asked, for each key, what it rests by
(RestKey), and RestKey listed every key on again (KeysOn), which itself
looked along the list for each key: cubic in the keys. /api/providers
and every /api/keys/* answer, an import too, are drawn through it. The
gateway asked, for each key of every request, whether it serves the
model (Serves), and each asking read the provider's lists again (a
filememo stat, and a read while the file is new) and looked along each
model's key ids. fetchPerKey looked along the lists for each key and
model the same way.

Fix.
- KeysOn drops repeats by a set.
- RestKeys finds once whether several keys are on; providerInfo uses it.
- Serving(model) reads the lists once and answers for each key from sets
  (keyLists, made again when the lists are read anew); perKeyBarred and
  the test pick use it. Serves is Serving for one key.
- fetchPerKey keeps each model's keys as a set and indexes the old list
  by key once; a model kept for a failed key is kept once, as before.
- KeysListed and ListsRead count listings and list reads for the tests.
Behaviour is otherwise the same.

Measured (go test in a sandbox HOME, a custom provider, 50 models):
                         1000 keys          5000 keys
  providerInfo           1.54s -> 7.5ms     2m42.9s -> 18.3ms
  providersState         1.65s -> 110ms     2m40.3s -> 146ms
  POST /api/keys/import  1.64s -> 82ms      2m44.7s -> 288ms
  of one more key
  gateway perKey         100.9ms -> 8.2ms   (old not run) -> 40ms
  (each request)

Tests (counts, not time), each red on the old code by hand:
- TestProviderInfoListsKeysOnce: old 1001 listings for 1000 keys, 11 for
  10; new 0 and 0.
- TestPerKeyReadsListsOnce: old 1001 list reads for 1000 keys, 11 for
  10; new 1 and 1.
- TestServingReadsListsOnce, TestRestKeysIsRestKey,
  TestKeysOnDropsRepeats: same answers as before.

And the vendor reads. KeyBalances, PlanQuotas and remoteKeyCards started a goroutine
for each key and asked the vendor with all of them at once: a thousand
keys were a thousand requests to one relay together, each holding a
connection and its body.

Fix. keySlots holds each provider's reads to keyReadsAtOnce (8); other
providers' keys are read beside them. A read whose context is done goes
on without a slot and fails at once, as before.

Tests:
- TestKeyReadsEightAtOnce: a Command Code provider of 31 keys and a
  remote magpie of 31 keys behind a server that holds every request.
  Old code (the slots taken out by hand): 31 at once for balances, plans
  and remote limits; new: 8, every card still there.
- TestKeySlots: a done context takes no slot; another provider's keys
  have their own.

Run: gofmt -l (clean); GOOS=linux and GOOS=windows go vet -tags nogui
./... (clean); go test -tags nogui -count=1 -timeout 45m ./... in a sandbox HOME (GOCACHE, GOMODCACHE pinned) on af601541e with this series: 66 packages ok, 18 without tests, no FAIL (gateway 468.5s, provider 215.9s, gui 122.8s).
2026-10-16 08:43:02 +08:00

49 lines
1.6 KiB
Go

package gui
import (
"fmt"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// @NetRozee on X: a custom endpoint given a thousand keys froze magpie.
// The Providers page asked, for each key, what it rests by, and that
// listed every key on again: a thousand keys took 1.5s to draw, five
// thousand 2m40s, and each answer of /api/keys/* (an import too) waited
// as long. The listings are counted, not timed: a provider's row lists
// its keys as often with a thousand keys as with ten.
func TestProviderInfoListsKeysOnce(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
lists := func(n int) int64 {
p := provider.Provider{ID: fmt.Sprintf("relay%d", n), Name: "Relay", Chat: "http://127.0.0.1:1/v1", Key: "sk-first"}
for i := range n {
p.Keys = append(p.Keys, provider.KeyAccount{Key: fmt.Sprintf("sk-key-%d-%04d", n, i)})
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
q, err := provider.Find(p.ID)
if err != nil {
t.Fatal(err)
}
providerInfo(*q, nil)
before := provider.KeysListed()
info := providerInfo(*q, nil)
if len(info.KeyList) != n+1 {
t.Errorf("%d keys listed, want %d", len(info.KeyList), n+1)
}
return provider.KeysListed() - before
}
few, many := lists(10), lists(1000)
t.Logf("keys listed %d times for 10 keys, %d for 1000", few, many)
if many > few+2 {
t.Errorf("a provider's row listed its keys %d times with 1000 keys, %d with 10: once a key", many, few)
}
}