idle: the loops following magpie's files wake on its own writes, not a 1-2s poll (#3185)

An idle `magpie serve` (no providers, no requests) woke 12-13 times a
second. A runtime trace showed the wakers were polling loops: the
middleware observer's refresh every 1s, pluginrt.KeepFresh's stat of five
files every 2s, OTel's session watch every 2s and its exporter tick every
5s, all with nothing to do.

Now filememo.Forget, which every write magpie makes of settings,
providers, sign-ins and caller keys already calls, also closes a channel
(filememo.Written). The plugins.json, tools and plugins.yml saves call it
too. The middleware observer, KeepFresh and OTel's session watch wait on
it, and look every filememo.OthersEvery (5s) only for what another
process (the CLI, a second magpie) wrote. Each takes the channel before
it looks, so a write made during a look isn't missed. middleware's
current() also reloads at once when filememo's generation moved, rather
than after its 1s skip. OTel's session watch waits 5s, not 2s, while
sessions aren't exported, and its exporter's timer runs only while a
batch holds something.

Measured: sandbox HOME, MAGPIE_ADDR=127.0.0.1:3481, `magpie serve`,
proc_pid_rusage (idle + interrupt wakeups) over 30s after 40s warm-up,
old (cec54c1d0) and new builds alternated:
  old  13.2 / 13.3 / 12.2 wakeups/s, 0.40 / 0.35 / 0.42 ms CPU/s
  new   7.9 /  8.6 wakeups/s,         0.22 / 0.30 ms CPU/s
What's left is the Go runtime itself, awake.Keep's 5s tick and the 5s
looks for other processes' writes.

Tests: TestKeepFreshFollowsPluginsYml now also runs KeepFresh with a 1h
interval and needs Set to apply at once; TestStrategiesFollowOwnWrite
needs a plugin turned off through plugin.SetOff to stop being served
within 2s, with no refresh by hand; TestWritten. With the Written cases
replaced by a nil channel, both of the first two went red (applied = 1,
want 0; still served 2s after). Run at this head: go test -count=1
-timeout 45m ./internal/filememo ./internal/plugin ./internal/pluginrt
./internal/middleware ./internal/otlp ./internal/settings
./internal/access: all ok. GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/...: clean. gofmt -l internal: clean.

Not run: the full suite and internal/gateway's tests (gateway.go only
changes KeepFresh's interval argument, and it compiles under vet); the
macOS app with its window open (only headless serve was measured).
This commit is contained in:
yetone
2026-10-25 23:45:34 +08:00
parent 67271e7020
commit 091153bf71
12 changed files with 148 additions and 14 deletions
+20
View File
@@ -88,9 +88,29 @@ func Forget() {
mu.Lock()
gen++
gens.Store(gen)
close(written)
written = make(chan struct{})
mu.Unlock()
}
// written is closed, and made anew, by every Forget.
var written = make(chan struct{})
// Written is closed by the next write magpie makes (Forget): a loop that
// follows magpie's files waits on it, and looks at them only every so
// often for what another process wrote, rather than polling each second
// for its own writes too (#3185: those polls woke an idle magpie ~12
// times a second).
func Written() <-chan struct{} {
mu.Lock()
defer mu.Unlock()
return written
}
// OthersEvery is how often such a loop looks for what another process
// (the CLI, a second magpie) wrote.
const OthersEvery = 5 * time.Second
// gens is gen, read without mu.
var gens atomic.Uint64
+22
View File
@@ -151,3 +151,25 @@ func TestHoldMissing(t *testing.T) {
t.Fatalf("unheld, %d looks for 3 reads", n)
}
}
// Written is closed by the next Forget, and a wait begun after it waits for
// the one after.
func TestWritten(t *testing.T) {
w := Written()
select {
case <-w:
t.Fatal("closed before any write")
default:
}
Forget()
select {
case <-w:
case <-time.After(5 * time.Second):
t.Fatal("not closed by Forget")
}
select {
case <-Written():
t.Fatal("the next wait is closed already")
default:
}
}
+2 -1
View File
@@ -32,6 +32,7 @@ import (
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/awake"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/gwbudget"
"github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/gwtool"
@@ -613,7 +614,7 @@ func (s *Server) ListenAndServe(ctx context.Context) error {
defer stopLimits()
freshCtx, stopFresh := context.WithCancel(ctx)
defer stopFresh()
go pluginrt.KeepFresh(freshCtx, 2*time.Second, settings.Path(), provider.Path(), plugin.ListPath(), plugin.ToolsPath(), access.Path())
go pluginrt.KeepFresh(freshCtx, filememo.OthersEvery, settings.Path(), provider.Path(), plugin.ListPath(), plugin.ToolsPath(), access.Path())
srv := &http.Server{Handler: s.Front(), ReadHeaderTimeout: 30 * time.Second, IdleTimeout: 5 * time.Minute}
// the magpie serving the gateway, and only it, keeps the saved accounts
// signed in, so two never refresh one sign-in at once
+8 -2
View File
@@ -25,6 +25,7 @@ import (
"time"
"github.com/Calcium-Ion/moejs"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/jsmod"
"github.com/yetone/magpie/internal/plugin"
)
@@ -84,17 +85,22 @@ var (
loadMu sync.Mutex
cur atomic.Pointer[set]
nextCheck atomic.Int64
// checkedGen is filememo's generation when current last looked
checkedGen atomic.Uint64
)
// current is the middleware in force, loaded again when plugins.json or a
// middleware's file changed, which is looked at once a second.
// middleware's file changed, which is looked at once a second, and at
// once after magpie wrote a file of its own.
func current() *set {
s := cur.Load()
now := time.Now().UnixNano()
n := nextCheck.Load()
if s != nil && (now < n || !nextCheck.CompareAndSwap(n, now+int64(time.Second))) {
g := filememo.Gen()
if s != nil && g == checkedGen.Load() && (now < n || !nextCheck.CompareAndSwap(n, now+int64(time.Second))) {
return s
}
checkedGen.Store(g)
st := stamp()
if s != nil && s.stamp == st {
return s
+9 -2
View File
@@ -35,6 +35,7 @@ import (
"github.com/Calcium-Ion/moejs"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/routes"
"github.com/yetone/magpie/internal/usage"
@@ -404,19 +405,25 @@ func (o *observer) Serve() (stop func()) {
o.mu.Lock()
o.queue = q
o.mu.Unlock()
written := filememo.Written() // before the look, so a write made during it isn't missed
o.refresh()
wg.Add(2)
go func() {
defer wg.Done()
t := time.NewTicker(time.Second)
// a plugin turned on here is seen once saved (Written); every
// OthersEvery only catches one the CLI or a second magpie turned on
// (#3185: a 1s tick woke an idle magpie every second)
t := time.NewTicker(filememo.OthersEvery)
defer t.Stop()
for {
select {
case <-done:
return
case <-written:
case <-t.C:
o.refresh()
}
written = filememo.Written()
o.refresh()
}
}()
go func() {
+21
View File
@@ -119,6 +119,27 @@ func TestStrategiesServedWhileOn(t *testing.T) {
}
}
// A module turned off here (the Plugins page) is no longer served at once,
// without waiting for the look for other processes' writes, which is
// seconds apart (#3185).
func TestStrategiesFollowOwnWrite(t *testing.T) {
e := file(t, `export const strategies = { a: (p, s) => s.map((_, i) => i) };`)
use(t, e)
serve(t)
if !routes.Served("x/a") {
t.Fatal("not served")
}
if err := plugin.SetOff(plugin.Name(e.Spec), true); err != nil {
t.Fatal(err)
}
for end := time.Now().Add(2 * time.Second); routes.Served("x/a"); {
if time.Now().After(end) {
t.Fatal("still served 2s after it was turned off here")
}
time.Sleep(5 * time.Millisecond)
}
}
// Not served (plugins/middleware off), nothing is registered.
func TestStrategiesStopWithServe(t *testing.T) {
use(t, file(t, `export const strategies = { a: (p, s) => s.map((_, i) => i) };`))
+14 -4
View File
@@ -217,10 +217,20 @@ func (e *otelExporter) run(every time.Duration) {
defer close(e.done)
defer e.cancel()
defer e.client.CloseIdleConnections()
ticker := time.NewTicker(every)
defer ticker.Stop()
// the timer runs only while the batch holds something, so an idle
// exporter doesn't wake every few seconds (#3185)
timer := time.NewTimer(every)
timer.Stop()
defer timer.Stop()
batch := make([]otelItem, 0, otelBatchSize)
add := func(it otelItem) {
if len(batch) == 0 {
timer.Reset(every)
}
batch = append(batch, it)
}
flush := func() {
timer.Stop()
e.flush(batch)
for _, it := range batch {
if n := int64(recordBytes(it.record)); n > 0 {
@@ -232,11 +242,11 @@ func (e *otelExporter) run(every time.Duration) {
for {
select {
case it := <-e.queue:
batch = append(batch, it)
add(it)
if len(batch) == otelBatchSize {
flush()
}
case <-ticker.C:
case <-timer.C:
flush()
case <-e.stop:
for {
+16
View File
@@ -5,6 +5,7 @@ import (
"reflect"
"time"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/redact"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
@@ -113,23 +114,38 @@ func (e *otelExporter) watchSessions(ctx context.Context, started time.Time, eve
defer ticker.Stop()
var reader *sessions.TraceReader
var prior settings.OTel
written := filememo.Written() // before each look, so a save during it isn't missed
if config, err := settings.OTelExport(); err == nil && config.Enabled && config.Sessions {
reader = sessions.NewTraceReader(started)
reader.SetSessionIndex(&e.identities)
prior = config
}
// while sessions aren't exported, a save here turns them on at once
// (Written), and OthersEvery only catches the CLI's (#3185: a 2s tick
// woke an idle magpie that exports nothing)
wait := every
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
case <-written:
}
written = filememo.Written()
config, err := settings.OTelExport()
if err != nil || !config.Enabled || !config.Sessions {
reader = nil
e.sessions.Store(nil)
if wait != filememo.OthersEvery {
wait = filememo.OthersEvery
ticker.Reset(wait)
}
continue
}
if wait != every {
wait = every
ticker.Reset(wait)
}
if reader == nil || !reflect.DeepEqual(config, prior) {
reader = sessions.NewTraceReader(time.Now())
reader.SetSessionIndex(&e.identities)
+2
View File
@@ -34,6 +34,7 @@ import (
"sync/atomic"
"time"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/lastgood"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/proc"
@@ -228,6 +229,7 @@ func save(l List) error {
if err := lastgood.Keep(listPath(), lastgood.JSON, 0o600); err != nil {
return err
}
defer filememo.Forget() // what follows plugins.json looks again (Written)
if err := writeWhole(listPath(), append(b, '\n')); err != nil {
return err
}
+2
View File
@@ -13,6 +13,7 @@ import (
"sync"
"time"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/steady"
)
@@ -107,6 +108,7 @@ func noteTools(loaded []Loaded) {
if len(m) == 0 && errors.Is(err, fs.ErrNotExist) {
return
}
defer filememo.Forget() // the gateway follows this note (Written)
if err := writeWhole(toolsPath(), append(b, '\n')); err != nil {
log.Printf("plugin tools: noting them: %v", err)
}
+7
View File
@@ -17,6 +17,7 @@ import (
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/filelock"
"github.com/yetone/magpie/internal/filememo"
)
// plugins.yml says which built-in plugins are on, and each one's config:
@@ -292,6 +293,7 @@ func write(f File) error {
}
b = append([]byte(header), b...)
path := Path()
defer filememo.Forget() // KeepFresh looks again (Written)
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
@@ -374,16 +376,21 @@ func KeepFresh(ctx context.Context, every time.Duration, also ...string) {
tick := time.NewTicker(every)
defer tick.Stop()
for {
// taken before the look, so a write made during it isn't missed
written := filememo.Written()
if now := stamp(); now != last {
last = now
if _, err := Refresh(); err != nil {
log.Printf("plugins: %v", err)
}
}
// a write of this process's is looked at once made; every only
// catches another process's
select {
case <-ctx.Done():
return
case <-tick.C:
case <-written:
}
}
}
+25 -5
View File
@@ -167,7 +167,8 @@ func TestBrokenFileIsKept(t *testing.T) {
}
// A plugin turned off or on in plugins.yml by another process is disposed
// or applied in the one keeping fresh, without a restart.
// or applied in the one keeping fresh, without a restart; turned off or on
// in that process, at once.
func TestKeepFreshFollowsPluginsYml(t *testing.T) {
home(t)
var applied atomic.Int32
@@ -176,10 +177,14 @@ func TestKeepFreshFollowsPluginsYml(t *testing.T) {
c.OnDispose(func() { applied.Add(-1) })
return nil
}})
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { KeepFresh(ctx, 10*time.Millisecond); close(done) }()
defer func() { cancel(); <-done; Set("fresh-test", false); Refresh() }()
keep := func(every time.Duration) (stop func()) {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { KeepFresh(ctx, every); close(done) }()
return func() { cancel(); <-done }
}
stop := keep(10 * time.Millisecond)
defer func() { stop(); Set("fresh-test", false); Refresh() }()
wait := func(want int32) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
@@ -199,4 +204,19 @@ func TestKeepFreshFollowsPluginsYml(t *testing.T) {
t.Fatal(err)
}
wait(1)
// turned off or on in this process, it follows at once, not on the
// next look for another process's writes (#3185: those looks woke an
// idle magpie, so they are seconds apart now)
stop()
stop = keep(time.Hour)
wait(1)
if err := Set("fresh-test", false); err != nil {
t.Fatal(err)
}
wait(0)
if err := Set("fresh-test", true); err != nil {
t.Fatal(err)
}
wait(1)
}