main
ply4 git calls191 ms45 KiB2 cachedprotocol v2
236 lines14 KiBRaw
1// The recording transport: a fetch the workbench hands to each ply client it runs (one per channel), which forwards
2// every request upstream and records the exchange: when it started, when the server's headers came, when its answer
3// ended, the bytes each way, the status, the cf-ray, the headers (a credential never: request headers are an allow-list
4// and every text passes the run's scrubber), and what the git bytes say (src/bench/wire.ts). It also applies a step's
5// fault (ply/probes Fault) to the first matching request of the `main` channel, as ply's recording proxy does.
6// Channels: `main` (ply, the client under test), `side` (seeding and the racing writer), `judge` (reading the result).
7import type { Fault } from 'ply/probes';
8import type { Span, Tracer } from 'ply';
9import { parseCfRay, renderRay, type Ray } from '../rays.ts';
10import { readAdvertisement, readFetchAnswer, readPkts, readReceiveRequest, readReport, readV2Request, reportVerdict, type Advertisement, type FetchAnswer, type ReceiveRequest, type Report, type V2Request, type AdvertisedRef } from './wire.ts';
12export type Channel = 'main' | 'side' | 'judge';
13// Which part of the run an exchange belongs to.
14export type Phase = { readonly tag: 'seed' } | { readonly tag: 'step'; readonly index: number } | { readonly tag: 'judge' } | { readonly tag: 'cleanup' };
15export type ExchangeKind = 'advertise-upload' | 'advertise-receive' | 'ls-refs' | 'fetch' | 'receive-pack' | 'other';
17export type Wire =
18 | { readonly tag: 'advertisement'; readonly advertisement: Advertisement }
19 | { readonly tag: 'receive-pack'; readonly request: ReceiveRequest; readonly report: Report }
20 | { readonly tag: 'ls-refs'; readonly request: V2Request; readonly refs: readonly AdvertisedRef[] }
21 | { readonly tag: 'fetch'; readonly request: V2Request; readonly answer: FetchAnswer }
22 | { readonly tag: 'opaque' };
24export type Answer =
25 | { readonly tag: 'http'; readonly status: number }
26 // no HTTP answer reached the recorder: the upstream closed, a fault cut the request, or the client gave up
27 | { readonly tag: 'no-response'; readonly error: string };
29// The fault applied to this exchange, as it happened.
30export type Applied =
31 | { readonly tag: 'race'; readonly moved: number }
32 | { readonly tag: 'cut-response'; readonly heldBytes: number }
33 | { readonly tag: 'cut-request'; readonly sentBytes: number }
34 | { readonly tag: 'stall-request'; readonly atBytes: number; readonly ms: number }
35 | { readonly tag: 'stall-response'; readonly ms: number }
36 | { readonly tag: 'drop-response'; readonly deliveredBytes: number };
38export type Exchange = {
39 readonly i: number;
40 readonly phase: Phase;
41 readonly channel: Channel;
42 readonly method: string;
43 readonly path: string;
44 readonly kind: ExchangeKind;
45 readonly answer: Answer;
46 // milliseconds from the start of the run; headersMs is null when no headers came
47 readonly startMs: number;
48 readonly headersMs: number | null;
49 readonly endMs: number;
50 readonly requestBytes: number;
51 readonly responseBytes: number;
52 readonly cfRay: Ray;
53 readonly requestHeaders: ReadonlyArray<readonly [string, string]>;
54 readonly responseHeaders: ReadonlyArray<readonly [string, string]>;
55 readonly wire: Wire;
56 readonly applied: Applied | null;
57};
59// A fault armed for the next matching request of `main`: a push step's first receive-pack POST, or a fetch step's first
60// upload-pack POST that carries command=fetch. A race runs the other writer before forwarding.
61export type Armed = { readonly fault: Exclude<Fault, { readonly tag: 'none' }>; readonly on: 'receive' | 'fetch'; readonly race: () => Promise<number> };
63export type Recorder = {
64 readonly exchanges: Exchange[];
65 readonly fetchFor: (channel: Channel) => typeof fetch;
66 readonly arm: (a: Armed) => void;
67 readonly disarm: () => boolean; // true when the armed fault never fired
68 readonly setPhase: (p: Phase, span: Span | null) => void;
69};
71export type RecorderDeps = {
72 readonly upstream: typeof fetch;
73 readonly now: () => number; // milliseconds since the run started
74 readonly scrub: (text: string) => string;
75 readonly tracer: Tracer;
76 // the bodies of an exchange, for the run's blob store (R2); never awaited on the request path's answer
77 readonly keepBodies: (i: number, request: Uint8Array, response: Uint8Array) => void;
78};
80const REQUEST_HEADERS = new Set(['content-type', 'accept', 'git-protocol', 'user-agent', 'content-encoding', 'content-length']);
81const PROBE_BYTES = 4; // stock git's 4-byte probe POST; ply sends none, but a 4-byte body is never the push
82const CUT_REQUEST_DELAY_MS = 500;
83// A streamed request body: Node's fetch (the tests) wants duplex 'half'; the Workers runtime ignores it.
84const streamed = (init: RequestInit): RequestInit => (init.body instanceof ReadableStream ? ({ ...init, duplex: 'half' } as RequestInit) : init);
86export function kindOf(method: string, path: string, body: Uint8Array): ExchangeKind {
87 const bare = path.split('?')[0] || '';
88 if (method === 'GET' && bare.endsWith('/info/refs')) return path.includes('service=git-receive-pack') ? 'advertise-receive' : 'advertise-upload';
89 if (method === 'POST' && bare.endsWith('/git-receive-pack')) return 'receive-pack';
90 if (method === 'POST' && bare.endsWith('/git-upload-pack')) {
91 const cmd = readV2Request(body).command;
92 if (cmd === 'ls-refs') return 'ls-refs';
93 if (cmd === 'fetch') return 'fetch';
94 }
95 return 'other';
98function wireOf(kind: ExchangeKind, request: Uint8Array, response: Uint8Array, answered: boolean): Wire {
99 switch (kind) {
100 case 'advertise-upload':
101 case 'advertise-receive':
102 return answered ? { tag: 'advertisement', advertisement: readAdvertisement(response) } : { tag: 'opaque' };
103 case 'receive-pack':
104 return { tag: 'receive-pack', request: readReceiveRequest(request), report: readReport(response) };
105 case 'ls-refs': {
106 const refs = readPkts(response).pkts.flatMap(p => {
107 if (p.tag !== 'data') return [];
108 const [oid, ref] = new TextDecoder('latin1').decode(p.bytes).replace(/\n$/, '').split(' ');
109 return typeof oid === 'string' && /^[0-9a-f]{40}$/.test(oid) && typeof ref === 'string' ? [{ oid: oid as AdvertisedRef['oid'], ref }] : [];
110 });
111 return { tag: 'ls-refs', request: readV2Request(request), refs };
112 }
113 case 'fetch':
114 return { tag: 'fetch', request: readV2Request(request), answer: readFetchAnswer(response) };
115 case 'other':
116 return { tag: 'opaque' };
117 }
120const sleep = (ms: number, signal: AbortSignal | null): Promise<void> => new Promise((resolve, reject) => {
121 const t = setTimeout(resolve, ms);
122 if (signal !== null) signal.addEventListener('abort', () => { clearTimeout(t); reject(signal.reason); }, { once: true });
123});
124const errorText = (e: unknown): string => {
125 const m = e instanceof Error ? `${e.name}: ${e.message}` : String(e);
126 const cause = e instanceof Error && e.cause instanceof Error ? `: ${e.cause.message}` : '';
127 return `${m}${cause}`.slice(0, 300);
128};
130async function plainBody(req: Request): Promise<Uint8Array> {
131 if (req.method === 'GET' || req.method === 'HEAD' || req.body === null) return new Uint8Array(0);
132 const raw = new Uint8Array(await req.arrayBuffer());
133 if (req.headers.get('content-encoding') !== 'gzip') return raw;
134 return new Uint8Array(await new Response(new Blob([raw]).stream().pipeThrough(new DecompressionStream('gzip'))).arrayBuffer());
137export function createRecorder(deps: RecorderDeps): Recorder {
138 const exchanges: Exchange[] = [];
139 let armed: Armed | null = null;
140 let phase: Phase = { tag: 'seed' };
141 let parent: Span | null = null;
143 const fetchFor = (channel: Channel): typeof fetch => async (input, init) => {
144 const req = new Request(input, init);
145 const url = new URL(req.url);
146 const path = `${url.pathname}${url.search}`;
147 const body = await plainBody(req);
148 const kind = kindOf(req.method, path, body);
149 const i = exchanges.length;
150 const startMs = deps.now();
151 const span = deps.tracer.start('bench.exchange', { parent, attrs: { 'bench.channel': channel, 'bench.exchange': i, 'bench.kind': kind, 'http.request.method': req.method, 'url.path': url.pathname, 'http.request.body.size': body.length } });
152 let fault: Armed | null = null;
153 if (armed !== null && channel === 'main' && (armed.on === 'receive' ? kind === 'receive-pack' && body.length > PROBE_BYTES : kind === 'fetch')) { fault = armed; armed = null; }
154 const headers = new Headers();
155 for (const [k, v] of req.headers) if (k !== 'content-encoding' && k !== 'content-length') headers.set(k, v);
156 const requestHeaders = [...req.headers].filter(([k]) => REQUEST_HEADERS.has(k)).map(([k, v]) => [k, deps.scrub(v)] as const);
157 let applied: Applied | null = null;
158 const finish = (answer: Answer, headersMs: number | null, response: Uint8Array, res: Response | null): void => {
159 const ray = res === null ? { kind: 'no_response' } as const : parseCfRay(res.headers.get('cf-ray'));
160 const ex: Exchange = {
161 i, phase, channel, method: req.method, path: deps.scrub(path), kind, answer, startMs, headersMs, endMs: deps.now(),
162 requestBytes: body.length, responseBytes: response.length, cfRay: ray, requestHeaders,
163 responseHeaders: res === null ? [] : [...res.headers].map(([k, v]) => [k, deps.scrub(v)] as const),
164 wire: wireOf(kind, body, response, answer.tag === 'http' && answer.status === 200), applied,
165 };
166 exchanges.push(ex);
167 deps.keepBodies(i, body, response);
168 const report = ex.wire.tag === 'receive-pack' ? reportVerdict(ex.wire.report) : 'n/a';
169 span.set({ 'cf.ray': renderRay(ray), 'http.response.body.size': response.length, 'bench.report': report, 'bench.fault': applied === null ? 'none' : applied.tag, 'bench.ttfb_ms': headersMs === null ? -1 : headersMs - startMs });
170 if (answer.tag === 'http') span.set({ 'http.response.status_code': answer.status });
171 if (answer.tag === 'no-response') span.error(new Error(answer.error)); else span.ok();
172 span.end();
173 };
174 try {
175 if (fault !== null && fault.fault.tag === 'race') applied = { tag: 'race', moved: await fault.race() };
176 const reqBody = req.method === 'GET' || req.method === 'HEAD' ? null : body;
177 if (fault !== null && fault.fault.tag === 'cut-request' && reqBody !== null) {
178 const cutAt = Math.max(1, Math.floor(reqBody.length * fault.fault.fraction));
179 applied = { tag: 'cut-request', sentBytes: cutAt };
180 const ctl = new AbortController();
181 const part = new ReadableStream<Uint8Array>({ start(c) { c.enqueue(reqBody.slice(0, cutAt)); setTimeout(() => { c.error(new Error('probe: request body cut')); ctl.abort(); }, CUT_REQUEST_DELAY_MS); } });
182 try {
183 const up = await deps.upstream(req.url, streamed({ method: req.method, headers, body: part, redirect: 'manual', signal: ctl.signal }));
184 if (up.body !== null) await up.body.cancel();
185 finish({ tag: 'http', status: up.status }, deps.now(), new Uint8Array(0), up);
186 } catch (e) {
187 finish({ tag: 'no-response', error: `cut at ${cutAt} of ${reqBody.length} bytes: ${errorText(e)}` }, null, new Uint8Array(0), null);
188 }
189 throw new TypeError('network connection lost (probe: request body cut)');
190 }
191 let sent: BodyInit | null = reqBody === null ? null : reqBody.slice();
192 if (fault !== null && fault.fault.tag === 'stall-request' && reqBody !== null) {
193 const at = Math.max(1, Math.floor(reqBody.length * fault.fault.fraction));
194 const ms = fault.fault.ms;
195 applied = { tag: 'stall-request', atBytes: at, ms };
196 sent = new ReadableStream<Uint8Array>({ async start(c) { c.enqueue(reqBody.slice(0, at)); await sleep(ms, null); c.enqueue(reqBody.slice(at)); c.close(); } });
197 }
198 const up = await deps.upstream(req.url, streamed({ method: req.method, headers, body: sent, redirect: 'manual', signal: req.signal }));
199 const headersMs = deps.now();
200 const buf = new Uint8Array(await up.arrayBuffer());
201 finish({ tag: 'http', status: up.status }, headersMs, buf, up);
202 const out = new Headers(up.headers);
203 out.delete('content-encoding');
204 out.set('content-length', String(buf.length));
205 if (fault !== null && fault.fault.tag === 'cut-response') {
206 applied = { tag: 'cut-response', heldBytes: buf.length };
207 exchanges[i] = { ...exchanges[i] as Exchange, applied };
208 throw new TypeError('network connection lost (probe: answer cut)');
209 }
210 if (fault !== null && fault.fault.tag === 'stall-response') {
211 applied = { tag: 'stall-response', ms: fault.fault.ms };
212 exchanges[i] = { ...exchanges[i] as Exchange, applied };
213 await sleep(fault.fault.ms, req.signal);
214 }
215 if (fault !== null && fault.fault.tag === 'drop-response') {
216 const cut = Math.floor(buf.length * fault.fault.fraction);
217 applied = { tag: 'drop-response', deliveredBytes: cut };
218 exchanges[i] = { ...exchanges[i] as Exchange, applied };
219 const part = new ReadableStream<Uint8Array>({ start(c) { c.enqueue(buf.slice(0, cut)); c.error(new TypeError('network connection lost (probe: answer dropped)')); } });
220 return new Response(part, { status: up.status, statusText: up.statusText, headers: out });
221 }
222 return new Response(up.status === 204 || up.status === 304 ? null : buf, { status: up.status, statusText: up.statusText, headers: out });
223 } catch (e) {
224 if (exchanges.length === i) finish({ tag: 'no-response', error: errorText(e) }, null, new Uint8Array(0), null);
225 throw e;
226 }
227 };
229 return {
230 exchanges,
231 fetchFor,
232 arm: a => { armed = a; },
233 disarm: () => { const left = armed !== null; armed = null; return left; },
234 setPhase: (p, span) => { phase = p; parent = span; },
235 };
Diagnostics4 git calls · 191 ms
CallStatusSizeTime0191 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · 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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=185
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=2
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=61
Open pack map
fetch2004.3 KiB185 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4428
git.response.cache=MISS
git.response.duration_ms=185
git.fetch.wants=b4f5e3f2fe029c0d392869bc099cc0b0597c4452
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=4428
git.pkt.payload.max=4407
git.sideband.band1.bytes=4406
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
a5e32953dbbe4628
Request
0bca9d7d…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format