main
1// RateCounter: the ONE SQLite Durable Object behind the batched rate limiter (docs/rate-limit.md). It is called by
2// isolate flushes (apply, ~5/s per busy isolate) and by RateCache revalidations (blockedDoc, ≤ 1/s per colo).
3// The logic is CounterCore (src/rate/core.ts); this class adds the fault hooks and the log lines.
4// Durable Object timers do not advance during synchronous work (Spectre mitigation), so apply time is not measured
5// here: the caller measures the RPC, and Workers Observability carries this invocation's CPU time.
6import { DurableObject } from 'cloudflare:workers';
7import { dig } from '../absent.ts';
8import { unwrapOr } from '../option.ts';
9import { CounterCore, type ApplyRequest, type ApplyResult, type BlockedDoc, type SqlExec } from './core.ts';
11export type RateCounterApi = {
12 apply(req: ApplyRequest): Promise<ApplyResult>;
13 blockedDoc(): Promise<BlockedDoc>;
14};
16/** How often an apply also logs the prune backlog and the database size (ply.rate.stats). */
17const STATS_EVERY_MS = 30_000;
19export class RateCounter extends DurableObject {
20 #core: CounterCore | null = null;
21 #statsAt = 0;
23 #counter(): CounterCore {
24 if (this.#core === null) {
25 const sql = this.ctx.storage.sql;
26 const exec: SqlExec = (q, ...b) => sql.exec(q, ...b);
27 this.#core = new CounterCore(exec);
28 }
29 return this.#core;
30 }
32 async apply(req: ApplyRequest): Promise<ApplyResult> {
33 const fault = req.fault !== null && req.attempt <= req.fault.attempts ? req.fault : null;
34 if (fault !== null && fault.kind === 'error') {
35 console.log(JSON.stringify({ msg: 'ply.rate.apply', id: req.id, attempt: req.attempt, fault: 'error' }));
36 throw new Error('injected fault: error before apply');
37 }
38 const result = this.#counter().apply(req, Date.now());
39 console.log(JSON.stringify({
40 msg: 'ply.rate.apply',
41 id: req.id,
42 attempt: req.attempt,
43 replay: result.replay,
44 keys: result.keys,
45 hits: result.hits,
46 new_blocks: result.newBlocks,
47 rows_written: result.rowsWritten,
48 gen: result.gen,
49 pruned_hits: result.pruned.hits,
50 pruned_batches: result.pruned.batches,
51 pruned_blocked: result.pruned.blocked,
52 prune_backlog: result.pruned.backlog,
53 fault: fault === null ? '' : `${fault.kind}=${fault.ms}`,
54 }));
55 this.#stats();
56 for (const o of result.over) console.log(JSON.stringify({ msg: 'ply.rate.over', k: o.k.slice(0, 8), b: o.b, sum: o.sum, limit: o.limit, crossed: o.crossed, peak: o.peak, at: Date.now() }));
57 if (fault !== null && fault.kind === 'slow-ack') await new Promise(r => setTimeout(r, Math.min(fault.ms, 30_000)));
58 return { ...result, over: [] };
59 }
61 /** At most every STATS_EVERY_MS: expired rows still stored per table (the prune backlog) and the SQLite database size. */
62 #stats(): void {
63 const now = Date.now();
64 if (now - this.#statsAt < STATS_EVERY_MS) return;
65 this.#statsAt = now;
66 try {
67 const core = this.#counter();
68 const size = unwrapOr(dig(this.ctx.storage.sql, ['databaseSize']), -1);
69 // Not COUNT(*) of rate_hits (a scan of ~1M rows under a million clients a minute): the backlog range counts are
70 // small while the prune keeps up, and the database size covers the rest.
71 console.log(JSON.stringify({ msg: 'ply.rate.stats', backlog: core.backlog(now), db_bytes: typeof size === 'number' ? size : -1 }));
72 } catch (e) {
73 console.log(JSON.stringify({ msg: 'ply.rate.stats', error_name: e instanceof Error ? e.name : 'Error' }));
74 }
75 }
77 async blockedDoc(): Promise<BlockedDoc> {
78 return this.#counter().doc(Date.now());
79 }
80}
Diagnostics4 git calls · 304 ms
CallStatusSizeTime0304 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit8 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=8 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2001.5 KiB294 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=294 git.response.bytes=1565 git.response.cache=MISS git.response.duration_ms=294 git.fetch.wants=e4528e0098cbe54a920898140550192d2b23857a git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1565 git.pkt.payload.max=1544 git.sideband.band1.bytes=1543 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e347eb7bd74652
- Request
- 5006af7b…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format