You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f1caa3b
Browse filesBrowse the repository at this point in the historyBrowse files
Same theme as the earlier history-I/O fixes this week, applied to the
one place they don't help at all: formwatch serve. /metrics and
/api/forms both called history::all_known_forms fresh on every single
HTTP request - even after the filename-based fix (ac7cf22) made that
one file read per form instead of every historical run, it's still a
full directory walk plus one file read per form, repeated on every
request to a service whose entire purpose is being polled. Prometheus
scrapes /metrics on a fixed interval, an orchestrator hits /readyz
repeatedly, a status page refreshes /api/forms - all far more often
than `monitor` actually writes a new run on its own cron schedule.
Added HistoryCache: a small Mutex<Option<(Instant, Arc<Vec<RunResult>>)>>
with a 2-second TTL, shared via Arc across every connection the
process handles. Both endpoints read through it instead of calling
all_known_forms directly, so a burst of near-simultaneous requests (a
status page loading /metrics and /api/forms back to back, several
Prometheus scrapes close together) collapses to one disk read. 2
seconds of staleness is unnoticeable to any of the real consumers this
serves - none of them need sub-second freshness - so no new CLI flag
or config knob for the TTL; it's a fixed constant.
route() keeps its existing signature otherwise, still directly
unit-testable without a real server; existing tests pass a
HistoryCache::new(Duration::ZERO) so they see exactly the same
"always fresh" behavior as before the cache existed.
Verified with a real cache-behavior test, not just correctness: writes
one run, reads /metrics, writes a second run within the TTL window,
confirms /metrics still reports the stale (cached) count, then sleeps
past the TTL and confirms the next read picks up the second run.
Verified the test actually catches a regression by temporarily making
the cache never expire (dropped the TTL check entirely) - the
refresh-after-expiry assertion failed exactly as predicted - then
restored the fix.
cargo fmt/clippy -D warnings clean, full suite green (164 tests).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
constINDEX:&str = "formwatch (read-only)\n\n GET /healthz liveness\n GET /readyz readiness\n GET /metrics Prometheus metrics\n GET /api/forms latest run of every form (JSON)\n";
39
40
41
+
/// How long a loaded [`history::all_known_forms`] result is reused before
42
+
/// the next `/metrics` or `/api/forms` request re-reads history from
43
+
/// disk. `/metrics` in particular is meant to be scraped (Prometheus,
44
+
/// `/readyz`-style liveness polling, a status page) far more often than
45
+
/// `monitor` actually writes new runs — without this, every scrape
46
+
/// re-walked the entire history directory (one file read per form) for
47
+
/// data that, almost always, hadn't changed since the last scrape a few
48
+
/// seconds earlier. Short enough that nobody scraping a monitoring
0 commit comments