Repository navigation
Conversation
/stats and /api/stats ran every dashboard query on each request. Cache the computed statsData on the server for statsTTL (60 s in production, a field so tests can shorten it), shared by both endpoints. Concurrent misses wait on one in-flight fill instead of each running the queries. The fill runs on context.WithoutCancel of the starting request plus its own 30 s timeout, so the first visitor leaving does not fail it for the others; a waiter whose own context ends gets its context error and the fill still lands in the cache. Errors are not cached. A panic in the fill goroutine becomes an error rather than taking the process down. The snapshot is now shared, so the tie break sort moves from handleStatsPage into queryStats (the old computeStats). Leaving it in the handler is a data race under concurrent renders, which the new concurrent render test catches with -race. /stats.json and pings are untouched. The dashboard footer and deploy/README.md note the up to 60 s staleness. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: vavallee <vavallee@protonmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/statsand/api/statsran every dashboard query (a dozen aggregates over installs and the daily tables) on each request. They now share one cached*statsDatafor 60 seconds.computeStatsis now the cached entry point (stats_cache.go); the old body is renamedqueryStats. Handlers are unchanged, which keeps this independent of fix(telemetry-server): stop logging cancelled stats requests as errors #4091.server.statsTTL(60 s inmain, zero means no cache, which is whatnewTestServergets, so the existing tests still hit the DB every time).golang.org/x/syncis not in go.mod, so no new module.context.WithoutCancel(request)plus its own 30 s timeout. A visitor who leaves gets their context error back (which fix(telemetry-server): stop logging cancelled stats requests as errors #4091 then treats as client gone) and the fill carries on for everyone else and lands in the cache. A request whose context is already done does not start a fill.handleStatsPageintoqueryStats. Leaving it in the handler is a real data race: I put it back temporarily with the cache in place and-raceflaggedstats_page.goon concurrent renders. The renderers already copy before mutating.Staleness: the dashboard footer now says figures are recomputed at most once a minute (it said "refreshes on every page load"), the endpoint list in
main.gosays cached up to 60 s, anddeploy/README.mdnotes it. Nothing depends on these being live to the second: pings write straight to the DB,/stats.json(whatdiscord-statsreads) is computed per request as before,tools/pingdriftreads onlybuild_shafrom/api/stats, which is the package var and not part of the cached data, and the backup workflow uses/api/backup.Fragment:
changelog.d/telemetry-stats-cache.md.How it was verified
stats_cache_test.go, with a counting stub swapped in throughserver.statsQuery:context.Canceledpromptly, a waiter gets data, the fill context was never cancelled, a later request is served from cache, one query total/stats,/api/stats,/statsrun the query once/statsrenders on a warm cache with tied buckets (race guard)queryStatsstill applies the tie breakThere is no fail before on main for the cache tests (the fields do not exist there), so here is mutation evidence instead:
One mutation survives: caching on error too. A failed fill has nil data and the lookup requires non nil data, so it behaves the same.
go test -race ./cmd/telemetry-server/...green, golangci-lint 0 issues. Also merged this branch with #4091 locally: no conflict, and the combined package passes-race -count=2, including #4091's cancel tests through the cached path.Checklist
Merging to main deploys:
ping-server.ymlpushesghcr.io/vavallee/bindery-ping:sha-<commit>, and the infra repo's bump workflow (every 30 minutes) opens and auto merges a PR moving the pinned sha once that build is green.🤖 Generated with Claude Code