An idle magpie still woke a dozen times on each 5s beat: the loops that follow plugins.yml, settings.json, providers.json, the plugin list, tools and access each looked at the disk for what the CLI or a second magpie wrote. The gateway now watches those files' folders (fsnotify: FSEvents, inotify, ReadDirectoryChangesW), and a change from another process wakes the same loops at once, as magpie's own writes do (filememo.Written). With the watch running the loops look for a change they missed only once a minute (OthersEvery); where the folders can't be watched, or the watch errs, they go on looking every 5s. Run at466cafa0c+ this change. The rebase onto13f9c7fcabrought in internal/gateway's claude bridge and redact files, none of the files here; after it, go build ./... and the filememo, pluginrt, awake tests ran again ok: - go test -timeout 45m ./internal/filememo ./internal/pluginrt ./internal/middleware ./internal/otlp ./internal/gateway: all ok (gateway 384s). - TestWatchWakesOnOthersWrites -count=3 ok; red with wake() taken out. - gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on the changed packages ok. - Real gateway, sandbox HOME, idle: proc_pid_rusage wakeups over 120s after a 90s warm-up, two rounds each: v0.1.1561 2.1 / 2.7 per second, this 1.9 / 2.0 (and 1.8 in a third run). - Same gateway: plugins.yml replaced from outside with broken YAML; the gateway logged "plugins: ... doesn't parse" 0.02s after the write (it could take up to 5s before). File restored after. Not tried: Linux and Windows real runs (vet only).
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package filememo
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// What another process writes to a followed file wakes the loops waiting
|
|
// on Written at once, written whole (a new file put in its place), in
|
|
// place, or made where there was none; and while that holds the loops
|
|
// look for it themselves only now and then (#3185).
|
|
func TestWatchWakesOnOthersWrites(t *testing.T) {
|
|
dir := t.TempDir()
|
|
whole, inPlace, made := filepath.Join(dir, "settings.json"), filepath.Join(dir, "providers.json"), filepath.Join(dir, "plugins.yml")
|
|
os.WriteFile(whole, []byte("{}"), 0o600)
|
|
os.WriteFile(inPlace, []byte("{}"), 0o600)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() { Watch(ctx, whole, inPlace, made); close(done) }()
|
|
defer func() { cancel(); <-done }()
|
|
for deadline := time.Now().Add(10 * time.Second); OthersEvery() != othersWatched; time.Sleep(5 * time.Millisecond) {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the watch never started")
|
|
}
|
|
}
|
|
woken := func(name string, write func()) {
|
|
t.Helper()
|
|
w := Written()
|
|
write()
|
|
select {
|
|
case <-w:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatalf("%s written by another process woke nothing", name)
|
|
}
|
|
}
|
|
woken("a file written whole", func() {
|
|
tmp := whole + ".tmp"
|
|
os.WriteFile(tmp, []byte(`{"a":1}`), 0o600)
|
|
os.Rename(tmp, whole)
|
|
})
|
|
woken("a file written in place", func() {
|
|
f, _ := os.OpenFile(inPlace, os.O_WRONLY|os.O_APPEND, 0)
|
|
f.WriteString(" ")
|
|
f.Close()
|
|
})
|
|
woken("a file made", func() { os.WriteFile(made, []byte("on: []"), 0o600) })
|
|
cancel()
|
|
<-done
|
|
if OthersEvery() != othersPolled {
|
|
t.Fatalf("with the watch ended the loops look every %v, want %v", OthersEvery(), othersPolled)
|
|
}
|
|
}
|