diff --git a/internal/gui/api.go b/internal/gui/api.go index e12dff2ed..123c0658d 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -1135,7 +1135,13 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { webPage.Store(true) } mux := http.NewServeMux() - mux.Handle("/", devPage(revalidated(http.FileServer(http.FS(staticFS()))))) + page := devPage(revalidated(http.FileServer(http.FS(staticFS())))) + mux.Handle("/", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if warmsSessions(r) { + warmSessions() + } + page.ServeHTTP(rw, r) + })) devRoutes(mux) pluginPageRoutes(mux, isWeb(w)) feedbackRoutes(mux) diff --git a/internal/gui/lightweight_rule_test.go b/internal/gui/lightweight_rule_test.go index 52bf38561..a9ea47a07 100644 --- a/internal/gui/lightweight_rule_test.go +++ b/internal/gui/lightweight_rule_test.go @@ -1,6 +1,9 @@ package gui -import "testing" +import ( + "net/http/httptest" + "testing" +) // A closed window's webview goes on the second look in a row that finds it // closed with lightweight mode on (#580), never while it is shown or the @@ -56,3 +59,24 @@ func TestMainOnFirstOpen(t *testing.T) { } } } + +// The warm-up's read of the history starts with the app's page, never +// with the tray panel's page or a file the page loads. +func TestWarmsSessions(t *testing.T) { + for _, c := range []struct { + method, url string + want bool + }{ + {"GET", "/?theme=dark", true}, + {"GET", "/", true}, + {"GET", "/index.html", true}, + {"GET", "/?mode=panel&theme=dark", false}, + {"GET", "/app.js", false}, + {"HEAD", "/", false}, + } { + r := httptest.NewRequest(c.method, c.url, nil) + if got := warmsSessions(r); got != c.want { + t.Errorf("%s %s: %v, want %v", c.method, c.url, got, c.want) + } + } +} diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index a26df3bbe..c0f2422ce 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -112,7 +112,7 @@ func gatewaySession(g usage.GatewaySession) sessions.Session { } func sessionRoutes(mux *http.ServeMux, w Windows) { - warmSessions() + steadyGC() mux.HandleFunc("GET /api/sessions", coldTurn(func(rw http.ResponseWriter, r *http.Request) { // the list and the Codex threads' effort each list every session file defer sessions.OneListing()() @@ -912,12 +912,18 @@ func withGatewaySessions(st sessions.Stats, days int, nativeKeys map[string]bool return sessions.MergeExternalStats(st, daily, ext) } -// warmSessions reads every session file once magpie is up, so the Sessions -// page opens on the kept index and not on a first read of them all. Usage › -// Requests' whole history is read after it, not beside it on the disk: a -// first All parsing 12k sessions kept that page a skeleton for 40 s. -// Once a process: the app's page served to a browser (apppage.go) is a -// second Handler beside the window's. +// warmSessions reads every session file once the app's page is first +// served, so the Sessions page opens on the kept index and not on a first +// read of them all. Usage › Requests' whole history is read after it, not +// beside it on the disk: a first All parsing 12k sessions kept that page a +// skeleton for 40 s. Once a process: the app's page served to a browser +// (apppage.go) is a second Handler beside the window's. +// +// Not at start: a magpie started at login, by `magpie tray` or as `magpie +// web` with no page open read the history for nobody, ~0.6 s of CPU and a +// rebuilt Requests index after an update, and what the reads kept went +// two minutes later (statsIdle, requestIdle). The tray panel's page +// doesn't start it: it shows neither page. func warmSessions() { if testing.Testing() { return @@ -925,10 +931,24 @@ func warmSessions() { warmOnce.Do(warmStart) } -var warmOnce sync.Once +var warmOnce, steadyOnce sync.Once + +// steadyGC is steadyGCStart, once a process and never in a test. +func steadyGC() { + if testing.Testing() { + return + } + steadyOnce.Do(steadyGCStart) +} + +// warmsSessions tells the app's page from the panel's and from the files +// it loads: the page that starts warmSessions. +func warmsSessions(r *http.Request) bool { + return r.Method == http.MethodGet && (r.URL.Path == "/" || r.URL.Path == "/index.html") && r.URL.Query().Get("mode") != "panel" +} func warmStart() { - steadyGCStart() + steadyGC() go func() { // a fresh install reads no agent's sessions until asked; one // that read them before keeps reading them (decided once)