GitHubivarvong

ply

main
ply6 git calls90 ms21 KiB3 cachedprotocol v2
74 lines4.4 KiBRaw
1// v2 negotiation: send haves in rounds without `done` until the server says ready (or we run out), then done.
2// Returns the final fetchPack result plus the rounds' acks. `external` supplies thin-pack bases.
3import type { Transport } from './transport.js';
4import type { FetchOpts } from './fetch.js';
5import type { FetchPackExtra, FetchPackResult } from './fetchpack.js';
6import type { ErrorOf, PlyError } from './errors.js';
7import type { Input } from './absent.js';
8import type { Result } from './result.js';
9import { fail, remoteQuoted } from './errors.js';
10import { checkLimit } from './limits.js';
11import { fetchPack } from './fetchpack.js';
12import { fetchRequest } from './fetch.js';
13import { parseOid } from './oid.js';
14import type { Oid } from './oid.js';
15import { getOrElse, ok } from './result.js';
16import { fromNullable, withDefaults } from './absent.js';
18const NEGOTIATE_BATCH = 16;
19const NEGOTIATE_ROUNDS = 32;
20// characters of a bad acknowledgment quoted in the refusal
21const ACK_SHOWN = 60;
23// The fetch arguments, plus the haves to offer in rounds of `batch` (at most `maxRounds` rounds).
24export type NegotiateConfig = { readonly batch: number; readonly maxRounds: number };
25export type NegotiateOpts = Omit<FetchOpts, 'done'> & Input<NegotiateConfig>;
26export type Round = { readonly sent: number; readonly acks: readonly string[]; readonly nak: boolean; readonly ready: boolean; readonly pack: boolean };
27export type NegotiateResult = FetchPackResult & { readonly rounds: Round[]; readonly common: string[] };
29// The round count and batch size, checked before any request: finite non-negative numbers, the batch at least one.
30type Caps = { readonly rounds: number; readonly batch: number };
31function negotiateCaps(maxRounds: unknown, batch: unknown): Result<Caps, ErrorOf<'bad_request'>> {
32 const rounds = checkLimit(maxRounds, 'maxNegotiateRounds');
33 if (!rounds.ok) return rounds;
34 const size = checkLimit(batch, 'negotiateBatch');
35 if (!size.ok) return size;
36 if (!Number.isInteger(rounds.value)) return fail('bad_request', 'negotiate rounds must be an integer', { what: 'maxNegotiateRounds' });
37 if (!Number.isInteger(size.value) || size.value < 1) return fail('bad_request', 'negotiate batch must be a positive integer', { what: 'negotiateBatch' });
38 return ok({ rounds: rounds.value, batch: size.value });
41export async function negotiate(t: Transport, opts: NegotiateOpts, extra: Pick<FetchPackExtra, 'external' | 'resolve'> = {}): Promise<Result<NegotiateResult, PlyError>> {
42 const { wants, haves: givenHaves, batch: _batch, maxRounds: _maxRounds, ...rest } = opts;
43 const { batch, maxRounds } = withDefaults(opts, { batch: NEGOTIATE_BATCH, maxRounds: NEGOTIATE_ROUNDS });
44 const { external, resolve } = extra;
45 const checked = negotiateCaps(maxRounds, batch);
46 if (!checked.ok) return checked;
47 const caps = checked.value;
48 // The whole command parsed once, every have with it (#156): a bad one is refused before the first round, not in the
49 // round that would have sent it. The rounds send the parsed ids.
50 const parsed = fetchRequest({ ...rest, wants, haves: getOrElse(fromNullable(givenHaves), []) });
51 if (!parsed.ok) return parsed;
52 const { haves } = parsed.value;
53 // what the server acknowledged: each an id, offered again as a have in every later round
54 const acked = new Set<Oid>();
55 const rounds: Round[] = [];
56 let i = 0, ready = false;
57 for (let round = 0; round < caps.rounds && i < haves.length && !ready; round++) {
58 const sent = haves.slice(i, i + caps.batch); i += caps.batch;
59 // oxlint-disable-next-line no-await-in-loop -- each round's haves depend on the last round's acks
60 const fetched = await fetchPack(t, { ...rest, wants, haves: [...acked, ...sent], done: false }, { external, resolve });
61 if (!fetched.ok) return fetched;
62 const r = fetched.value;
63 for (const a of r.acks) {
64 const id = parseOid(a);
65 if (!id.ok) return fail('protocol', `negotiate: the server acknowledged ${remoteQuoted(a, ACK_SHOWN)}, not an object id`);
66 acked.add(id.value);
67 }
68 rounds.push({ sent: sent.length, acks: r.acks, nak: r.nak, ready: r.ready, pack: Boolean(r.parsed) });
69 ready = r.ready;
70 if (r.parsed) return ok({ ...r, rounds, common: [...acked] }); // server went straight to packfile
71 }
72 const r = await fetchPack(t, { ...rest, wants, haves: [...acked], done: true }, { external, resolve });
73 return r.ok ? ok({ ...r.value, rounds, common: [...acked] }) : r;
Diagnostics6 git calls · 90 ms
CallStatusSizeTime090 ms
discover200191 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
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 KiB3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=11990
git.response.cache=UPDATING
git.response.duration_ms=3
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=2
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=10
Open pack map
fetchtree:0200919 B · hit2 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=2
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=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02002.4 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=79
git.response.bytes=2507
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=8f720d29ff85ce682ffdad9c393b04a3834c3203
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=2507
git.pkt.payload.max=2480
git.sideband.band1.bytes=2480
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=2480
git.pack.trailer=723b6bf1d4d6021fe5eb940c622b9ff00ee4a381
Open pack map
fetch2001.8 KiB79 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1880
git.response.cache=MISS
git.response.duration_ms=79
git.fetch.wants=7cc18509235e07332af0e94f534043bd806c737e
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=1880
git.pkt.payload.max=1853
git.sideband.band1.bytes=1853
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
a5e2dd782906c125
Request
b4b233ac…
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