Skip to content

Fix /status reporting sessions as zero - #2248

Merged
felladrin merged 1 commit into
mainfrom
fix/status-sessions-always-zero
Jul 29, 2026
Merged

Fix /status reporting sessions as zero#2248
felladrin merged 1 commit into
mainfrom
fix/status-sessions-always-zero

Conversation

@felladrin

Copy link
Copy Markdown
Owner

Fixes the /status endpoint always reporting sessions: 0.

Currently, the memory-bounding cleanup added in #2157 calls verifiedTokens.clear() on the whole set every 60 seconds. That wipes the count every minute, so getVerifiedTokensAmount() only returns a non-zero value if a search happens to land in the current 60-second window. Before #2157 the set was never cleared, so sessions meant "distinct sessions since restart", which is the basis it shares with textualSearches/graphicalSearches when computing the per-session averages.

This PR keeps memory bounded without destroying the metric: it evicts idle tokens with a per-token TTL instead of a full wipe, and tracks distinct sessions with a monotonic counter kept separate from the expiring cache, so the count stays on the "since restart" basis.

What changed

File Change
server/verifiedTokens.ts Set -> Map<token, lastSeen>; cleanup evicts only entries idle longer than 30 min; sessions now comes from a monotonic sessionCount that survives eviction
server/verifyTokenAndRateLimit.ts Every authorized request now records/refreshes the token's last-seen time, so an active session keeps its slot and returning after the idle window counts as a new session
server/verifiedTokens.test.ts New tests: distinct-token counting, count surviving eviction, returning-after-eviction re-count, and an active token staying alive across the idle window

How to test

  1. npx vitest run server/verifiedTokens.test.ts server/verifyTokenAndRateLimit.test.ts (20 tests pass).
  2. Run the app, do a search, then hit /status: sessions reads 1 and stays there past the 60-second mark (before this change it dropped back to 0).

The /status `sessions` field almost always read 0. The 60s cleanup added
to bound memory called `verifiedTokens.clear()` on the whole set, wiping
the count every minute, so it only showed a value when a search happened
to land in the current window.

Bound memory with a per-token idle TTL instead of a full wipe, and track
distinct sessions with a monotonic counter decoupled from the expiring
cache, restoring the since-restart meaning that pairs with the search
counters it is averaged against.
@felladrin
felladrin marked this pull request as ready for review July 29, 2026 10:49
@felladrin felladrin changed the title Fix /status reporting sessions as zero Fix /status reporting sessions as zero Jul 29, 2026
@felladrin
felladrin merged commit 417e3ba into main Jul 29, 2026
7 checks passed
@felladrin
felladrin deleted the fix/status-sessions-always-zero branch July 29, 2026 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant