sessions: the stats caches are let go two minutes after the page stops asking

The Sessions page polls /api/sessions/stats every 15 s. Each range it asked
for kept its Stats, the overview and tag sums, about 15 MB on a 14k-session
history, for as long as magpie ran, with the window closed too. Now a timer
armed on every ask drops them two minutes after the last one (not while a
read is busy), with the sessions package's kept ranges, then returns the
memory to the OS. The parsed files stay, so asking again sums them up
again without reading a file.

Measured on mote with a copy of the real accounts (14k sessions), the
statsIdle delay set to 20 s in a measuring build only:
- footprint after six stats asks: t47 (origin/main) 140 MB, t48 151 MB;
- 45 s later: t47 130 MB, t48 116 MB;
- asking again after the release: days=0 0.34 s (was 0.06 s while kept),
  days=30 0.17 s.

Run on mote: go vet ./internal/gui ./internal/sessions; go test
./internal/gui ./internal/sessions ok. TestIdleStatsLetGo is red with
`statsMemo.m = nil` taken out ("2 ranges ... kept; want none").
The timer is off under go test, so other gui tests keep their memos.
Not tried: the GUI page itself; the full suite.
This commit is contained in:
yetone
2026-11-10 13:58:41 +08:00
parent bb4dd86790
commit 53280aa08e
3 changed files with 157 additions and 0 deletions
+57
View File
@@ -704,6 +704,7 @@ func statsFor(days int) sessions.Stats {
func statsForGen(days int) (sessions.Stats, uint64) {
days = max(0, days)
keepStatsWhileAsked()
statsMemo.Lock()
if k := statsMemo.m[days]; k != nil {
statsMemo.asks++
@@ -764,6 +765,62 @@ func statsForGen(days int) (sessions.Stats, uint64) {
return st, gen
}
// statsIdle is how long the Sessions page's stats are kept after the last
// ask. The page polls every 15 s while it is open; once it has been shut
// for this long, every range's Stats (a Summary for each session ever:
// ~15 MB of a 14k-session history), the overviews and the answers' tags
// go. The parsed session files stay, so the next ask sums them up again
// (~0.1 s a range) without reading a file.
const statsIdle = 2 * time.Minute
var statsIdleTimer struct {
sync.Mutex
t *time.Timer
// off: tests, which ask for stats over minutes, have it armed only
// when they ask for it
off bool
}
func init() { statsIdleTimer.off = testing.Testing() }
// keepStatsWhileAsked puts off forgetIdleStats for statsIdle.
func keepStatsWhileAsked() {
statsIdleTimer.Lock()
defer statsIdleTimer.Unlock()
if statsIdleTimer.off {
return
}
if statsIdleTimer.t != nil {
statsIdleTimer.t.Reset(statsIdle)
return
}
statsIdleTimer.t = time.AfterFunc(statsIdle, forgetIdleStats)
}
// forgetIdleStats lets go of what statsForGen and writeOverview kept, and
// hands the pages back to the system at once; a range being read again
// puts it off.
func forgetIdleStats() {
statsMemo.Lock()
for _, k := range statsMemo.m {
if k.busy {
statsMemo.Unlock()
keepStatsWhileAsked()
return
}
}
statsMemo.m = nil
statsMemo.Unlock()
overviews.Lock()
overviews.m = nil
overviews.Unlock()
statsTags.Lock()
statsTags.m = nil
statsTags.Unlock()
sessions.ForgetKeptRanges()
debug.FreeOSMemory()
}
func combinedSessionStats(days int) sessions.Stats {
return combinedSessionStatsAll([]int{days})[0]
}
+90
View File
@@ -0,0 +1,90 @@
package gui
import (
"net/http/httptest"
"testing"
)
// Once the Sessions page has gone unasked for statsIdle, every range's
// Stats, the overviews and the answers' tags are let go: a long history's
// were ~15 MB held for good by a window that showed another page, or none.
// The next ask is answered as before, from a new read.
func TestIdleStatsLetGo(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
statsIdleTimer.Lock()
statsIdleTimer.off = false
statsIdleTimer.Unlock()
t.Cleanup(func() {
statsIdleTimer.Lock()
if statsIdleTimer.t != nil {
statsIdleTimer.t.Stop()
statsIdleTimer.t = nil
}
statsIdleTimer.off = true
statsIdleTimer.Unlock()
})
before, gen := statsForGen(0)
statsFor(30)
writeOverview(httptest.NewRecorder(), 0, "", "", "")
overviews.Lock()
had := len(overviews.m)
overviews.Unlock()
if had == 0 {
t.Fatal("the overview asked for was not kept")
}
statsIdleTimer.Lock()
armed := statsIdleTimer.t != nil
statsIdleTimer.Unlock()
if !armed {
t.Fatal("an ask for stats armed no idle timer")
}
forgetIdleStats()
statsMemo.Lock()
n := len(statsMemo.m)
statsMemo.Unlock()
overviews.Lock()
o := len(overviews.m)
overviews.Unlock()
statsTags.Lock()
tags := len(statsTags.m)
statsTags.Unlock()
if n != 0 || o != 0 || tags != 0 {
t.Fatalf("after the idle timer: %d ranges, %d overviews, %d tags kept; want none", n, o, tags)
}
after, gen2 := statsForGen(0)
if gen2 == gen {
t.Fatal("the range asked after the idle timer was answered from what it let go")
}
if !sameStats(before, after) {
t.Fatal("the range read again differs from the one let go")
}
}
// A range being read again behind the page is not let go under it: the
// read would put back a range the memo no longer has.
func TestIdleStatsWaitForABusyRead(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
statsFor(0)
statsMemo.Lock()
statsMemo.m[0].busy = true
statsMemo.Unlock()
t.Cleanup(func() {
statsMemo.Lock()
if k := statsMemo.m[0]; k != nil {
k.busy = false
}
statsMemo.Unlock()
})
forgetIdleStats()
statsMemo.Lock()
kept := statsMemo.m[0] != nil
statsMemo.Unlock()
if !kept {
t.Fatal("a range being read again was let go")
}
}
+10
View File
@@ -204,3 +204,13 @@ func forgetKeptStats() {
keptStats.files, keptStats.ranges, keptStats.native = nil, nil, nil
keptStats.prices, keptStats.titles, keptStats.desktop = nil, titleAnswers{}, nil
}
// ForgetKeptRanges lets go of the ranges' Stats kept for the next read,
// and what they were checked against, once no page has asked for a while.
// The parsed files stay: a page that asks again sums them up again
// (~0.1 s for a long history) and reads no file.
func ForgetKeptRanges() {
mu.Lock()
defer mu.Unlock()
forgetKeptStats()
}