From d3fc6f2bfada18e6ae7887b79ed1e4c650ca37e1 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 10 Nov 2026 03:33:45 +0800 Subject: [PATCH] provider: FileError and groupsKeeping read providers.json's kept parse Each Providers page asked FileError whether providers.json reads, and groupsKeeping whether any group unlists models; outside a held request both parsed the whole file again (6.7MB growslice of 112MB per 40 /api/providers asks on the real-account copy). Both now use the parse filememo keeps until the file changes (storedFile); FileError falls back to read() only when that parse fails, so a broken file still says so with read's own error. Both are read-only uses. Run: TestFileErrorReadsParsedFileOnce red with the memo path taken out ("parsed the file 3 more times"), green with it; TestFileError* and the unlist tests on mote. --- internal/provider/models.go | 3 ++- internal/provider/provider.go | 10 ++++++++++ internal/provider/store_unreadable_test.go | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/internal/provider/models.go b/internal/provider/models.go index a326f2a91..f050dfb86 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -3199,7 +3199,8 @@ func unlistedByGroups(entries []Entry) map[string][]string { // groupsKeeping is unlistedByGroups with the groups themselves, by the // models, "provider/model", they keep out of the lists. func groupsKeeping(entries []Entry) map[string][]Group { - if !slices.ContainsFunc(heldOf("file", load).Groups, func(g Group) bool { return g.Unlists }) { + // read only: the file parsed once until it changes (storedFile) + if !slices.ContainsFunc(storedFile().Groups, func(g Group) bool { return g.Unlists }) { return nil } out := map[string][]Group{} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index e2bc6452e..a16bd0e33 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -760,7 +760,12 @@ func (e *unreadableError) Unwrap() []error { return []error{ErrUnreadable, e.err // Reads used for an edit must keep errors: a broken file is not an empty // catalog to write over. Only a missing file is a first use. +// fileReads counts read, for the test that a page's look at whether the +// file parses doesn't parse it again. +var fileReads atomic.Int64 + func read() (file, error) { + fileReads.Add(1) var f file b, err := steady.ReadFile(Path()) if errors.Is(err, os.ErrNotExist) { @@ -798,6 +803,11 @@ var utf8BOM = []byte("\xef\xbb\xbf") // FileError is why providers.json can't be read, nil when it can or isn't // there: for what lists the providers to say so, not "none yet". func FileError() error { + // a file that parses, parsed once until it changes (storedFile): each + // Providers page parsed the whole of it again only to see it does + if _, err := filememo.Read("providers", Path(), parseProviders); err == nil { + return nil + } _, err := read() return err } diff --git a/internal/provider/store_unreadable_test.go b/internal/provider/store_unreadable_test.go index 77830c687..7fc555d1a 100644 --- a/internal/provider/store_unreadable_test.go +++ b/internal/provider/store_unreadable_test.go @@ -126,3 +126,24 @@ func TestStoreReplacesAtomically(t *testing.T) { t.Fatalf("temp files left: %v", left) } } + +// A look at whether providers.json reads (each Providers page asks) uses +// the parse kept until the file changes, not a whole parse of its own. +func TestFileErrorReadsParsedFileOnce(t *testing.T) { + providerFileHome(t) + writeProviderFile(t, originalProviderFile) + _ = storedFile() + before := fileReads.Load() + for range 3 { + if err := FileError(); err != nil { + t.Fatalf("a file that reads: %v", err) + } + } + if n := fileReads.Load() - before; n != 0 { + t.Fatalf("FileError parsed the file %d more times, want none while it is unchanged", n) + } + writeProviderFile(t, originalProviderFile[:40]) + if err := FileError(); !errors.Is(err, ErrUnreadable) { + t.Fatalf("a file cut short: %v", err) + } +}