provider: the catalog the last request built goes once its time is past

What a request held (heldFor) and what was kept across requests (keptFor:
the catalog, the switched-off providers' models, the groups) were dropped
only when the next request looked. With no request after it they stayed
for the life of the process: with a copy of the real accounts, an idle
magpie's end heap held 7.5 MB of catalog built at start by RenameLegacy
(catalogKept and entriesOf, through piOffered), 16.8% of a 47 MB heap,
for nothing that would read it again.

Now a request that holds or keeps something arms a sweep for when the
first of it is past its time; the sweep drops what is past and arms again
for the next of the rest, or stops when nothing is left. It reads the
durations as the arm read them, so a test setting heldFor meanwhile is
not raced, and a sooner time arms a sooner sweep than one already armed.

TestHeldAndKeptGoWithNoRequestAfter is red with the arm removed (6 held
and 1 kept 10s after the request).

-race on internal/provider: TestSignInDoneOnceTheAccountIsBack (#3100)
and TestPluginCheckinAsksAgainAtUntil (#6014, filed now) race the same
way on origin/main 72f2deebe; the new test passes under -race -count=3.

Ran on mote: go test ./internal/gateway ./internal/agent ./internal/gui
ok at the base 72f2deebe; ./internal/provider ok alone there (in the
four-package run TestCodexListTagNotKeptOverAFreshFile was red under load,
#4216; it passes -count=20 alone). Rebased on 47e30d0e9 (internal/provider
changed): go test ./internal/provider ok, GOOS=windows go vet ./... and
GOOS=linux go vet -tags nogui ./... ok at this head.

Not measured yet: the idle footprint with the real-account copy.
This commit is contained in:
yetone
2026-11-11 04:13:12 +08:00
parent 47e30d0e97
commit 5d6935b11a
2 changed files with 137 additions and 0 deletions
+78
View File
@@ -303,6 +303,7 @@ func heldOf[T any](key string, build func() T) T {
held.built = map[string]heldValue{}
}
held.built[key] = heldValue{v, time.Now()}
armStaleSweep()
}
delete(heldFlight, key)
held.Unlock()
@@ -387,11 +388,87 @@ func keptFit[T any](key string, fits func(any) bool, build func() T) T {
kept.of = map[string]any{}
}
kept.of[key] = v
armStaleSweep()
}
kept.Unlock()
return v
}
// staleFor is heldFor and keptFor as the last arm read them, in the
// request that held or kept something (the sweep runs on a timer of its
// own and reads these rather than what a test may be setting meanwhile),
// and when the sweep armed soonest will run; zero: none is armed.
var staleFor struct {
sync.Mutex
held, kept time.Duration
next time.Time
}
// armStaleSweep arms the sweep, unless one is armed for as soon: what was
// held or kept past its time was dropped only when the next request
// looked, so the catalog the last request built (~7.5 MB with real
// accounts) stayed for the life of a magpie nobody asked again.
func armStaleSweep() {
staleFor.Lock()
defer staleFor.Unlock()
staleFor.held, staleFor.kept = heldFor, keptFor
staleArmLocked(min(heldFor, keptFor))
}
// staleArmLocked arms the sweep d from now, unless one is armed sooner.
// staleFor is held.
func staleArmLocked(d time.Duration) {
d = max(d, 10*time.Millisecond)
at := time.Now().Add(d)
if !staleFor.next.IsZero() && !at.Before(staleFor.next) {
return
}
staleFor.next = at
time.AfterFunc(d, sweepStale)
}
// sweepStale drops what is held past heldFor and kept past keptFor, and
// looks again when the next of the rest will be, until nothing is left.
func sweepStale() {
staleFor.Lock()
heldFor, keptFor := staleFor.held, staleFor.kept
// let go first: what is held or kept while this one looks arms its own
staleFor.next = time.Time{}
staleFor.Unlock()
next := time.Duration(-1)
soon := func(left time.Duration) {
if next < 0 || left < next {
next = left
}
}
held.Lock()
for k, h := range held.built {
if left := heldFor - time.Since(h.at); left <= 0 {
delete(held.built, k)
} else {
soon(left)
}
}
if len(held.built) == 0 {
held.built = nil
}
held.Unlock()
kept.Lock()
if kept.of != nil {
if left := keptFor - time.Since(kept.at); left <= 0 {
kept.of = nil
} else {
soon(left)
}
}
kept.Unlock()
if next >= 0 {
staleFor.Lock()
staleArmLocked(next)
staleFor.Unlock()
}
}
// holdingNow is whether a request holds the catalog now.
func holdingNow() bool {
held.Lock()
@@ -456,6 +533,7 @@ func heldFrom[T any](key string, entries []Entry, build func([]Entry) T) T {
held.built = map[string]heldValue{}
}
held.built[key] = heldValue{f, time.Now()}
armStaleSweep()
}
held.Unlock()
return v
+59
View File
@@ -0,0 +1,59 @@
package provider
import (
"path/filepath"
"testing"
"time"
)
// What the last request held and kept goes once its time is past, with no
// request after it: the catalog was dropped only when the next one looked,
// so a magpie nobody asked again kept the last catalog for good.
func TestHeldAndKeptGoWithNoRequestAfter(t *testing.T) {
isolate(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"))
defer func(d, k time.Duration) { heldFor, keptFor = d, k }(heldFor, keptFor)
heldFor, keptFor = 50*time.Millisecond, 100*time.Millisecond
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
func() {
defer Hold()()
if _, _, ok := Resolve("relay/m1"); !ok {
t.Fatal("relay/m1 not resolved")
}
}()
left := func() (int, int) {
held.Lock()
h := len(held.built)
held.Unlock()
kept.Lock()
defer kept.Unlock()
return h, len(kept.of)
}
if h, k := left(); h == 0 || k == 0 {
t.Fatalf("after a request: %d held, %d kept; the test looks at nothing", h, k)
}
deadline := time.Now().Add(10 * time.Second) // room for a loaded machine
for {
h, k := left()
if h == 0 && k == 0 {
break
}
if time.Now().After(deadline) {
t.Fatalf("10s after the last request, %d held and %d kept, past %v and %v", h, k, heldFor, keptFor)
}
time.Sleep(20 * time.Millisecond)
}
// and a request after the let-go resolves as before
func() {
defer Hold()()
if _, _, ok := Resolve("relay/m1"); !ok {
t.Fatal("relay/m1 not resolved after the let-go")
}
}()
}