main
1// The batched limiter on the request path (docs/rate-limit.md). One Workers Cache read of the blocked document decides;
2// an admitted hit goes into the isolate's batch and is flushed from ctx.waitUntil. Nothing here waits on the Durable
3// Object unless the document is a cache MISS/EXPIRED.
4import { dig } from '../absent.ts';
5import { EDGE_ATTR, edgeTracingOf, type EdgeSpan } from '../edge-trace.ts';
6import { Batcher, defaultTuning, type FlushReport } from './batcher.ts';
7import { BUCKET_PACK, BUCKET_PAGE, DOC_URL, KEY_HEX, MAX_DOC_BYTES, MEMO_FALLBACK_MS, MEMO_REFRESH_ABANDON_MS, MEMO_REFRESH_MS, READ_BUDGET_MS } from './config.ts';
8import type { Fault } from './core.ts';
9import { DOC_AT_HEADER, indexDoc, type DocIndex } from './doc.ts';
10import { counterNamespace, counterOf } from './stub.ts';
12export type RateMode = 'legacy' | 'batched' | 'memo';
13export type SpanSet = (key: string, value: string | number | boolean) => void;
15export type RateIdentity = {
16 /** Full SHA-256 hex: the legacy shard name. Its first KEY_HEX characters are the batched key. */
17 readonly hex: string;
18 readonly mode: RateMode;
19 readonly fault: Fault | null;
20 /** The request proved the load-test token. */
21 readonly load: boolean;
22 /**
23 * Where `mode` came from: `default` is the env's RATE_MODE (what every real request runs), `header` is a load request's
24 * `x-ply-rate-mode`. The CD rate gate (scripts/cd/rate-gate.ts) judges only `default` checks against the configured mode.
25 */
26 readonly modeSource: 'default' | 'header';
27};
29export const LOAD_TOKEN_HEADER = 'x-ply-load-token';
30export const LOAD_CLIENT_HEADER = 'x-ply-rate-client';
31export const LOAD_MODE_HEADER = 'x-ply-rate-mode';
32export const LOAD_FAULT_HEADER = 'x-ply-rate-fault';
34const enc = new TextEncoder();
36async function sha256Hex(text: string): Promise<string> {
37 const digest = await crypto.subtle.digest('SHA-256', enc.encode(text));
38 let hex = '';
39 for (const byte of new Uint8Array(digest)) hex += byte.toString(16).padStart(2, '0');
40 return hex;
41}
43async function tokenEqual(left: string, right: string): Promise<boolean> {
44 const [x, y] = await Promise.all([crypto.subtle.digest("SHA-256", enc.encode(left)), crypto.subtle.digest("SHA-256", enc.encode(right))]);
45 const subtle = crypto.subtle as SubtleCrypto & { timingSafeEqual: unknown };
46 if (typeof subtle.timingSafeEqual === 'function') return (subtle.timingSafeEqual as (a: ArrayBuffer, b: ArrayBuffer) => boolean).call(subtle, x, y);
47 // Node (unit tests) has no timingSafeEqual on SubtleCrypto. Equal-length digests, compared without an early exit.
48 const xs = new Uint8Array(x);
49 const ys = new Uint8Array(y);
50 let diff = 0;
51 for (let i = 0; i < xs.length; i++) diff |= xs[i] ^ ys[i];
52 return diff === 0;
53}
55function textBinding(env: unknown, name: string): string {
56 const found = dig(env, [name]);
57 return found.tag === 'some' && typeof found.value === 'string' ? found.value : '';
58}
60/** `slow-ack=<ms>;attempts=<n>` or `error;attempts=<n>`. Anything else is no fault. */
61export function parseFault(raw: string | null): Fault | null {
62 if (raw === null) return null;
63 const slow = /^slow-ack=(\d{1,5})(;attempts=(\d{1,2}))*$/.exec(raw.trim());
64 if (slow !== null) return { kind: 'slow-ack', ms: Number(slow[1]), attempts: Number(slow[3] || '1') };
65 const error = /^error(;attempts=(\d{1,2}))*$/.exec(raw.trim());
66 if (error !== null) return { kind: 'error', ms: 0, attempts: Number(error[2] || '1') };
67 return null;
68}
70/** The env's default mode. Batched and memo need the counter binding; otherwise legacy. */
71export function defaultMode(env: unknown, ctx: unknown): RateMode {
72 const asked = textBinding(env, 'RATE_MODE');
73 if ((asked === 'batched' || asked === 'memo') && counterNamespace(env, ctx) !== null) return asked;
74 return 'legacy';
75}
77/**
78 * The secret that unlocks the load-test headers: PLY_LOAD_TOKEN, a credential of its own. PLY_TOKEN (the /v1 bearer)
79 * does not unlock them, so the load generator (ply-load) never holds the API credential.
80 */
81export const LOAD_TOKEN_BINDING = 'PLY_LOAD_TOKEN';
83/**
84 * The client's rate identity. Load-test headers count only with `x-ply-load-token` equal to PLY_LOAD_TOKEN (constant
85 * time); the token is never forwarded, logged or echoed. With no PLY_LOAD_TOKEN bound, no request is a load request.
86 */
87export async function rateIdentity(req: Request, env: unknown, ctx: unknown): Promise<RateIdentity> {
88 let mode = defaultMode(env, ctx);
89 const presented = req.headers.get(LOAD_TOKEN_HEADER);
90 const secret = textBinding(env, LOAD_TOKEN_BINDING);
91 if (presented !== null && secret.length > 0 && (await tokenEqual(presented, secret))) {
92 const label = req.headers.get(LOAD_CLIENT_HEADER);
93 const asked = req.headers.get(LOAD_MODE_HEADER);
94 let modeSource: RateIdentity['modeSource'] = 'default';
95 if (asked === 'legacy') { mode = 'legacy'; modeSource = 'header'; }
96 if ((asked === 'batched' || asked === 'memo') && counterNamespace(env, ctx) !== null) { mode = asked; modeSource = 'header'; }
97 const hex = await sha256Hex(`ply-viewer-rate/1\n${label === null ? 'load' : `load:${label.slice(0, 128)}`}`);
98 return { hex, mode, fault: parseFault(req.headers.get(LOAD_FAULT_HEADER)), load: true, modeSource };
99 }
100 const connecting = req.headers.get('cf-connecting-ip');
101 const hex = await sha256Hex(`ply-viewer-rate/1\n${connecting === null ? 'unknown' : connecting}`);
102 return { hex, mode, fault: null, load: false, modeSource: 'default' };
103}
105let checks = 0;
107/** Count this isolate's rate checks; true for its first. A cold isolate's first batched read may fail open by design. */
108export function firstCheckInIsolate(): boolean {
109 checks++;
110 return checks === 1;
111}
113// ---- the span helper: Workers spans stamped like ply's (src/edge-trace.ts EDGE_ATTR) ----
115export function versionOf(env: unknown): string {
116 const v = dig(env, ['VERSION', 'id']);
117 return v.tag === 'some' && typeof v.value === 'string' ? v.value : '';
118}
120export async function inSpan<T>(ctx: unknown, name: string, stamp: Readonly<Record<string, string>>, fn: (set: SpanSet) => Promise<T>): Promise<T> {
121 const tracing = edgeTracingOf(ctx);
122 if (tracing === null) return fn(() => {});
123 return tracing.startActiveSpan(name, async (span: EdgeSpan) => {
124 const set: SpanSet = (k, v) => { try { span.setAttribute(k, v); } catch { /* tracing never fails a request */ } };
125 for (const [k, v] of Object.entries(stamp)) if (v) set(k, v);
126 try {
127 return await fn(set);
128 } catch (e) {
129 try { span.setStatus({ code: 'error', message: e instanceof Error ? e.name : 'Error' }); } catch { /* ignore */ }
130 throw e;
131 } finally {
132 try { span.end(); } catch { /* ignore */ }
133 }
134 });
135}
137// ---- read side ----
139type Memo = { etag: string; index: DocIndex; gen: number; at: number; entries: number; fetchedAt: number; bytes: number; truncated: boolean };
140let memo: Memo | null = null;
142export type DocRead = { readonly read: 'ok' | 'memo' | 'fail' | 'late_memo' | 'late_open'; readonly cache: string; readonly memo: Memo | null; readonly parsed: boolean };
144type CacheBinding = { fetch(req: Request): Promise<Response> };
146function rateCacheOf(ctx: unknown): CacheBinding | null {
147 const found = dig(ctx, ['exports', 'RateCache']);
148 if (found.tag === 'none' || found.value === null) return null;
149 return found.value as CacheBinding;
150}
152/** The body as text, or null (and the body cancelled) once it passes `max` bytes: never buffers more than max + a chunk. */
153async function boundedText(res: Response, max: number): Promise<string | null> {
154 if (res.body === null) return '';
155 const reader = res.body.getReader();
156 const parts: Uint8Array[] = [];
157 let size = 0;
158 for (;;) {
159 const { done, value } = await reader.read();
160 if (done) break;
161 size += value.byteLength;
162 if (size > max) { await reader.cancel().catch(() => {}); return null; }
163 parts.push(value);
164 }
165 const all = new Uint8Array(size);
166 let at = 0;
167 for (const p of parts) { all.set(p, at); at += p.byteLength; }
168 return new TextDecoder().decode(all);
169}
171async function fetchDoc(ctx: unknown, now: number): Promise<DocRead> {
172 const rateCache = rateCacheOf(ctx);
173 const fallback = (cache: string): DocRead => (memo !== null && now - memo.fetchedAt < MEMO_FALLBACK_MS ? { read: 'memo', cache, memo, parsed: false } : { read: 'fail', cache, memo: null, parsed: false });
174 if (rateCache === null) return fallback('NONE');
175 let res: Response;
176 try {
177 res = await rateCache.fetch(new Request(DOC_URL));
178 } catch {
179 return fallback('ERROR');
180 }
181 const cacheHeader = res.headers.get('cf-cache-status');
182 const cache = cacheHeader === null ? 'NONE' : cacheHeader.toUpperCase();
183 if (res.status !== 200) {
184 if (res.body !== null) await res.body.cancel();
185 return fallback(cache);
186 }
187 const declared = Number(res.headers.get('content-length'));
188 if (Number.isFinite(declared) && declared > MAX_DOC_BYTES) {
189 // Not read: an oversized body must not reach the isolate's heap. The last document (if any) stays in use.
190 if (res.body !== null) await res.body.cancel();
191 return fallback('TOO_LARGE');
192 }
193 const etag = res.headers.get('etag');
194 const atHeader = Number(res.headers.get(DOC_AT_HEADER));
195 if (etag !== null && memo !== null && memo.etag === etag) {
196 if (res.body !== null) await res.body.cancel();
197 memo = { ...memo, at: Number.isFinite(atHeader) && atHeader > 0 ? atHeader : memo.at, fetchedAt: now };
198 return { read: 'ok', cache, memo, parsed: false };
199 }
200 try {
201 const text = await boundedText(res, MAX_DOC_BYTES);
202 if (text === null) return fallback('TOO_LARGE');
203 const d = indexDoc(text);
204 memo = { etag: etag === null ? '' : etag, index: d.index, gen: d.gen, at: d.at, entries: d.entries, fetchedAt: now, bytes: text.length, truncated: d.truncated };
205 return { read: 'ok', cache, memo, parsed: true };
206 } catch {
207 return fallback(cache);
208 }
209}
211/**
212 * The read on the request path never waits on the Durable Object (docs/rate-limit.md "Read budget"). The cached fetch
213 * gets `budgetMs`; a HIT answers in a few ms. Past the budget (a MISS is a synchronous DO read, 400+ ms on lat2; an
214 * isolate's first loopback, ~55 ms) the request decides from the isolate's last document, or admits when it has none,
215 * and the fetch finishes in waitUntil, refreshing that document for the next request. `budgetMs` null waits (tests).
216 */
217export async function readDoc(ctx: unknown, now: number, budgetMs: number | null = null): Promise<DocRead> {
218 const pending = fetchDoc(ctx, now);
219 if (budgetMs === null) return pending;
220 const late: DocRead = { read: 'late_open', cache: 'LATE', memo: null, parsed: false };
221 const won = await Promise.race([pending, new Promise<DocRead>(resolve => setTimeout(() => resolve(late), budgetMs))]);
222 if (won !== late) return won;
223 const waitUntil = dig(ctx, ['waitUntil']);
224 if (waitUntil.tag === 'some' && typeof waitUntil.value === 'function') (waitUntil.value as (p: Promise<unknown>) => void).call(ctx, pending.catch(() => null));
225 const held = memo;
226 if (held !== null && Date.now() - held.fetchedAt < MEMO_FALLBACK_MS) return { read: 'late_memo', cache: 'LATE', memo: held, parsed: false };
227 return late;
228}
230// ---- memo mode: decide from the isolate's document, refresh it single-flight in waitUntil ----
232export type RefreshReport = { readonly read: DocRead['read']; readonly cache: string; readonly parsed: boolean; readonly ms: number; readonly joins: number };
233type Flight = { promise: Promise<RefreshReport>; readonly startedAt: number; joins: number };
234let flight: Flight | null = null;
236/**
237 * How this isolate's last refresh ended. `none`: its first read is still in flight (or not started), so a cold check is
238 * start-up. `ok`: the document was read, so a memo that has since aged past MEMO_FALLBACK_MS expired while the isolate
239 * sat idle (no request came to refresh it). `failed`: the read path returned no document (a hung flight that was
240 * abandoned counts), so a cold check is a broken read path.
241 */
242export type LastRefresh =
243 | { readonly kind: 'none' }
244 | { readonly kind: 'ok' }
245 | { readonly kind: 'failed'; readonly read: DocRead['read'] | 'abandoned' | 'threw' };
246let lastRefresh: LastRefresh = { kind: 'none' };
248function waitUntilOf(ctx: unknown): (p: Promise<unknown>) => void {
249 const found = dig(ctx, ['waitUntil']);
250 if (found.tag === 'some' && typeof found.value === 'function') return p => (found.value as (p: Promise<unknown>) => void).call(ctx, p);
251 return () => {};
252}
254/**
255 * At most one refresh of the isolate's document in flight. The request that starts it keeps it alive with
256 * ctx.waitUntil, inside a `ply.rate.refresh` span; a request that finds one in flight joins it (counted, and also
257 * waitUntil'd: free, and the refresh survives the starter's context ending). A flight older than
258 * MEMO_REFRESH_ABANDON_MS is abandoned and replaced. Returns whether this call started a new flight.
259 */
260export function refreshMemo(ctx: unknown, env: unknown, trigger: RefreshTrigger): { started: boolean; promise: Promise<RefreshReport> } {
261 const now = Date.now();
262 const keep = waitUntilOf(ctx);
263 if (flight !== null && now - flight.startedAt < MEMO_REFRESH_ABANDON_MS) {
264 flight.joins++;
265 keep(flight.promise);
266 return { started: false, promise: flight.promise };
267 }
268 if (flight !== null) lastRefresh = { kind: 'failed', read: 'abandoned' }; // a hung read is no longer start-up
269 const mine: Flight = { promise: Promise.resolve({ read: 'fail', cache: 'NONE', parsed: false, ms: 0, joins: 0 }), startedAt: now, joins: 0 };
270 flight = mine;
271 const run = inSpan(ctx, 'ply.rate.refresh', { [EDGE_ATTR.version]: versionOf(env), [EDGE_ATTR.route]: 'rate.refresh' }, async set => {
272 const t0 = performance.now();
273 set('rate.refresh.trigger', trigger.kind);
274 if (trigger.kind === 'cold') {
275 set('rate.refresh.cold', trigger.cause.kind);
276 if (trigger.cause.kind === 'memo_expired') set('rate.refresh.memo_expired_ms', trigger.cause.memoAgeMs);
277 }
278 let r: DocRead;
279 try {
280 r = await fetchDoc(ctx, now);
281 } catch (e) {
282 lastRefresh = { kind: 'failed', read: 'threw' };
283 throw e;
284 } finally {
285 if (flight === mine) flight = null;
286 }
287 lastRefresh = r.read === 'ok' ? { kind: 'ok' } : { kind: 'failed', read: r.read };
288 const report: RefreshReport = { read: r.read, cache: r.cache, parsed: r.parsed, ms: Math.round((performance.now() - t0) * 1000) / 1000, joins: mine.joins };
289 set('rate.refresh.read', report.read);
290 set('rate.refresh.cache', report.cache);
291 set('rate.refresh.parsed', report.parsed);
292 set('rate.refresh.ms', report.ms);
293 set('rate.refresh.joins', report.joins);
294 if (r.memo !== null) {
295 set('rate.doc_bytes', r.memo.bytes);
296 if (r.memo.truncated) set('rate.doc_truncated', true);
297 set('rate.doc_entries', r.memo.entries);
298 set('rate.doc_gen', r.memo.gen);
299 set('rate.doc_age_ms', Math.max(0, Date.now() - r.memo.at));
300 }
301 return report;
302 });
303 mine.promise = run.catch((): RefreshReport => {
304 if (flight === mine) flight = null;
305 return { read: 'fail', cache: 'ERROR', parsed: false, ms: 0, joins: mine.joins };
306 });
307 keep(mine.promise);
308 return { started: true, promise: mine.promise };
309}
311/**
312 * Why a memo-mode check found no usable document (docs/rate-limit.md "CD gate"). Each one admits (fail_open):
313 *
314 * isolate_start the isolate's first refresh has not settled: start-up, expected.
315 * memo_expired the isolate held a document, its last refresh read ok, and the document aged past MEMO_FALLBACK_MS
316 * because no request came to refresh it (an idle isolate): expected. The refresh this check starts is
317 * stamped with the cause, so the CD gate counts it as a read-path failure when that refresh fails too.
318 * read_failed the last refresh settled with no document: the read path is broken. A real failure.
319 */
320export type ColdCause =
321 | { readonly kind: 'isolate_start' }
322 | { readonly kind: 'memo_expired'; readonly memoAgeMs: number }
323 | { readonly kind: 'read_failed'; readonly lastRead: DocRead['read'] | 'abandoned' | 'threw'; readonly memoAgeMs: number | null };
325export type RefreshTrigger = { readonly kind: 'cold'; readonly cause: ColdCause } | { readonly kind: 'stale' };
327/** Why a check admitted without a decision (`rate.fail_open_reason`): a memo cold cause, or a batched read that failed or ran late. */
328export type FailOpenReason = ColdCause['kind'] | 'read_late';
330/** Cold causes that are expected (`rate.cold` on the check): the CD rate gate keeps them out of the warm share. */
331export type ExpectedCold = Extract<ColdCause['kind'], 'isolate_start' | 'memo_expired'>;
333export function coldCause(held: Memo | null, last: LastRefresh, now: number): ColdCause {
334 const memoAgeMs = held === null ? null : Math.max(0, now - held.fetchedAt);
335 switch (last.kind) {
336 case 'none': return { kind: 'isolate_start' };
337 // An ok refresh always sets the memo; a null one here is unreachable, and is judged as the strict case.
338 case 'ok': return memoAgeMs === null ? { kind: 'read_failed', lastRead: 'ok', memoAgeMs } : { kind: 'memo_expired', memoAgeMs };
339 case 'failed': return { kind: 'read_failed', lastRead: last.read, memoAgeMs };
340 }
341}
343/** The memo-mode read: never a fetch on the request path. */
344export type MemoRead =
345 | { readonly read: 'memo_fresh'; readonly memo: Memo; readonly memoAgeMs: number }
346 | { readonly read: 'memo_stale_refresh'; readonly memo: Memo; readonly memoAgeMs: number; readonly started: boolean }
347 | { readonly read: 'cold'; readonly cause: ColdCause; readonly started: boolean };
349export function readMemo(ctx: unknown, env: unknown, now: number): MemoRead {
350 const held = memo !== null && now - memo.fetchedAt < MEMO_FALLBACK_MS ? memo : null;
351 if (held === null) {
352 const cause = coldCause(memo, lastRefresh, now);
353 const { started } = refreshMemo(ctx, env, { kind: 'cold', cause });
354 return { read: 'cold', cause, started };
355 }
356 const age = Math.max(0, now - held.fetchedAt);
357 if (age < MEMO_REFRESH_MS) return { read: 'memo_fresh', memo: held, memoAgeMs: age };
358 const { started } = refreshMemo(ctx, env, { kind: 'stale' });
359 return { read: 'memo_stale_refresh', memo: held, memoAgeMs: age, started };
360}
362/** Stamp an expected cold check (`rate.cold`); the CD gate excludes these from the warm fail_open share. */
363export function stampCold(set: SpanSet, kind: ExpectedCold): void {
364 set('rate.cold', kind);
365}
367/** Stamp a memo-mode cold read's cause on its `ply.rate.check` span. */
368function stampColdCause(set: SpanSet, cause: ColdCause): void {
369 set('rate.fail_open_reason', cause.kind);
370 switch (cause.kind) {
371 case 'isolate_start': stampCold(set, cause.kind); return;
372 case 'memo_expired': stampCold(set, cause.kind); set('rate.memo_expired_ms', cause.memoAgeMs); return;
373 case 'read_failed':
374 set('rate.cold_last_read', cause.lastRead);
375 if (cause.memoAgeMs !== null) set('rate.memo_expired_ms', cause.memoAgeMs);
376 return;
377 }
378}
380// ---- write side: the isolate's one batcher ----
382let isolate = '';
383let batcher: Batcher | null = null;
385function isolateId(): string {
386 if (isolate === '') isolate = crypto.randomUUID().replace(/-/g, '').slice(0, 20);
387 return isolate;
388}
390function sleep(ms: number): Promise<void> {
391 return new Promise(resolve => setTimeout(resolve, ms));
392}
394function stampFlush(set: SpanSet, r: FlushReport): void {
395 set('rate.batch.id', r.id);
396 set('rate.batch.keys', r.keys);
397 set('rate.batch.hits', r.hits);
398 set('rate.batch.early', r.early);
399 set('rate.batch.linger_ms', r.lingerMs);
400 set('rate.batch.attempts', r.attempts.length);
401 set('rate.batch.attempt_log', r.attempts.map(a => `${a.outcome}:${a.ms}`).join(','));
402 set('rate.batch.timeouts', r.attempts.filter(a => a.outcome === 'timeout').length);
403 set('rate.batch.errors', r.attempts.filter(a => a.outcome === 'error').length);
404 const lastError = r.attempts.filter(a => a.error !== '').map(a => a.error).pop();
405 if (lastError !== void 0) set('rate.batch.error', lastError);
406 set('rate.batch.outcome', r.outcome);
407 set('rate.batch.deadline_hit', r.deadlineHit);
408 set('rate.batch.replay', r.replay);
409 set('rate.batch.flush_ms', r.flushMs);
410 set('rate.batch.alive_ms', r.aliveMs);
411 const ok = r.attempts.filter(a => a.outcome === 'ok').pop();
412 if (ok !== void 0) set('rate.do.rpc_ms', ok.ms);
413 if (r.ack !== null) {
414 set('rate.do.rows_written', r.ack.rowsWritten);
415 set('rate.do.new_blocks', r.ack.newBlocks);
416 set('rate.do.gen', r.ack.gen);
417 }
418 if (r.fault !== null) set('rate.fault', `${r.fault.kind}=${r.fault.ms};attempts=${r.fault.attempts}`);
419}
421function batcherFor(env: unknown, limitFor: (bucket: 'page' | 'pack') => number): Batcher {
422 if (batcher === null) {
423 batcher = new Batcher({
424 now: () => Date.now(),
425 sleep,
426 send: (req, owner) => {
427 const stub = counterOf(env, owner);
428 if (stub === null) return Promise.reject(new Error('RATE_COUNTER is not bound'));
429 return stub.apply(req);
430 },
431 isolateId,
432 limits: () => [limitFor('page'), limitFor('pack')] as const,
433 report: () => {},
434 ...defaultTuning(),
435 });
436 }
437 return batcher;
438}
440/** Tests: forget the isolate's batcher, memo and refresh flight. */
441export function resetRateState(): void {
442 checks = 0;
443 batcher = null;
444 memo = null;
445 flight = null;
446 lastRefresh = { kind: 'none' };
447 isolate = '';
448}
450export type BatchedDecision = { ok: boolean; remaining: number; retryAfterMs: number };
452/**
453 * The batched and memo decisions (id.mode). Batched reads the cached document within READ_BUDGET_MS; memo decides from
454 * the isolate's document with no fetch and refreshes it in waitUntil (readMemo). `set` is the ply.rate.check span's attribute setter.
455 * Denied requests are not counted (as legacy). A failed read with no usable memo admits (fail open).
456 */
457export async function admitBatched(input: {
458 readonly ctx: unknown;
459 readonly env: unknown;
460 readonly id: RateIdentity;
461 readonly bucket: 'page' | 'pack';
462 readonly now: number;
463 readonly limitFor: (bucket: 'page' | 'pack') => number;
464 readonly set: SpanSet;
465}): Promise<BatchedDecision> {
466 const { ctx, env, id, bucket, now, set } = input;
467 const key = id.hex.slice(0, KEY_HEX);
468 const b = bucket === 'pack' ? BUCKET_PACK : BUCKET_PAGE;
469 let held: Memo | null;
470 let open: boolean;
471 if (id.mode === 'memo') {
472 const m = readMemo(ctx, env, now);
473 set('rate.read', m.read);
474 set('rate.cache', 'MEMO');
475 switch (m.read) {
476 case 'memo_fresh':
477 set('rate.memo_age_ms', m.memoAgeMs);
478 held = m.memo;
479 open = false;
480 break;
481 case 'memo_stale_refresh':
482 set('rate.memo_age_ms', m.memoAgeMs);
483 set('rate.refresh.started', m.started);
484 held = m.memo;
485 open = false;
486 break;
487 case 'cold':
488 set('rate.refresh.started', m.started);
489 stampColdCause(set, m.cause);
490 held = null;
491 open = true;
492 break;
493 }
494 } else {
495 const doc = await readDoc(ctx, now, READ_BUDGET_MS);
496 set('rate.read', doc.read);
497 set('rate.cache', doc.cache);
498 set('rate.doc_parsed', doc.parsed);
499 held = doc.memo;
500 open = doc.read === 'fail' || doc.read === 'late_open';
501 if (doc.read === 'fail') set('rate.fail_open_reason', 'read_failed' satisfies FailOpenReason);
502 if (doc.read === 'late_open') set('rate.fail_open_reason', 'read_late' satisfies FailOpenReason);
503 }
504 if (held !== null) {
505 set('rate.doc_entries', held.entries);
506 set('rate.doc_gen', held.gen);
507 set('rate.doc_age_ms', Math.max(0, Date.now() - held.at));
508 const hit = held.index.get(`${key}:${b}`);
509 if (hit !== void 0 && hit[0] > now) {
510 set('rate.decision', 'deny');
511 set('rate.blocked_for_ms', Math.max(0, now - hit[1]));
512 return { ok: false, remaining: 0, retryAfterMs: hit[0] - now };
513 }
514 }
515 set('rate.decision', open ? 'fail_open' : 'allow');
516 const batch = batcherFor(env, input.limitFor);
517 // A faulted (load-test) hit never joins the shared batch: it flushes alone, so a fault slows or drops only its own count.
518 const before = id.fault !== null || (batch.pending().keys === 0 && batch.pending().hits === 0);
519 const version = versionOf(env);
520 const joined = id.fault !== null ? batch.addIsolated(key, b, id.fault, ctx) : batch.add(key, b, null, ctx);
521 const waitUntil = waitUntilOf(ctx);
522 set('rate.batch.keys_held', batch.keysHeld);
523 if (joined === null) {
524 set('rate.batch.dropped', batch.lastDrop === null ? 'overflow' : batch.lastDrop);
525 } else if (before) {
526 // This request opened the batch: its waitUntil keeps the isolate alive through the linger and the retries.
527 const flushSpan = inSpan(ctx, 'ply.rate.flush', { [EDGE_ATTR.version]: version, [EDGE_ATTR.route]: 'rate.flush' }, async fset => {
528 const report = await joined;
529 stampFlush(fset, report);
530 return report;
531 });
532 flushSpan.catch(() => {});
533 waitUntil(flushSpan);
534 set('rate.batch.opened', true);
535 } else if (batch.pending().keys === 0) {
536 // This request filled the batch to MAX_BATCH_KEYS and started its flush in this request's context: keep it alive too.
537 waitUntil(joined);
538 set('rate.batch.closed_early', true);
539 }
540 return { ok: true, remaining: 0, retryAfterMs: 0 };
541}
Diagnostics4 git calls · 451 ms
CallStatusSizeTime0451 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 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 · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=4 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 · hit3 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=3 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
fetch2007.9 KiB444 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=444 git.response.bytes=8137 git.response.cache=MISS git.response.duration_ms=444 git.fetch.wants=aa59e670dee2a59b62c5fa7c2d61c019f7e25f7c 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=8137 git.pkt.payload.max=8116 git.sideband.band1.bytes=8115 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
- a5e32c577b245094
- Request
- 9cce6b2f…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format