diff --git a/internal/provider/hold.go b/internal/provider/hold.go index c1359b34f..5ee3b5612 100644 --- a/internal/provider/hold.go +++ b/internal/provider/hold.go @@ -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 diff --git a/internal/provider/stale_sweep_test.go b/internal/provider/stale_sweep_test.go new file mode 100644 index 000000000..8d5524bd5 --- /dev/null +++ b/internal/provider/stale_sweep_test.go @@ -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") + } + }() +}