Public: anyone can read this; the crew can change it.

src/core/usage_count.ts counts in memory and flushes every pending key in one transaction #2137

opened this

Merged 49d 0h ago, live in production

This adds src/core/usagecount.ts and test/usage-count.test.ts. Nothing calls the module yet (#1918 does that). bun x tsc --noEmit is clean, and the new test and test/check-list.test.ts pass (17 tests, about 250 ms).

Notes

opened this

This adds src/core/usage_count.ts and test/usage-count.test.ts. Nothing calls the module yet (#1918 does that). bun x tsc --noEmit is clean, and the new test and test/check-list.test.ts pass (17 tests, about 250 ms).

  • createUsageCounter(db) returns {count, flush, stop}.
    • count throws a RangeError and writes nothing for a role outside anon|user|admin|agent, a surface outside api|mcp|web, an unknown method, a status outside 200–599, or an empty or over-200-character pattern.
    • A valid count is one map increment under an hour (YYYY-MM-DDTHH, UTC) and the status class (2xx…5xx). It runs no query.
    • flush upserts every pending key in one transaction, adding to n. If the write fails, the counts go back into the pending set.
    • It flushes by itself about a second after the first pending count, in a deferred timer once 500 keys are pending, and on stop(). The timer is unref'd.
  • readUsage(db, sinceHour) returns the rows from that hour on, oldest first.
  • Tests cover:
    • three counts → one row of 3, with nothing before the flush;
    • two hours → two rows;
    • every refusal writes nothing;
    • the timer flush and the 500-key flush.
  • Test hooks: createUsageCounter takes two optional extra arguments, a clock and a flush delay. They let the tests stay far under 1 s.

What I left out: I did not edit scripts/gate.ts. It no longer has a GATE list: gateFiles derives the gate from every test/*.test.ts minus EXCLUDED. The new test is gated as soon as it exists, so there was nothing to name.

I ran only bun x tsc --noEmit, the new test and test/check-list.test.ts, not the whole suite, so I haven't seen any failure elsewhere.

The tests create the table by running the existing 20261016130000-usage-count.ts migration.

To try it: bun test test/usage-count.test.ts.

🤖 Generated with Claude Code

Closes #2078

commented

src/core/usage_count.ts now keeps its counts when a write fails and retries by itself. A failing timer flush no longer takes the server down. The usage-count tests and the type check pass; I didn't run the rest of the suite.

  • Timer path: the timer calls flush through a wrapper that swallows the error. Only an explicit flush() or stop() sees the throw.
  • Retry: the catch in flush puts the counts back and calls schedule again, unless the counter is stopped. count now also schedules whenever timer === null, not only when pending was empty.
  • Second bug, found by the new test: db.query(...) ran before the try. A missing table made it throw after pending.clear(), so the counts were lost. I moved it inside the try.
  • New test: it drops the table, counts twice and lets the timer fail twice without an uncaught exception. It then checks that an explicit flush() throws, recreates the table, and checks that the kept counts (n=2) are written by the retry. A later count and stop() bring it to n=3.
  • Bad events: count still throws RangeError on a malformed event, because the existing test pins that. I documented it in the header comment: a caller on the request path must wrap count in try/catch. If you'd rather it drop the event silently, say so; that's a small change plus a test edit.
  • Wiring: nothing calls the module yet. Wiring it in is a separate issue.

To try it, run bun test test/usage-count.test.ts.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs