Commit c01e468
A homepage must not 500 because a count is slow
The site was answering `/` with a 500 every few minutes. Measured 2026-08-23:
30.1s then a 500, thirteen seconds into a `stats-warm` window; 5.5s on the
retry; 0.24s once the window passed. Three things were true at once and this
changes all three.
**The warmer was running at a 42% duty cycle.** `statsTick` runs `categoryStats`
-- nine conditional aggregates over 476,726 feeds, which no index covers -- on
a connection given a deliberate 150s deadline, every `STATS_WARM_SECONDS` and
that defaults to 300. Over 274 minutes: 56 runs, 86-150s each, 42.4% of the
wall clock, and 20 of the 56 abandoned at the ceiling having paid the whole
scan for nothing. `STATS_WARM_SECONDS=3600` is now set on the poller service.
The app already serves this blob up to a day stale, so hourly is honest.
**A read is not free, whatever the comment says.** Both `statsTick` and
`statsWarmer.js` justify the job with "it is a read, and reads do not queue
behind the single writer everything else contends for". That is true of
`serializeWrites` and the Redis queue, which are in-process, and false at the
database: one Turso instance, one SQLite file. Probed from outside across a
confirmed 02:25:41-02:26:39 window, reads held at 79ms while an *empty* write
transaction took 55,699ms -- one call spanning the entire warm and returning as
it ended. That is where the crawler's `write-retried` -> `write-retried` ->
`write-failed` chain comes from, and it is ~90s wide because the client
deadline is 30s and it tries three times.
**The web service was starting the same scan the poller had just given up on.**
`CATEGORY_TTL_MS` was five minutes, so past that `remember` fires a background
refresh, and on this key the refresh is the 86-150s scan against a 20s timeout
it has never once met. Every time the warmer was abandoned the entry went stale
and a reader picked the scan back up. Now an hour, matched to the warmer.
**And the homepage had no guard at all.** It called `listFeeds`, `countFeeds`
and `countFeedsByKind` directly, uncached, with no deadline and no fallback.
`crawlstats.js` already documents what two of those cost at this size: a bare
`count(*)` of `feeds` is 6.9s, and `select category, count(*) ... group by
category` -- which is `countFeedsByKind` verbatim -- exceeds the 30s client
deadline. When it does, nothing catches it and Next answers the site's most
visited URL with a 500. They now go through `remember` in one key, so a stalled
database costs a slightly old count instead of an error page, and a burst of
readers costs one scan rather than one each.
The interval change removes most of the occasions. The homepage change is the
half that matters when there is a next one.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent e6e9acd commit c01e468
3 files changed
Lines changed: 142 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| |||
46 | 45 | | |
47 | 46 | | |
48 | 47 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
55 | 52 | | |
56 | 53 | | |
57 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
47 | 68 | | |
48 | 69 | | |
49 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
0 commit comments