main
ply4 git calls23 ms42 KiB3 cachedprotocol v2
80 lines4.1 KiBRaw
1// In-Worker reads. isEnabled is false when the flag is missing, the store is down, or the variant is not boolean true.
2import { parseFlagKey } from './brand.ts';
3import { loadRuleset, type FlagCacheEnv, type FlagsCacheBinding } from './cache.ts';
4import { elapsed, type Clock } from './clock.ts';
5import { parseUnit, type AttributeInput } from './document.ts';
6import { evaluateDocument } from './eval.ts';
7import { chainGet, dig, fromNullable } from '../absent.ts';
8import { some } from '../option.ts';
9import { emitEval, endSpan, openSpan, resolutionSample, type TracingLike } from './telemetry.ts';
10import { USAGE_NOTE_FLAG, USAGE_NOTE_LINE } from './ttl.ts';
11import { EMPTY_DOCUMENT, isEnabledVariant, type CacheStatus, type FlagEvaluation, type FlagSelection, type Unit } from './types.ts';
12import type { HeaderField } from '../http/inbound.ts';
14export type FlagRuntime = {
15 env: FlagCacheEnv;
16 cache: FlagsCacheBinding | null;
17 tracing: TracingLike | null;
18};
20/** A missing context, export, cache, or tracer is null. A present value stays, including a falsy one. */
21export function flagRuntime(env: FlagCacheEnv, ctx: unknown): FlagRuntime {
22 const host = fromNullable(ctx);
23 if (host.tag === 'none') return { env, cache: null, tracing: null };
24 const cache = dig(host.value, ['exports', 'FlagsCache']);
25 const tracing = chainGet(host.value, 'tracing');
26 return {
27 env,
28 cache: cache.tag === 'some' ? cache.value as FlagsCacheBinding : null,
29 tracing: tracing.tag === 'some' ? tracing.value as TracingLike : null,
30 };
33export type ResolveResult =
34 | { readonly kind: 'ok'; readonly evaluations: readonly FlagEvaluation[]; readonly revision: string | null; readonly cache: CacheStatus }
35 | { readonly kind: 'unavailable'; readonly reason: string };
37export async function resolveFlags(runtime: FlagRuntime, unit: Unit, keys: FlagSelection, clock: Clock = () => performance.now()): Promise<ResolveResult> {
38 // Open the span before the await so its platform duration includes the cache/R2 yield.
39 // The hash itself does not move the Workers clock; resolve_ms is this await, not the hash.
40 const span = openSpan(runtime.tracing, 'flags.eval');
41 try {
42 const timed = await elapsed(clock, () => loadRuleset(runtime.env, runtime.cache));
43 const loaded = timed.value;
44 if (loaded.kind === 'unavailable') return { kind: 'unavailable', reason: loaded.reason };
45 const document = loaded.kind === 'ready' ? loaded.document : EMPTY_DOCUMENT;
46 const evalStarted = clock();
47 const evaluations = evaluateDocument(document, unit, keys);
48 const evalMs = clock() - evalStarted;
49 emitEval(runtime.tracing, {
50 cacheStatus: loaded.cache,
51 revision: loaded.kind === 'ready' ? loaded.revision : null,
52 evalMs,
53 resolveMs: timed.ms,
54 r2ReadMs: loaded.r2ReadMs,
55 unitId: unit.id,
56 resolutions: evaluations.map(resolutionSample),
57 }, line => console.log(line), some(span));
58 return { kind: 'ok', evaluations, revision: loaded.kind === 'ready' ? loaded.revision : null, cache: loaded.cache };
59 } finally {
60 endSpan(span);
61 }
64export async function isEnabled(env: FlagCacheEnv, ctx: unknown, key: string, unit: { readonly id: string; readonly attributes: AttributeInput }): Promise<boolean> {
65 if (!env.FLAGS) return false;
66 const parsedKey = parseFlagKey(key);
67 if (parsedKey.kind !== 'key') return false;
68 const parsed = parseUnit(unit.id, unit.attributes);
69 if (parsed.kind === 'invalid') return false;
70 const resolved = await resolveFlags(flagRuntime(env, ctx), { id: parsed.id, attributes: parsed.attributes }, { kind: 'listed', keys: [parsedKey.value] });
71 if (resolved.kind === 'unavailable') return false;
72 const evaluation = resolved.evaluations[0];
73 return evaluation ? isEnabledVariant(evaluation.variant) : false;
76export async function usageNote(env: FlagCacheEnv, ctx: unknown, unitHeader: HeaderField): Promise<string | null> {
77 const id = unitHeader.kind === 'present' && unitHeader.value.length > 0 ? unitHeader.value : 'anonymous';
78 const on = await isEnabled(env, ctx, USAGE_NOTE_FLAG, { id, attributes: { kind: 'absent' } });
79 return on ? USAGE_NOTE_LINE : null;
Diagnostics4 git calls · 23 ms
CallStatusSizeTime023 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
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 · hit3 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=145
git.response.cache=HIT
git.response.duration_ms=3
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=61
Open pack map
fetch2001.5 KiB · hit17 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=17
git.response.bytes=1568
git.response.cache=HIT
git.response.duration_ms=17
git.fetch.wants=62c1e63cf62865945fd61dda53fd6d690913be77
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=1568
git.pkt.payload.max=1547
git.sideband.band1.bytes=1546
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
a5e2dd5aeb32c125
Request
5f3cbeea…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format