An idle magpie serve still woke ~6 times a second with nothing to do. Its loops each kept a ticker of their own, started at their own instant: awake.Keep (5s), the plugin config's KeepFresh (5s), the middleware observer (5s), OTel's session watch (5s idle), the Codex daemon check (15s), dsh's wiring (30s), the model lists' look (1m). Every one woke the process apart, a few threads each time. internal/beat's Next(every) fires at the next multiple of every on the clock, so the 5s loops come due at one instant, the 15s and 30s ones on every third and sixth of it, and the runtime runs timers due together in one wakeup. Each loop now waits on beat.Next instead of its ticker. No channel is shared between callers (a synctest bubble's channel reached from another crashes the process, #3632). Run at this head (05ea29ac8+ this): - Sandbox HOME serve, idle, 90s warm-up then 120s of proc_pid_rusage (pkg_idle_wkups + interrupt_wkups), old (origin/main19253f67b) and new taking turns: old 6.1, 5.3, 6.2 wakeups/s; new 2.7, 2.6 (an earlier shared-timer form of beat), 3.0 (this one). CPU 0.20-0.36 ms/s old, 0.11-0.22 new. - TestIntervalsMeet (5s/15s/30s/1m beats from 09:00:03.2 and on a beat), TestNextFires; go test ./internal/beat ./internal/awake ./internal/middleware ./internal/otlp -count=3, ./internal/pluginrt, ./internal/agent -run 'Dsh|CodexDaemon', ./internal/provider -skip TestUnreadLoginsWriteErrorsKeepCredentials (#3632): ok. ./internal/pluginrt -count=3 fails TestKeepFreshFollowsPluginsYml ("two plugins fresh-test") on origin/main too; -count=1 is ok. - gofmt; go vet darwin, GOOS=linux and GOOS=windows -tags nogui. Not tried: a GUI build's wakeups (the tray process has AppKit's own).
22 lines
795 B
Go
22 lines
795 B
Go
// Package beat wakes the loops that look at something every so often at
|
|
// one time: each waits for the next multiple of its interval on the clock,
|
|
// so a 5s loop, a 15s one and a 30s one wake together. An idle magpie woke
|
|
// apart for each of them, a few threads every time (#3185). Timers due at
|
|
// one instant are run by one wakeup, so the loops need no timer in common.
|
|
package beat
|
|
|
|
import "time"
|
|
|
|
// Next fires at the next multiple of every on the clock, after now.
|
|
func Next(every time.Duration) <-chan time.Time {
|
|
if every <= 0 {
|
|
every = time.Millisecond
|
|
}
|
|
return time.After(time.Until(after(time.Now(), every)))
|
|
}
|
|
|
|
// after is the first multiple of every on the clock after now.
|
|
func after(now time.Time, every time.Duration) time.Time {
|
|
return now.Truncate(every).Add(every)
|
|
}
|