ClaudeHomesIn and CodexHomesIn run on every Sessions poll, and keyed each folder they found by filepath.EvalSymlinks, which Lstats every part of the name: ~3 MB of garbage per 40 polls on a real home. On unix a folder is now keyed by its device and inode, which a link to it shares; a folder that can't be looked at is keyed by its name, and other systems keep EvalSymlinks. The folder checks use DirStat, moved here from internal/sessions so both share one. Run (mica, at this tree): go vet for darwin and windows of appdir, sessions, provider and gui, for linux of the first three (gui needs cgo there); GOOS=freebsd go build; go test ./internal/appdir ./internal/sessions ./internal/provider ok. TestHomesLookAllocatesLittle: 55 and 68 allocations a look now, 387 and 400 on the old code, which fails it. Real account (mote, 40 polls of /api/sessions): w9 31.28 MB allocated with os.lstatNolog 3 MB; w10 25.98 MB, lstat gone from the top list. CPU over 30s 1.47s, within noise of w8's 1.29s at mote's load.
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package sessions
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// Directory membership changes with the directory's mtime; file contents don't.
|
|
// Keep names only, and always stat candidate files for current size/mtime. This
|
|
// avoids rereading every Cowork project directory at each five-second refresh.
|
|
var directoryCache = struct {
|
|
sync.Mutex
|
|
entries map[string]directoryEntry
|
|
}{entries: map[string]directoryEntry{}}
|
|
|
|
type directoryEntry struct {
|
|
mod time.Time
|
|
size int64
|
|
at time.Time // when it was listed
|
|
entries []os.DirEntry
|
|
}
|
|
|
|
// racyDirectory is how close to a directory's mtime a listing can't be
|
|
// trusted: mtimes are coarse (a Linux kernel tick, a second on some
|
|
// filesystems) and a directory's size often doesn't change, so an entry
|
|
// added in the same tick as a listing left the mtime as it was and stayed
|
|
// hidden. Such a listing is read again until it's older than this.
|
|
const racyDirectory = 2 * time.Second
|
|
|
|
// readDirectory is path's entries. A folder that isn't there lists as none.
|
|
// One that couldn't be read (a stat or listing refused: too many files open
|
|
// under a long history's parse, a sharing violation while an antivirus or
|
|
// a sync client holds it, a network home that stalled) is unknown, not
|
|
// empty: its last good listing stands. Taken for none, every session in it
|
|
// left the Sessions page, and its parses were let go with them, until a
|
|
// later poll read the folder again and parsed them anew.
|
|
func readDirectory(path string) []os.DirEntry {
|
|
mod, size, dir, err := appdir.DirStat(path)
|
|
if err != nil {
|
|
return unreadDirectory(path, err)
|
|
}
|
|
if !dir {
|
|
return nil
|
|
}
|
|
directoryCache.Lock()
|
|
old, ok := directoryCache.entries[path]
|
|
directoryCache.Unlock()
|
|
if ok && old.mod.Equal(mod) && old.size == size && old.at.Sub(old.mod) > racyDirectory {
|
|
return old.entries
|
|
}
|
|
at := time.Now()
|
|
entries, err := os.ReadDir(path)
|
|
if err != nil {
|
|
if kept := unreadDirectory(path, err); kept != nil {
|
|
return kept
|
|
}
|
|
return nil
|
|
}
|
|
directoryCache.Lock()
|
|
if len(directoryCache.entries) >= 16384 {
|
|
directoryCache.entries = map[string]directoryEntry{}
|
|
}
|
|
directoryCache.entries[path] = directoryEntry{mod, size, at, entries}
|
|
directoryCache.Unlock()
|
|
return entries
|
|
}
|
|
|
|
// unreadDirectory is what a folder lists as when reading it failed with
|
|
// err: nothing when it is gone, else the last good listing, if there is
|
|
// one.
|
|
func unreadDirectory(path string, err error) []os.DirEntry {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
directoryCache.Lock()
|
|
delete(directoryCache.entries, path)
|
|
directoryCache.Unlock()
|
|
return nil
|
|
}
|
|
directoryCache.Lock()
|
|
defer directoryCache.Unlock()
|
|
return directoryCache.entries[path].entries
|
|
}
|
|
|
|
// SessionGlob is filepath.Glob for session discovery, with directory listings
|
|
// cached by metadata. It never caches file contents or suppresses new entries.
|
|
func SessionGlob(pattern string) ([]string, error) {
|
|
if _, err := filepath.Match(pattern, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
if !strings.ContainsAny(pattern, "*?[") {
|
|
if _, err := os.Lstat(pattern); err != nil {
|
|
return nil, nil
|
|
}
|
|
return []string{pattern}, nil
|
|
}
|
|
dir, name := filepath.Split(pattern)
|
|
dir = filepath.Clean(dir)
|
|
parents, err := SessionGlob(dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []string
|
|
for _, p := range parents {
|
|
for _, e := range readDirectory(p) {
|
|
if yes, _ := filepath.Match(name, e.Name()); yes {
|
|
out = append(out, filepath.Join(p, e.Name()))
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|