GitHubivarvong
ply
main
1// #255: the exchange clock. A stall is refused (first byte, idle); a long healthy stream is not cut for its duration;
2// requestTimeoutMs is an opt-in whole-exchange cap. Small real timers; every case names the limit that fired.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import { createTransport } from '../src/http.js';
6import { withLimits, DEFAULT_LIMITS } from '../src/limits.js';
7import { exchangeClock, watchedUpload, timeoutLimit, transition, deadlineOf } from '../src/deadline.js';
8import { pushPack } from '../src/receivecaps.js';
9import { ZERO_OID } from '../src/push.js';
11const te = new TextEncoder();
12const RESULT = { 'content-type': 'application/x-git-upload-pack-result' };
13const sleep = ms => new Promise(r => setTimeout(r, ms));
14const spanOf = () => { const attrs = {}; return { attrs, set: a => Object.assign(attrs, a), event() {}, error() {} }; };
15const T = { firstByteTimeoutMs: 150, idleTimeoutMs: 150 };
17// a body of `n` data frames, one every `gapMs`, then a flush; `stallAfter` frames and then nothing
18const dripping = (n, gapMs, stallAfter = Infinity) => {
19 let i = 0;
20 return new ReadableStream({
21 async pull(c) {
22 if (i >= stallAfter) { await new Promise(() => {}); return; }
23 if (i >= n) { c.enqueue(te.encode('0000')); c.close(); return; }
24 if (i > 0) await sleep(gapMs);
25 i++; c.enqueue(te.encode('0008abcd'));
26 },
27 }, { highWaterMark: 0 });
28};
29// a fetch double that honours the signal: the response after `headersMs`, with `body()` as its body
30const answering = (headersMs, body) => (_u, init) => new Promise((resolve, reject) => {
31 const timer = setTimeout(() => resolve(new Response(body(init), { status: 200, headers: RESULT })), headersMs);
32 init.signal.addEventListener('abort', () => { clearTimeout(timer); reject(init.signal.reason); }, { once: true });
33});
34const drain = async (t, body = new Uint8Array(0), span = spanOf()) => {
35 let frames = 0;
36 for await (const _ of t.frames('git-upload-pack', body, span, { bypassCache: false })) frames++;
37 return { frames, span };
38};
39const refusal = async (p) => { try { await p; } catch (e) { return e; } assert.fail('expected a refusal'); };
41test('defaults: first byte and idle 60 s, no total cap', () => {
42 assert.deepEqual([DEFAULT_LIMITS.firstByteTimeoutMs, DEFAULT_LIMITS.idleTimeoutMs, DEFAULT_LIMITS.heldTimeoutMs, DEFAULT_LIMITS.requestTimeoutMs], [60_000, 60_000, 240_000, 0]);
43});
45test('a slow but steady body runs far past every timer and is not cut', async () => {
46 // 20 frames 40 ms apart: 760 ms of body, five times each 150 ms timer
47 const t = createTransport('https://h/r', { fetchImpl: answering(20, () => dripping(20, 40)), limits: withLimits(T) }).value;
48 const t0 = Date.now();
49 const { frames, span } = await drain(t);
50 assert.equal(frames, 21);
51 assert.ok(Date.now() - t0 > 600, `${Date.now() - t0} ms`);
52 assert.equal(span.attrs['git.timeout.limit'], undefined);
53 assert.deepEqual([span.attrs['git.limits.first_byte_timeout_ms'], span.attrs['git.limits.idle_timeout_ms'], span.attrs['git.limits.request_timeout_ms']], [150, 150, 0]);
54});
56test('the same body under an opt-in requestTimeoutMs is cut at the total', async () => {
57 const t = createTransport('https://h/r', { fetchImpl: answering(20, () => dripping(20, 40)), limits: withLimits({ ...T, requestTimeoutMs: 300 }) }).value;
58 const span = spanOf();
59 const e = await refusal(drain(t, new Uint8Array(0), span));
60 assert.deepEqual([e.code, e.what], ['timeout', 'requestTimeoutMs']);
61 assert.match(e.message, /request exceeded 300 ms/);
62 assert.deepEqual([span.attrs['git.timeout.limit'], span.attrs['git.timeout.ms']], ['requestTimeoutMs', 300]);
63});
65test('a body that stalls is refused after the idle timeout, not before', async () => {
66 const t = createTransport('https://h/r', { fetchImpl: answering(10, () => dripping(20, 40, 3)), limits: withLimits(T) }).value;
67 const t0 = Date.now();
68 const span = spanOf();
69 const e = await refusal(drain(t, new Uint8Array(0), span));
70 const took = Date.now() - t0;
71 assert.deepEqual([e.code, e.what], ['timeout', 'idleTimeoutMs']);
72 assert.match(e.message, /no bytes moved for 150 ms/);
73 assert.ok(took >= 200 && took < 1500, `${took} ms`);
74 assert.equal(span.attrs['git.timeout.limit'], 'idleTimeoutMs');
75});
77test('slow headers are refused by the first-byte timeout', async () => {
78 const t = createTransport('https://h/r', { fetchImpl: answering(600, () => dripping(1, 0)), limits: withLimits(T) }).value;
79 const t0 = Date.now();
80 const e = await refusal(drain(t));
81 assert.deepEqual([e.code, e.what], ['timeout', 'firstByteTimeoutMs']);
82 assert.ok(Date.now() - t0 < 500);
83});
85test('an advertisement GET is bounded the same way: slow headers, then a stalled body', async () => {
86 const advert = stall => () => new ReadableStream({ async pull(c) { if (stall) await new Promise(() => {}); c.enqueue(te.encode('001e# service=git-upload-pack\n0000')); c.close(); } });
87 const fetchImpl = (headersMs, stall) => (_u, init) => new Promise((resolve, reject) => {
88 const timer = setTimeout(() => resolve(new Response(advert(stall)(), { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-advertisement' } })), headersMs);
89 init.signal.addEventListener('abort', () => { clearTimeout(timer); reject(init.signal.reason); }, { once: true });
90 });
91 const slow = await createTransport('https://h/r', { fetchImpl: fetchImpl(600, false), limits: withLimits(T) }).value.advertise('git-upload-pack', spanOf());
92 assert.deepEqual([slow.ok, slow.error?.code, slow.error?.what], [false, 'timeout', 'firstByteTimeoutMs']);
93 const stalled = await createTransport('https://h/r', { fetchImpl: fetchImpl(10, true), limits: withLimits(T) }).value.advertise('git-upload-pack', spanOf());
94 assert.deepEqual([stalled.ok, stalled.error?.code, stalled.error?.what], [false, 'timeout', 'idleTimeoutMs']);
95 const fine = await createTransport('https://h/r', { fetchImpl: fetchImpl(10, false), limits: withLimits(T) }).value.advertise('git-upload-pack', spanOf());
96 assert.equal(fine.ok, true);
97});
99// a fetch double that reads the request body as a server would, then answers `afterMs` after the body ends
100const reading = (afterMs, seen = {}) => (_u, init) => new Promise((resolve, reject) => {
101 init.signal.addEventListener('abort', () => reject(init.signal.reason), { once: true });
102 (async () => {
103 const reader = init.body.getReader();
104 let bytes = 0;
105 for (;;) { const r = await reader.read(); if (r.done) break; bytes += r.value.length; }
106 seen.bytes = bytes;
107 await sleep(afterMs);
108 resolve(new Response(dripping(1, 0), { status: 200, headers: RESULT }));
109 })().catch(reject);
110});
111// an upload of `n` chunks `gapMs` apart; `stallAfter` chunks and then nothing
112const uploading = (n, gapMs, stallAfter = Infinity) => {
113 let i = 0;
114 return new ReadableStream({
115 async pull(c) {
116 if (i >= stallAfter) { await new Promise(() => {}); return; }
117 if (i >= n) { c.close(); return; }
118 if (i > 0) await sleep(gapMs);
119 i++; c.enqueue(new Uint8Array(1024));
120 },
121 }, { highWaterMark: 0 });
122};
124test('a long steady upload passes; the first-byte wait starts when the upload ends', async () => {
125 // 12 chunks 40 ms apart (440 ms of upload), then the server answers 100 ms later: under 150 ms from the upload's end
126 const seen = {};
127 const t = createTransport('https://h/r', { fetchImpl: reading(100, seen), limits: withLimits(T) }).value;
128 const { frames } = await drain(t, { stream: uploading(12, 40), length: 12 * 1024 });
129 assert.deepEqual([frames, seen.bytes], [2, 12 * 1024]);
130});
132test('a request body whose source stops producing is refused as idleTimeoutMs: no more pieces reach the platform', async () => {
133 const t = createTransport('https://h/r', { fetchImpl: reading(0), limits: withLimits(T) }).value;
134 const t0 = Date.now();
135 const e = await refusal(drain(t, { stream: uploading(12, 10, 2), length: 12 * 1024 }));
136 assert.deepEqual([e.code, e.what], ['timeout', 'idleTimeoutMs']);
137 assert.ok(Date.now() - t0 < 1500);
138});
140test('pushPack: a pack stream that stops producing partway ends as idleTimeoutMs, as the ambiguous push it is', async () => {
141 const t = createTransport('https://h/r', { fetchImpl: reading(0), limits: withLimits({ ...T, maxPackBytes: 1 << 20 }) }).value;
142 const t0 = Date.now();
143 const r = await pushPack(t, { ref: 'refs/heads/main', oldOid: ZERO_OID, newOid: 'a'.repeat(40), pack: uploading(64, 5, 4), length: 64 * 1024 });
144 assert.equal(r.ok, false);
145 assert.ok(Date.now() - t0 < 1500, `${Date.now() - t0} ms`);
146 // the pack was partly sent: the server may have applied it, so the outcome is ambiguous, never the retryable `timeout`
147 assert.deepEqual([r.error.code, r.error.original, r.error.cause?.code, r.error.cause?.what], ['ambiguous', 'timeout', 'timeout', 'idleTimeoutMs']);
148});
150test('the caller signal aborts as aborted, not timeout', async () => {
151 const ac = new AbortController();
152 const t = createTransport('https://h/r', { fetchImpl: answering(10, () => dripping(50, 40)), limits: withLimits(T), signal: ac.signal }).value;
153 setTimeout(() => ac.abort(), 120);
154 const e = await refusal(drain(t));
155 assert.equal(e.code, 'aborted');
156 assert.equal(e.what, undefined);
157});
159test('0 turns a timer off: a stalled body with idle 0 runs until the opt-in total', async () => {
160 const t = createTransport('https://h/r', { fetchImpl: answering(10, () => dripping(20, 40, 2)), limits: withLimits({ firstByteTimeoutMs: 150, idleTimeoutMs: 0, requestTimeoutMs: 400 }) }).value;
161 const t0 = Date.now();
162 const e = await refusal(drain(t));
163 assert.deepEqual([e.code, e.what], ['timeout', 'requestTimeoutMs']);
164 assert.ok(Date.now() - t0 >= 350);
165});
167test('exchangeClock: progress while waiting for headers does not extend the first-byte wait; cancel fires nothing', async () => {
168 let fired = null;
169 const c = exchangeClock({ firstByteMs: 80, idleMs: 1000, heldMs: 0, totalMs: 0 }, null, k => { fired = k; });
170 const iv = setInterval(() => c.feed('upload_bytes'), 10);
171 await sleep(200);
172 clearInterval(iv);
173 assert.equal(fired, 'firstByteTimeoutMs');
174 assert.equal(timeoutLimit(c.signal.reason), 'firstByteTimeoutMs');
175 assert.equal(c.signal.reason.name, 'TimeoutError');
176 const d = exchangeClock({ firstByteMs: 40, idleMs: 40, heldMs: 0, totalMs: 40 }, null, () => { fired = 'late'; });
177 d.cancel();
178 await sleep(100);
179 assert.equal(d.signal.aborted, false);
180 assert.notEqual(fired, 'late');
181});
183test('exchangeClock: idle progress re-arms; the body phase fires only after a real gap', async () => {
184 let fired = null;
185 const c = exchangeClock({ firstByteMs: 50, idleMs: 60, heldMs: 0, totalMs: 0 }, null, k => { fired = k; });
186 c.feed('headers');
187 for (let i = 0; i < 8; i++) { await sleep(30); c.feed('body_chunk'); }
188 assert.equal(fired, null);
189 await sleep(150);
190 assert.equal(fired, 'idleTimeoutMs');
191});
193test('watchedUpload: no buffer of its own, the source locked only from the first pull, cancel reaches the source', async () => {
194 const c = exchangeClock({ firstByteMs: 0, idleMs: 0, heldMs: 0, totalMs: 0 }, null, () => {});
195 let pulls = 0, cancelled = null;
196 const source = new ReadableStream({ pull(ctl) { pulls++; ctl.enqueue(new Uint8Array(1)); }, cancel(r) { cancelled = r; } }, { highWaterMark: 0 });
197 const w = watchedUpload(source, c);
198 await sleep(20);
199 assert.deepEqual([pulls, source.locked], [0, false]);
200 const reader = w.getReader();
201 await reader.read();
202 assert.equal(source.locked, true);
203 await reader.cancel('stop');
204 assert.equal(cancelled, 'stop');
205 const untouched = new ReadableStream({ cancel(r) { cancelled = `untouched:${r}`; } });
206 await watchedUpload(untouched, c).cancel('x');
207 assert.equal(cancelled, 'untouched:x');
208});
210// The machine, every state and event pair. Written by hand from the rules, not from the code:
211// - nothing moves a state backwards; an event a state has no use for leaves it as it is
212// - headers seen (from any state before them) is `streaming`: the first byte is satisfied for good
213// - `restart` (the wait starts again) only on network progress: the request sent again, upload bytes while uploading
214// or streaming, the upload's end before the headers, the headers, a body chunk, the consumer asking for more
215// - consumer_took parks the clock (`held`) without a restart: the hold runs from the last network mark (#273 review);
216// only consumer_asked resumes it; `ended` and `done` are final
217const STATES = ['connecting', 'uploading', 'awaiting_first_byte', 'streaming', 'held', 'done'];
218const EVENTS = ['request_sent', 'upload_started', 'upload_bytes', 'upload_ended', 'headers', 'body_chunk', 'consumer_took', 'consumer_asked', 'ended'];
219// next state per event, in EVENTS order; a trailing `!` is a restart
220const TABLE = {
221 connecting: ['connecting!', 'uploading!', 'connecting', 'connecting', 'streaming!', 'connecting', 'connecting', 'connecting', 'done'],
222 uploading: ['uploading', 'uploading', 'uploading!', 'awaiting_first_byte!', 'streaming!', 'uploading', 'uploading', 'uploading', 'done'],
223 awaiting_first_byte: ['awaiting_first_byte', 'awaiting_first_byte', 'awaiting_first_byte', 'awaiting_first_byte', 'streaming!', 'awaiting_first_byte', 'awaiting_first_byte', 'awaiting_first_byte', 'done'],
224 streaming: ['streaming', 'streaming', 'streaming!', 'streaming', 'streaming', 'streaming!', 'held', 'streaming', 'done'],
225 held: ['held', 'held', 'held', 'held', 'held', 'held', 'held', 'streaming!', 'done'],
226 done: ['done', 'done', 'done', 'done', 'done', 'done', 'done', 'done', 'done'],
227};
228test('transition: every state and event pair', () => {
229 assert.deepEqual(Object.keys(TABLE), STATES);
230 for (const state of STATES) {
231 assert.equal(TABLE[state].length, EVENTS.length);
232 EVENTS.forEach((event, i) => {
233 const want = TABLE[state][i];
234 assert.deepEqual(transition(state, event), { state: want.replace('!', ''), restart: want.endsWith('!') }, `${state} + ${event}`);
235 });
236 }
237});
239// A state outside ClockState (a new one added without its arm, or a value from nowhere) is a bug, not `held` (#276):
240// the switch is exhaustive and its default throws, whatever the event.
241test('transition: a state the machine does not know throws instead of taking another state\'s semantics', () => {
242 for (const event of EVENTS.filter(e => e !== 'ended')) assert.throws(() => transition('paused', event), /unreachable: paused/u, event);
243});
245test('transition: no event moves a state backwards', () => {
246 // the order of an exchange; `streaming` and `held` alternate, so they share a rank
247 const rank = { connecting: 0, uploading: 1, awaiting_first_byte: 2, streaming: 3, held: 3, done: 4 };
248 for (const state of STATES) for (const event of EVENTS) assert.ok(rank[transition(state, event).state] >= rank[state], `${state} + ${event}`);
249});
251test('deadlineOf: first byte before the headers, idle while on the network, nothing while held, the total throughout', () => {
252 const t = { firstByteMs: 100, idleMs: 20, heldMs: 0, totalMs: 0 };
253 // mark 1000, start 400: the step deadline is mark + its limit
254 const at = state => { const d = deadlineOf(state, 1000, 400, t); return d === null ? null : [d.key, d.at, d.ms]; };
255 assert.deepEqual(STATES.map(at), [['firstByteTimeoutMs', 1100, 100], ['idleTimeoutMs', 1020, 20], ['firstByteTimeoutMs', 1100, 100], ['idleTimeoutMs', 1020, 20], null, null]);
256 // an opt-in total of 650 from start 400 is 1050: earlier than the first-byte wait, later than the idle one, and the
257 // only limit while the consumer holds a chunk
258 const capped = state => { const d = deadlineOf(state, 1000, 400, { ...t, totalMs: 650 }); return d === null ? null : [d.key, d.at]; };
259 assert.deepEqual(STATES.map(capped), [['requestTimeoutMs', 1050], ['idleTimeoutMs', 1020], ['requestTimeoutMs', 1050], ['idleTimeoutMs', 1020], ['requestTimeoutMs', 1050], null]);
260 // 0 turns a limit off
261 assert.equal(deadlineOf('streaming', 1000, 400, { firstByteMs: 100, idleMs: 0, heldMs: 0, totalMs: 0 }), null);
262 // #273: while held, the hold runs from the take (the mark): mark + held, unless the total is earlier
263 assert.deepEqual(capped('held') && [deadlineOf('held', 1000, 400, { ...t, heldMs: 30 }).key, deadlineOf('held', 1000, 400, { ...t, heldMs: 30 }).at], ['heldTimeoutMs', 1030]);
264 assert.equal(deadlineOf('held', 1000, 400, { ...t, heldMs: 30, totalMs: 620 }).key, 'requestTimeoutMs');
265});
267// The hot loop (#255 review, R2). One timer is left pending: past the headers a deadline only moves later, so the
268// pending timer is early enough and re-arms for what is left when it fires. The expected count is the rule's: zero
269// timer calls per chunk (main ec337c5 made none either), whatever the limits.
270const countingTimers = async run => {
271 const realSet = globalThis.setTimeout, realClear = globalThis.clearTimeout;
272 const calls = { set: 0, clear: 0 };
273 globalThis.setTimeout = (...a) => { calls.set++; return realSet(...a); };
274 globalThis.clearTimeout = (...a) => { calls.clear++; return realClear(...a); };
275 try { await run(calls); } finally { globalThis.setTimeout = realSet; globalThis.clearTimeout = realClear; }
276};
277test('a consumer that keeps up costs no setTimeout and no clearTimeout per body chunk', async () => {
278 await countingTimers(calls => {
279 for (const totalMs of [0, 120_000]) {
280 const c = exchangeClock({ firstByteMs: 60_000, idleMs: 60_000, heldMs: 240_000, totalMs }, null, () => {});
281 c.feed('headers');
282 calls.set = 0; calls.clear = 0;
283 for (let i = 0; i < 1000; i++) { c.feed('body_chunk'); c.feed('consumer_took'); c.feed('consumer_asked'); }
284 assert.deepEqual([totalMs, calls.set, calls.clear], [totalMs, 0, 0]);
285 c.cancel();
286 }
287 });
288});
289test('frames() makes the same number of timer calls for 10 body chunks as for 1000', async () => {
290 const fetchOf = n => () => { let i = 0; return Promise.resolve(new Response(new ReadableStream({ pull(c) { if (i++ === n) { c.enqueue(te.encode('0000')); c.close(); return; } c.enqueue(te.encode('0008abcd')); } }, { highWaterMark: 0 }), { status: 200, headers: RESULT })); };
291 const timerCalls = async n => {
292 const seen = { set: 0, clear: 0, frames: 0 };
293 await countingTimers(async calls => {
294 const t = createTransport('https://h/r', { fetchImpl: fetchOf(n) }).value;
295 for await (const _ of t.frames('git-upload-pack', new Uint8Array(4), spanOf(), { bypassCache: false })) seen.frames++;
296 seen.set = calls.set; seen.clear = calls.clear;
297 });
298 return seen;
299 };
300 const few = await timerCalls(10), many = await timerCalls(1000);
301 assert.deepEqual([few.frames, many.frames], [11, 1001]);
302 assert.deepEqual([many.set, many.clear], [few.set, few.clear]);
303});
305// A manual scheduler (#255 re-review, REQ 1): setTimeout, clearTimeout and Date.now are replaced and time moves only in
306// `advance`, so nothing sleeps and every fire time is exact.
307const manualTimers = async run => {
308 const realSet = globalThis.setTimeout, realClear = globalThis.clearTimeout, realNow = Date.now;
309 let T = 0;
310 const q = new Map();
311 globalThis.setTimeout = (fn, ms) => { const id = { unref() {} }; q.set(id, { at: T + (ms ?? 0), fn }); return id; };
312 globalThis.clearTimeout = id => { q.delete(id); };
313 Date.now = () => T;
314 // run every timer due by `to`, earliest first, each at its own time
315 const advance = to => {
316 for (;;) {
317 let best = null;
318 for (const e of q) if (e[1].at <= to && (best === null || e[1].at < best[1].at)) best = e;
319 if (best === null) break;
320 q.delete(best[0]); T = Math.max(T, best[1].at); best[1].fn();
321 }
322 T = to;
323 };
324 try { await run({ advance, time: () => T, pending: () => q.size }); } finally { globalThis.setTimeout = realSet; globalThis.clearTimeout = realClear; Date.now = realNow; }
325};
326// Expected times are the documented rule worked by hand, not read from the clock: idleTimeoutMs is the longest wait on
327// the network with no progress, and the wait starts again when the consumer asks. So after an ask at A with no further
328// bytes the cut is at A + idle, named idleTimeoutMs, unless start + requestTimeoutMs is earlier; a consumer that never
329// asks is cut only at start + requestTimeoutMs.
330test('exchangeClock: with requestTimeoutMs set, the idle limit survives a timer that fires while the consumer holds a chunk', async () => {
331 await manualTimers(({ advance, time }) => {
332 const run = (t, script) => {
333 const t0 = time();
334 let fired = 'none';
335 const c = exchangeClock(t, null, key => { fired = `${key}@${time() - t0}`; });
336 script(c, ms => { advance(t0 + ms); });
337 c.cancel();
338 return fired;
339 };
340 const capped = { firstByteMs: 1000, idleMs: 1000, heldMs: 0, totalMs: 100_000 };
341 // headers at 10, a chunk taken at 20; the timer set for 10 + 1000 fires while the consumer holds; ask at 1500, then
342 // silence: 1500 + 1000
343 assert.equal(run(capped, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('body_chunk'); c.feed('consumer_took'); at(1500); c.feed('consumer_asked'); at(200_000); }), 'idleTimeoutMs@2500');
344 // five holds of 1490 ms, each across a firing, each followed by 10 ms on the network; the last ask is at
345 // 10 + 4 * 1500 + 1490 = 7500, then silence: 7500 + 1000
346 assert.equal(run(capped, (c, at) => {
347 at(10); c.feed('headers');
348 for (let i = 0; i < 5; i++) { c.feed('consumer_took'); at(10 + i * 1500 + 1490); c.feed('consumer_asked'); at(10 + (i + 1) * 1500); }
349 at(200_000);
350 }), 'idleTimeoutMs@8500');
351 // the total cap is earlier than ask + idle (1500 + 1000): the cap fires, at its own time
352 assert.equal(run({ ...capped, totalMs: 2000 }, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('consumer_took'); at(1500); c.feed('consumer_asked'); at(200_000); }), 'requestTimeoutMs@2000');
353 // a consumer that never asks again is not charged idle time: only the total cap bounds it
354 assert.equal(run({ ...capped, totalMs: 5000 }, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('consumer_took'); at(200_000); }), 'requestTimeoutMs@5000');
355 });
356});
357// What README Limits and SAFETY.md I1 say about a held chunk (re-review REQ 3): a timer may still be pending, but with
358// no total cap it cuts nothing, and once it has fired it is not set again until the consumer asks.
359test('exchangeClock: with heldTimeoutMs 0, while the consumer holds a chunk a timer may be pending, and without requestTimeoutMs it cuts nothing', async () => {
360 await manualTimers(({ advance, pending }) => {
361 let fired = 'none';
362 const c = exchangeClock({ firstByteMs: 60_000, idleMs: 60_000, heldMs: 0, totalMs: 0 }, null, key => { fired = key; });
363 advance(10); c.feed('headers'); advance(20); c.feed('body_chunk'); c.feed('consumer_took');
364 // the one timer, set before the consumer took the chunk
365 assert.equal(pending(), 1);
366 // a day later the consumer still holds the chunk: nothing was cut, and the timer that fired was not set again
367 advance(86_400_000);
368 assert.deepEqual([fired, c.signal.aborted, pending()], ['none', false, 0]);
369 // the ask puts ply back on the network: the idle wait runs from here (86 400 000 + 60 000)
370 c.feed('consumer_asked');
371 assert.equal(pending(), 1);
372 advance(86_459_999);
373 assert.equal(fired, 'none');
374 advance(86_460_000);
375 assert.equal(fired, 'idleTimeoutMs');
376 });
377});
378test('frames(): with requestTimeoutMs set, a server that stalls after the consumer held a frame across a timer firing is cut at ask + idle', async () => {
379 await manualTimers(async ({ advance, time }) => {
380 // the server: one frame at once, then silence
381 let pulls = 0;
382 const body = new ReadableStream({ pull(c) { if (pulls++ === 0) { c.enqueue(te.encode('0008abcd')); return undefined; } return new Promise(() => {}); } }, { highWaterMark: 0 });
383 const t = createTransport('https://h/r', { fetchImpl: () => Promise.resolve(new Response(body, { status: 200, headers: RESULT })), limits: withLimits({ firstByteTimeoutMs: 300, idleTimeoutMs: 300, requestTimeoutMs: 6000 }) }).value;
384 let firedAt = null;
385 const attrs = {};
386 const span = { attrs, set: a => { if ('git.timeout.limit' in a) firedAt = time(); Object.assign(attrs, a); }, event() {}, error() {} };
387 const frames = t.frames('git-upload-pack', new Uint8Array(4), span, { bypassCache: false })[Symbol.asyncIterator]();
388 // everything up to the first frame happens at time 0; the consumer then holds it for 700 ms, across the timer set
389 // for 0 + 300
390 assert.equal((await frames.next()).done, false);
391 advance(700);
392 assert.equal(firedAt, null, 'the consumer is not charged idle time');
393 // the ask, at 700: the server has 300 ms from here, so the cut is at 1000 (the total cap is 6000)
394 const next = refusal(frames.next());
395 await new Promise(r => setImmediate(r));
396 advance(999);
397 assert.equal(firedAt, null);
398 advance(200_000);
399 const e = await next;
400 assert.deepEqual([firedAt, e.code, e.what, attrs['git.timeout.limit']], [1000, 'timeout', 'idleTimeoutMs', 'idleTimeoutMs']);
401 });
402});
403// #273 (option B): a consumer that holds a chunk is bounded by limits.heldTimeoutMs, apart from network idle. Expected
404// values are the issue's contract worked by hand: the held wait starts when the consumer takes the chunk (time 0 here)
405// and the cut is at take + heldTimeoutMs, named heldTimeoutMs, with the upstream body cancelled; a hold just under it
406// is not cut; while held, idleTimeoutMs (300, shorter than the hold) is never charged.
407test('defaults: heldTimeoutMs is 4 x the idle default (README Limits)', () => {
408 assert.equal(DEFAULT_LIMITS.heldTimeoutMs, 240_000);
409});
410test('frames(): a consumer that holds a chunk past heldTimeoutMs is cut as timeout/heldTimeoutMs and the upstream is cancelled; just under is not cut', async () => {
411 await manualTimers(async ({ advance, time }) => {
412 const exchange = () => {
413 let pulls = 0, cancelled = null;
414 const body = new ReadableStream({ pull(c) { pulls++; c.enqueue(te.encode('0008abcd')); }, cancel(r) { cancelled = r ?? 'cancelled'; } }, { highWaterMark: 0 });
415 const t = createTransport('https://h/r', { fetchImpl: () => Promise.resolve(new Response(body, { status: 200, headers: RESULT })), limits: withLimits({ firstByteTimeoutMs: 300, idleTimeoutMs: 300, heldTimeoutMs: 1000 }) }).value;
416 const attrs = {};
417 let firedAt = null;
418 const span = { attrs, set: a => { if ('git.timeout.limit' in a) firedAt = time(); Object.assign(attrs, a); }, event() {}, error() {} };
419 return { frames: t.frames('git-upload-pack', new Uint8Array(4), span, { bypassCache: false })[Symbol.asyncIterator](), attrs, fired: () => firedAt, cancelled: () => cancelled, pulls: () => pulls };
420 };
421 // held just under: 999 ms (three idle limits long), then the ask is served: not cut
422 const a = exchange();
423 const t0 = time();
424 assert.equal((await a.frames.next()).done, false);
425 advance(t0 + 999);
426 assert.equal(a.fired(), null, 'network idle is not charged while held, and 999 < heldTimeoutMs');
427 assert.equal((await a.frames.next()).done, false);
428 assert.equal(a.fired(), null);
429 await a.frames.return();
430 // held past it. The take reads no clock: the hold starts at the first clock read that sees it, here the idle timer
431 // pending since the headers (t1 + 300), so the cut is at t1 + 300 + 1000: never before take + 1000, late by at
432 // most one idle interval (README Limits)
433 const b = exchange();
434 const t1 = time();
435 assert.equal((await b.frames.next()).done, false);
436 advance(t1 + 1299);
437 assert.equal(b.fired(), null);
438 advance(t1 + 1300);
439 assert.equal(b.fired(), t1 + 1300);
440 await new Promise(r => setImmediate(r));
441 assert.notEqual(b.cancelled(), null, 'the upstream body is cancelled when the hold is cut');
442 const e = await refusal(b.frames.next());
443 assert.deepEqual([e.code, e.what, b.attrs['git.timeout.limit']], ['timeout', 'heldTimeoutMs', 'heldTimeoutMs']);
444 // the span names the hold limit beside its siblings (README Limits), and the message says what was held and for how long
445 assert.equal(b.attrs['git.limits.held_timeout_ms'], 1000);
446 assert.match(e.message, /held a chunk for 1000 ms .*limits\.heldTimeoutMs/);
447 });
448});
449test('exchangeClock: heldTimeoutMs runs from the first clock read after the take and stops at consumer_asked; 0 turns it off', async () => {
450 await manualTimers(({ advance, time }) => {
451 const run = (t, script) => {
452 const t0 = time();
453 let fired = 'none';
454 const c = exchangeClock(t, null, key => { fired = `${key}@${time() - t0}`; });
455 script(c, ms => { advance(t0 + ms); });
456 c.cancel();
457 return fired;
458 };
459 const lim = { firstByteMs: 1000, idleMs: 1000, heldMs: 4000, totalMs: 0 };
460 // took at 20, never asks. The take reads no clock; the first read after it is the first-byte timer armed at 0 for
461 // 1000 (still pending: the headers' idle deadline 1010 is later), so the hold runs from 1000: 1000 + 4000
462 assert.equal(run(lim, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('body_chunk'); c.feed('consumer_took'); at(200_000); }), 'heldTimeoutMs@5000');
463 // held 3990 each time, five times, the network answering at once: never cut; then silence after the last ask at
464 // 10 + 5 * 3990 = 19960: idle at 19960 + 1000
465 assert.equal(run(lim, (c, at) => {
466 at(10); c.feed('headers');
467 for (let i = 1; i <= 5; i++) { c.feed('body_chunk'); c.feed('consumer_took'); at(10 + i * 3990); c.feed('consumer_asked'); }
468 at(200_000);
469 }), 'idleTimeoutMs@20960');
470 // held shorter than idle: the pending timer (1000) is later than mark + held (10 + 200), so the take arms a new
471 // one, and that arm's clock read is the take's own time: 20 + 200, exactly on time
472 assert.equal(run({ ...lim, heldMs: 200 }, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('consumer_took'); at(200_000); }), 'heldTimeoutMs@220');
473 // idle off: the first-byte timer armed at 0 for 1000 is still pending at the take and due before 10 + 4000, so its
474 // fire starts the hold: 1000 + 4000 (late by 980 ms, under one held interval)
475 assert.equal(run({ ...lim, idleMs: 0 }, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('consumer_took'); at(200_000); }), 'heldTimeoutMs@5000');
476 // 0 turns it off: only the total cap bounds the hold
477 assert.equal(run({ ...lim, heldMs: 0, totalMs: 9000 }, (c, at) => { at(10); c.feed('headers'); at(20); c.feed('consumer_took'); at(200_000); }), 'requestTimeoutMs@9000');
478 });
479});
480// #273 review (owner decision): the hold adds no clock read per chunk. A chunk's cycle in frames() is consumer_took then
481// consumer_asked; on main (8e3939db) that is one Date.now (the ask's restart), and it must stay one: the take reads no
482// clock, and the hold runs from the first clock read after it (a timer firing, or an arm), not from a new read at the take.
483test('exchangeClock: a chunk (consumer_took + consumer_asked) reads the clock once, as on main', () => {
484 let reads = 0;
485 const c = exchangeClock({ firstByteMs: 60_000, idleMs: 60_000, heldMs: 240_000, totalMs: 0 }, null, () => {}, () => { reads++; return 0; });
486 c.feed('headers');
487 const before = reads;
488 for (let i = 0; i < 1000; i++) { c.feed('consumer_took'); c.feed('consumer_asked'); }
489 c.cancel();
490 assert.equal(reads - before, 1000);
491});
492// #273 review round 3 (reproduced at 626a4c7d, passes on main 8e3939db): a chunk whose network delivery took
493// heldTimeoutMs or longer must not be cut the moment it is taken. idle off, and idle (600 s) above held (240 s): headers
494// at 10, the ask at 20, the network silent 300 s, the chunk taken at 300 020 and held 5 ms. The README contract: a hold
495// is never cut before take + heldTimeoutMs.
496for (const idleMs of [0, 600_000]) test(`exchangeClock: idle ${idleMs}, a 5 ms hold after a 300 s network wait is not cut as heldTimeoutMs`, async () => {
497 await manualTimers(({ advance, time }) => {
498 const t0 = time();
499 let fired = 'none';
500 const c = exchangeClock({ firstByteMs: 60_000, idleMs, heldMs: 240_000, totalMs: 0 }, null, k => { fired = `${k}@${time() - t0}`; });
501 const at = ms => { advance(t0 + ms); };
502 at(10); c.feed('headers'); c.feed('consumer_took'); at(20); c.feed('consumer_asked');
503 at(300_020); c.feed('consumer_took'); at(300_025); c.feed('consumer_asked'); at(300_030);
504 assert.equal(fired, 'none');
505 // and a hold past it is still cut, no earlier than take + 240 000
506 c.feed('consumer_took'); at(300_030 + 239_999);
507 assert.equal(fired, 'none');
508 at(300_030 + 240_000 + Math.min(idleMs || 240_000, 240_000));
509 assert.equal(fired.startsWith('heldTimeoutMs@'), true, fired);
510 c.cancel();
511 });
512});
Diagnostics6 git calls · 196 ms
CallStatusSizeTime0196 ms
discover200191 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=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB · 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=11990 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 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=4347 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 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=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · 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=919 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2008.6 KiB186 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=186 git.response.bytes=8788 git.response.cache=MISS git.response.duration_ms=186 git.fetch.wants=0e3a54ceaccdff8a3b3c44366b60e4bc252e2392 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=5 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=8788 git.pkt.payload.max=8192 git.sideband.band1.bytes=8756 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://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e17faf385a3014
- Request
- 6a486ce6…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format