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.
This commit is contained in:
yetone
2026-11-10 03:48:24 +08:00
parent fda52fe5dd
commit d3fc6f2bfa
3 changed files with 33 additions and 1 deletions
+2 -1
View File
@@ -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{}
+10
View File
@@ -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
}
@@ -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)
}
}