diff --git a/internal/filememo/filememo.go b/internal/filememo/filememo.go index 6449e18e3..9c9a12f8d 100644 --- a/internal/filememo/filememo.go +++ b/internal/filememo/filememo.go @@ -44,8 +44,23 @@ var ( // on again (steady.Stat): one there before and not now may be being // replaced (Windows), and is gone = map[string]bool{} + // missing is the files found not there while a request held what was + // read, when, and in which of Forget's generations: taken as still + // not there as a file found unchanged is, not looked at again. A + // provider with no list fetched has no file of one, and a request + // asked after it for each of its models, each time a look at the disk + // (a third of a request's CPU with 30 providers of 60 models). + missing = map[string]missingFile{} + // stats counts the looks at the disk, for tests + stats int ) +type missingFile struct { + err error + checked time.Time + gen uint64 +} + // heldFor is how long a file found unchanged is taken as unchanged, while // a request holds what was read, before it is looked at again. const heldFor = time.Second @@ -86,6 +101,7 @@ func Look(path string) { seen[k] = e } } + delete(missing, path) } // Read is parse of the file at path, from what it gave last time when the @@ -101,10 +117,21 @@ func Read[T any](kind, path string, parse func([]byte) (T, error)) (T, error) { mu.Unlock() return e.v.(T), nil } + if m, ok := missing[path]; ok && holds > 0 && m.gen == gen && time.Since(m.checked) < heldFor { + mu.Unlock() + return zero, m.err + } g := gen mu.Unlock() fi, err := statFile(path) if err != nil { + if errors.Is(err, fs.ErrNotExist) { + mu.Lock() + if holds > 0 && g == gen { + missing[path] = missingFile{err, time.Now(), g} + } + mu.Unlock() + } return zero, err } if ok && e.mod.Equal(fi.ModTime()) && e.size == fi.Size() && e.raw == nil { @@ -153,6 +180,7 @@ func Read[T any](kind, path string, parse func([]byte) (T, error)) (T, error) { func statFile(path string) (os.FileInfo, error) { mu.Lock() wasGone := gone[path] + stats++ mu.Unlock() var fi os.FileInfo var err error @@ -166,6 +194,7 @@ func statFile(path string) (os.FileInfo, error) { gone[path] = true } else { delete(gone, path) + delete(missing, path) } mu.Unlock() return fi, err diff --git a/internal/filememo/filememo_test.go b/internal/filememo/filememo_test.go index aee28c79a..f539abf41 100644 --- a/internal/filememo/filememo_test.go +++ b/internal/filememo/filememo_test.go @@ -96,3 +96,58 @@ func TestHold(t *testing.T) { t.Fatalf("a file written just now: %q", v) } } + +// A file not there is, while a request holds what was read, taken as +// still not there as a file found unchanged is: a provider with no list +// fetched was a look at the disk for each of its models, each request. A +// write magpie makes (Forget), another's (Look), or the hold's end has it +// looked at again; unheld, every read looks. +func TestHoldMissing(t *testing.T) { + p := filepath.Join(t.TempDir(), "none") + parse := func(b []byte) (string, error) { return string(b), nil } + looks := func() int { + mu.Lock() + defer mu.Unlock() + return stats + } + release := Hold() + before := looks() + for range 100 { + if _, err := Read("missing", p, parse); !os.IsNotExist(err) { + t.Fatalf("a file not there: %v", err) + } + } + if n := looks() - before; n != 1 { + t.Fatalf("%d looks at a file not there, held", n) + } + os.WriteFile(p, []byte("a"), 0o600) + if _, err := Read("missing", p, parse); !os.IsNotExist(err) { + t.Fatalf("looked at again while held: %v", err) + } + Forget() + if v, err := Read("missing", p, parse); v != "a" || err != nil { + t.Fatalf("after a write magpie made: %q %v", v, err) + } + os.Remove(p) + Read("missing", p, parse) + os.WriteFile(p, []byte("b"), 0o600) + Look(p) + if v, err := Read("missing", p, parse); v != "b" || err != nil { + t.Fatalf("after another's write: %q %v", v, err) + } + os.Remove(p) + Read("missing", p, parse) + release() + os.WriteFile(p, []byte("c"), 0o600) + if v, err := Read("missing", p, parse); v != "c" || err != nil { + t.Fatalf("released: %q %v", v, err) + } + os.Remove(p) + before = looks() + for range 3 { + Read("missing", p, parse) + } + if n := looks() - before; n != 3 { + t.Fatalf("unheld, %d looks for 3 reads", n) + } +}