Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
The app counts its own numbers: Pi round trip, camera fps in, viewers, frames dropped and bytes out, at /metrics.prom and a one-line log each minute #100
smith/arm:smith/88-the-app-counts-its-own-numbers-pi-round → main 1 commit fork
6 files
changed +287
−11
Software/so101/src/arm-link.ts +6 −1
| @@ -59,6 +59,8 @@ export interface ArmLinkOptions { | ||
| 59 | 59 | onChange?: (state: LinkState, previous: LinkState) => void; |
| 60 | 60 | /** Called with every status that comes in. */ |
| 61 | 61 | onStatus?: (status: Status) => void; |
| 62 | /** Called with every round trip measured to the arm server, ms. */ | |
| 63 | onRtt?: (ms: number) => void; | |
| 62 | 64 | /** Called with the arm server's machine log as it comes in: entries checked, and the id of the server's run. */ |
| 63 | 65 | onMachine?: (entries: MachineEntry[], run: string) => void; |
| 64 | 66 | } |
| @@ -166,7 +168,9 @@ export class ArmLink { | ||
| 166 | 168 | ws.ping(); |
| 167 | 169 | }, this.t.pingEveryMs); |
| 168 | 170 | ws.on("pong", () => { |
| 169 | if (pingedAt) this.rttMs = Math.round((performance.now() - pingedAt) * 10) / 10; | |
| 171 | if (!pingedAt) return; | |
| 172 | this.rttMs = Math.round((performance.now() - pingedAt) * 10) / 10; | |
| 173 | this.o.onRtt?.(this.rttMs); | |
| 170 | 174 | }); |
| 171 | 175 | ws.on("message", (data) => { |
| 172 | 176 | const text = String(data); |
| @@ -194,6 +198,7 @@ export class ArmLink { | ||
| 194 | 198 | const asked = performance.now(); |
| 195 | 199 | const r = await fetch(`${this.o.remote.http}/status`, { signal: AbortSignal.timeout(this.t.connectTimeoutMs), cache: "no-store" }); |
| 196 | 200 | this.rttMs = Math.round((performance.now() - asked) * 10) / 10; |
| 201 | this.o.onRtt?.(this.rttMs); | |
| 197 | 202 | if (!r.ok) throw new Error(await whyNot(r, "/status")); |
| 198 | 203 | const body = parseStatus(await r.text()); |
| 199 | 204 | if (!body) throw new Error("/status did not answer an arm's status"); |
Software/so101/src/camera-feed.ts +6 −2
| @@ -26,6 +26,9 @@ export interface FeedOptions { | ||
| 26 | 26 | lingerMs?: number; |
| 27 | 27 | /** Bytes queued for one viewer past which it skips frames. */ |
| 28 | 28 | maxBacklog?: number; |
| 29 | /** Called for each frame that comes in from the source, and with the bytes written to a viewer. */ | |
| 30 | onFrame?: () => void; | |
| 31 | onBytes?: (bytes: number) => void; | |
| 29 | 32 | } |
| 30 | 33 | |
| 31 | 34 | export type FeedState = "idle" | "connecting" | "live" | "offline"; |
| @@ -65,7 +68,7 @@ export class CameraFeed { | ||
| 65 | 68 | private watchdog: ReturnType<typeof setInterval> | undefined; |
| 66 | 69 | private lastByte = 0; |
| 67 | 70 | private closed = false; |
| 68 | private readonly o: Required<FeedOptions>; | |
| 71 | private readonly o: Required<Omit<FeedOptions, "onFrame" | "onBytes">> & Pick<FeedOptions, "onFrame" | "onBytes">; | |
| 69 | 72 | private readonly source: string | null; |
| 70 | 73 | private readonly name: string; |
| 71 | 74 | |
| @@ -128,7 +131,7 @@ export class CameraFeed { | ||
| 128 | 131 | } |
| 129 | 132 | |
| 130 | 133 | private write(viewer: Viewer, bytes: Buffer) { |
| 131 | if (!viewer.res.destroyed && !viewer.res.writableEnded) viewer.res.write(bytes); | |
| 134 | if (!viewer.res.destroyed && !viewer.res.writableEnded) (viewer.res.write(bytes), this.o.onBytes?.(bytes.length)); | |
| 132 | 135 | } |
| 133 | 136 | |
| 134 | 137 | private whole(viewer: Viewer, type: string, body: Buffer) { |
| @@ -223,6 +226,7 @@ export class CameraFeed { | ||
| 223 | 226 | for (const v of this.viewers) if (v.inPart) this.write(v, bytes); |
| 224 | 227 | }, |
| 225 | 228 | end: () => { |
| 229 | this.o.onFrame?.(); | |
| 226 | 230 | if (this._state !== "live") { |
| 227 | 231 | if (this._state === "offline") console.log(`${this.name} ${this.source}: live`); |
| 228 | 232 | this._state = "live"; |
Software/so101/src/live-metrics.test.ts added +94
| @@ -0,0 +1,94 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { LiveMetrics, quantile } from "./live-metrics.ts"; | |
| 3 | ||
| 4 | /** Every sample line of Prometheus text: name, labels, value. */ | |
| 5 | function parse(text: string) { | |
| 6 | const line = /^([a-z_][a-z0-9_]*)(\{[a-z_]+="[^"]*"(?:,[a-z_]+="[^"]*")*\})? (-?\d+(?:\.\d+)?)$/; | |
| 7 | return text | |
| 8 | .trimEnd() | |
| 9 | .split("\n") | |
| 10 | .filter((l) => !l.startsWith("#")) | |
| 11 | .map((l) => { | |
| 12 | const m = line.exec(l); | |
| 13 | if (!m) throw new Error(`not a sample: ${l}`); | |
| 14 | return { name: m[1]!, labels: m[2] ?? "", value: Number(m[3]) }; | |
| 15 | }); | |
| 16 | } | |
| 17 | ||
| 18 | describe("live metrics", () => { | |
| 19 | test("quantile of nothing is null; of a list, the value at that rank", () => { | |
| 20 | expect(quantile([], 0.5)).toBeNull(); | |
| 21 | expect(quantile([1, 2, 3, 4], 0.5)).toBe(2); | |
| 22 | expect(quantile([1, 2, 3, 4], 0.95)).toBe(4); | |
| 23 | }); | |
| 24 | ||
| 25 | test("injected link delays show up in p95, and p50 stays at the usual", () => { | |
| 26 | const m = new LiveMetrics(); | |
| 27 | for (let i = 0; i < 95; i++) m.rtt(10); | |
| 28 | for (let i = 0; i < 5; i++) m.rtt(400); | |
| 29 | const s = m.snapshot(); | |
| 30 | expect(s.rttP50Ms).toBe(10); | |
| 31 | expect(s.rttP95Ms).toBe(10); | |
| 32 | m.rtt(400); | |
| 33 | m.rtt(400); | |
| 34 | expect(m.snapshot().rttP95Ms).toBe(400); | |
| 35 | expect(m.snapshot().rttP50Ms).toBe(10); | |
| 36 | }); | |
| 37 | ||
| 38 | test("a bad round trip is not a sample", () => { | |
| 39 | const m = new LiveMetrics(); | |
| 40 | m.rtt(NaN); | |
| 41 | m.rtt(-1); | |
| 42 | expect(m.snapshot().rttSamples).toBe(0); | |
| 43 | expect(m.snapshot().rttP95Ms).toBeNull(); | |
| 44 | }); | |
| 45 | ||
| 46 | test("camera frames per second are over the last ten seconds, and old ones age out", () => { | |
| 47 | let now = 1_000_000; | |
| 48 | const m = new LiveMetrics(() => now); | |
| 49 | for (let s = 0; s < 10; s++) { | |
| 50 | for (let i = 0; i < 20; i++) m.frame(); | |
| 51 | if (s < 9) now += 1000; | |
| 52 | } | |
| 53 | expect(m.snapshot().cameraFps).toBe(20); | |
| 54 | now += 20_000; | |
| 55 | expect(m.snapshot().cameraFps).toBe(0); | |
| 56 | expect(m.snapshot().cameraFramesTotal).toBe(200); | |
| 57 | }); | |
| 58 | ||
| 59 | test("viewers, commands and bytes are counted", () => { | |
| 60 | const m = new LiveMetrics(); | |
| 61 | m.viewer("follow", 1); | |
| 62 | m.viewer("camera", 1); | |
| 63 | m.viewer("camera", 1); | |
| 64 | m.viewer("camera", -1); | |
| 65 | m.viewer("live", -1); | |
| 66 | m.command(); | |
| 67 | m.command(); | |
| 68 | m.drop(); | |
| 69 | m.out(1500); | |
| 70 | const s = m.snapshot(); | |
| 71 | expect(s.viewers).toEqual({ follow: 1, live: 0, machine: 0, camera: 1 }); | |
| 72 | expect([s.followReceived, s.followDropped, s.bytesOut]).toEqual([2, 1, 1500]); | |
| 73 | }); | |
| 74 | ||
| 75 | test("the Prometheus text parses: a name, optional labels, a value on every line", () => { | |
| 76 | const m = new LiveMetrics(); | |
| 77 | m.rtt(12.5); | |
| 78 | m.viewer("machine", 1); | |
| 79 | m.out(7); | |
| 80 | const samples = parse(m.prometheus()); | |
| 81 | const get = (name: string, labels = "") => samples.find((x) => x.name === name && x.labels === labels)?.value; | |
| 82 | expect(get("arm_link_rtt_ms", '{quantile="0.95"}')).toBe(12.5); | |
| 83 | expect(get("viewers", '{kind="machine"}')).toBe(1); | |
| 84 | expect(get("viewer_bytes_sent_total")).toBe(7); | |
| 85 | expect(new LiveMetrics().prometheus()).not.toContain("arm_link_rtt_ms{"); | |
| 86 | }); | |
| 87 | ||
| 88 | test("the summary is one line", () => { | |
| 89 | const m = new LiveMetrics(); | |
| 90 | m.rtt(5); | |
| 91 | expect(m.summary()).not.toContain("\n"); | |
| 92 | expect(m.summary()).toContain("p95=5ms"); | |
| 93 | }); | |
| 94 | }); |
Software/so101/src/live-metrics.ts added +140
| @@ -0,0 +1,140 @@ | ||
| 1 | /** | |
| 2 | * The relay's own numbers, counted as it runs: the arm link's round trip, camera frames in, viewers per kind of socket, | |
| 3 | * /follow commands received and dropped, and bytes sent to viewers. Plain data in, plain data out: the hot paths only | |
| 4 | * bump a number (no allocation per frame); percentiles are worked out when someone asks. | |
| 5 | * | |
| 6 | * `prometheus()` is the text format for GET /metrics.prom; `summary()` the one line the relay logs each minute. | |
| 7 | */ | |
| 8 | ||
| 9 | const RTT_SAMPLES = 512; | |
| 10 | const RATE_SECONDS = 10; | |
| 11 | ||
| 12 | export type ViewerKind = "follow" | "live" | "machine" | "camera"; | |
| 13 | export const VIEWER_KINDS: readonly ViewerKind[] = ["follow", "live", "machine", "camera"]; | |
| 14 | ||
| 15 | export interface LiveSnapshot { | |
| 16 | rttP50Ms: number | null; | |
| 17 | rttP95Ms: number | null; | |
| 18 | rttSamples: number; | |
| 19 | cameraFramesTotal: number; | |
| 20 | /** Camera frames per second over the last ten seconds. */ | |
| 21 | cameraFps: number; | |
| 22 | viewers: Record<ViewerKind, number>; | |
| 23 | followReceived: number; | |
| 24 | followDropped: number; | |
| 25 | bytesOut: number; | |
| 26 | } | |
| 27 | ||
| 28 | /** The value at fraction `q` (0-1) of an ascending list; null when it is empty. */ | |
| 29 | export function quantile(sorted: ArrayLike<number>, q: number): number | null { | |
| 30 | if (!sorted.length) return null; | |
| 31 | return sorted[Math.min(sorted.length - 1, Math.ceil(q * sorted.length) - 1)]!; | |
| 32 | } | |
| 33 | ||
| 34 | export class LiveMetrics { | |
| 35 | private readonly rtts = new Float64Array(RTT_SAMPLES); | |
| 36 | private rttCount = 0; | |
| 37 | private rttNext = 0; | |
| 38 | private frames = 0; | |
| 39 | private readonly perSecond = new Uint32Array(RATE_SECONDS); | |
| 40 | private second = 0; | |
| 41 | private readonly viewerCount: Record<ViewerKind, number> = { follow: 0, live: 0, machine: 0, camera: 0 }; | |
| 42 | private received = 0; | |
| 43 | private dropped = 0; | |
| 44 | private bytes = 0; | |
| 45 | private readonly now: () => number; | |
| 46 | ||
| 47 | constructor(now: () => number = Date.now) { | |
| 48 | this.now = now; | |
| 49 | this.second = Math.floor(now() / 1000); | |
| 50 | } | |
| 51 | ||
| 52 | /** A round trip to the arm server, ms. */ | |
| 53 | rtt(ms: number) { | |
| 54 | if (!Number.isFinite(ms) || ms < 0) return; | |
| 55 | this.rtts[this.rttNext] = ms; | |
| 56 | this.rttNext = (this.rttNext + 1) % RTT_SAMPLES; | |
| 57 | if (this.rttCount < RTT_SAMPLES) this.rttCount++; | |
| 58 | } | |
| 59 | ||
| 60 | /** One camera frame came in from a source. */ | |
| 61 | frame() { | |
| 62 | this.frames++; | |
| 63 | this.advance(); | |
| 64 | this.perSecond[this.second % RATE_SECONDS]!++; | |
| 65 | } | |
| 66 | ||
| 67 | /** A viewer of `kind` connected (+1) or left (-1). */ | |
| 68 | viewer(kind: ViewerKind, delta: 1 | -1) { | |
| 69 | this.viewerCount[kind] = Math.max(0, this.viewerCount[kind] + delta); | |
| 70 | } | |
| 71 | ||
| 72 | /** A /follow command arrived from a browser. */ | |
| 73 | command() { | |
| 74 | this.received++; | |
| 75 | } | |
| 76 | ||
| 77 | /** A /follow command was not passed on to the arm. */ | |
| 78 | drop() { | |
| 79 | this.dropped++; | |
| 80 | } | |
| 81 | ||
| 82 | /** Bytes written to a viewer. */ | |
| 83 | out(bytes: number) { | |
| 84 | this.bytes += bytes; | |
| 85 | } | |
| 86 | ||
| 87 | private advance() { | |
| 88 | const s = Math.floor(this.now() / 1000); | |
| 89 | const gap = Math.min(s - this.second, RATE_SECONDS); | |
| 90 | for (let i = 1; i <= gap; i++) this.perSecond[(this.second + i) % RATE_SECONDS] = 0; | |
| 91 | if (s > this.second) this.second = s; | |
| 92 | } | |
| 93 | ||
| 94 | snapshot(): LiveSnapshot { | |
| 95 | this.advance(); | |
| 96 | const sorted = this.rtts.slice(0, this.rttCount).sort(); | |
| 97 | let inWindow = 0; | |
| 98 | for (const n of this.perSecond) inWindow += n; | |
| 99 | return { | |
| 100 | rttP50Ms: quantile(sorted, 0.5), | |
| 101 | rttP95Ms: quantile(sorted, 0.95), | |
| 102 | rttSamples: this.rttCount, | |
| 103 | cameraFramesTotal: this.frames, | |
| 104 | cameraFps: Math.round((inWindow / RATE_SECONDS) * 10) / 10, | |
| 105 | viewers: { ...this.viewerCount }, | |
| 106 | followReceived: this.received, | |
| 107 | followDropped: this.dropped, | |
| 108 | bytesOut: this.bytes, | |
| 109 | }; | |
| 110 | } | |
| 111 | ||
| 112 | /** Prometheus text exposition format, one sample per line. */ | |
| 113 | prometheus(): string { | |
| 114 | const s = this.snapshot(); | |
| 115 | const out: string[] = []; | |
| 116 | const metric = (name: string, type: "gauge" | "counter", help: string, samples: Array<[string, number | null]>) => { | |
| 117 | out.push(`# HELP ${name} ${help}`, `# TYPE ${name} ${type}`); | |
| 118 | for (const [labels, value] of samples) if (value !== null) out.push(`${name}${labels} ${value}`); | |
| 119 | }; | |
| 120 | metric("arm_link_rtt_ms", "gauge", "Round trip to the arm server over the recent samples, ms.", [ | |
| 121 | ['{quantile="0.5"}', s.rttP50Ms], | |
| 122 | ['{quantile="0.95"}', s.rttP95Ms], | |
| 123 | ]); | |
| 124 | metric("arm_link_rtt_samples", "gauge", "Round trips in the window the quantiles come from.", [["", s.rttSamples]]); | |
| 125 | metric("camera_frames_in_total", "counter", "Camera frames received from sources.", [["", s.cameraFramesTotal]]); | |
| 126 | metric("camera_frames_in_per_second", "gauge", "Camera frames per second over the last ten seconds.", [["", s.cameraFps]]); | |
| 127 | metric("viewers", "gauge", "Connected viewers by kind of socket.", VIEWER_KINDS.map((k) => [`{kind="${k}"}`, s.viewers[k]])); | |
| 128 | metric("follow_commands_received_total", "counter", "/follow commands received from browsers.", [["", s.followReceived]]); | |
| 129 | metric("follow_commands_dropped_total", "counter", "/follow commands not passed on to the arm.", [["", s.followDropped]]); | |
| 130 | metric("viewer_bytes_sent_total", "counter", "Bytes sent to viewers (camera streams and /follow replies).", [["", s.bytesOut]]); | |
| 131 | return out.join("\n") + "\n"; | |
| 132 | } | |
| 133 | ||
| 134 | /** The one-line summary, for the log. */ | |
| 135 | summary(): string { | |
| 136 | const s = this.snapshot(); | |
| 137 | const v = VIEWER_KINDS.map((k) => `${k}=${s.viewers[k]}`).join(" "); | |
| 138 | return `metrics: rtt p50=${s.rttP50Ms ?? "-"}ms p95=${s.rttP95Ms ?? "-"}ms camera=${s.cameraFps}fps viewers ${v} follow received=${s.followReceived} dropped=${s.followDropped} out=${s.bytesOut}B`; | |
| 139 | } | |
| 140 | } |
Software/so101/src/relay.test.ts +11
| @@ -136,6 +136,17 @@ describe("relay", () => { | ||
| 136 | 136 | cameras.close(); |
| 137 | 137 | }); |
| 138 | 138 | |
| 139 | test("/metrics.prom answers the relay's own numbers as Prometheus text", async () => { | |
| 140 | relay = await startRelay({ port: 0, simulation: simBase }); | |
| 141 | const r = await fetch(`http://127.0.0.1:${relay.port}/metrics.prom`); | |
| 142 | expect(r.status).toBe(200); | |
| 143 | expect(r.headers.get("content-type")).toContain("text/plain"); | |
| 144 | const text = await r.text(); | |
| 145 | expect(text).toContain("# TYPE viewers gauge"); | |
| 146 | expect(text).toMatch(/^viewers\{kind="follow"\} 0$/m); | |
| 147 | expect(text).toMatch(/^follow_commands_received_total 0$/m); | |
| 148 | }); | |
| 149 | ||
| 139 | 150 | test("unset: everything is the simulation, and the page says so", async () => { |
| 140 | 151 | relay = await startRelay({ port: 0, simulation: simBase }); |
| 141 | 152 | const base = `http://127.0.0.1:${relay.port}`; |
Software/so101/src/relay.ts +30 −8
| @@ -55,6 +55,7 @@ import type { Duplex } from "node:stream"; | ||
| 55 | 55 | import { Readable } from "node:stream"; |
| 56 | 56 | import { pipeline } from "node:stream/promises"; |
| 57 | 57 | import WebSocket, { WebSocketServer } from "ws"; |
| 58 | import { LiveMetrics, type ViewerKind } from "./live-metrics.ts"; | |
| 58 | 59 | import { ArmLink, type LinkTimings } from "./arm-link.ts"; |
| 59 | 60 | import { MachineFeed } from "./machine-feed.ts"; |
| 60 | 61 | import { perServo } from "./machine-log.ts"; |
| @@ -112,6 +113,8 @@ export interface RelayOptions { | ||
| 112 | 113 | readTimeoutMs?: number; |
| 113 | 114 | /** Who may command the arm; without one, anyone who reaches the relay may (a run on one's own machine). */ |
| 114 | 115 | lobby?: Lobby | null; |
| 116 | /** How often the one-line summary of the relay's own numbers is logged. */ | |
| 117 | summaryEveryMs?: number; | |
| 115 | 118 | /** How often each /follow browser is pinged; one that misses a ping is a lost connection. */ |
| 116 | 119 | heartbeatMs?: number; |
| 117 | 120 | /** Timings of the shared camera feeds. */ |
| @@ -223,7 +226,13 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 223 | 226 | const readTimeoutMs = options.readTimeoutMs ?? 2500; |
| 224 | 227 | const lobby = options.lobby ?? null; |
| 225 | 228 | const heartbeatMs = options.heartbeatMs ?? 1000; |
| 226 | const cameras = options.cameras ?? new CameraHub({ feed: options.camera, seed: remote ? [{ name: "Camera 1", source: remote.cameras[0]! }] : [] }); | |
| 229 | const live = new LiveMetrics(); | |
| 230 | /** Counts a viewer in until `closing` says it has gone. */ | |
| 231 | const watch = (kind: ViewerKind, closing: { once(event: "close", fn: () => void): unknown }) => { | |
| 232 | live.viewer(kind, 1); | |
| 233 | closing.once("close", () => live.viewer(kind, -1)); | |
| 234 | }; | |
| 235 | const cameras = options.cameras ?? new CameraHub({ feed: { ...options.camera, onFrame: () => live.frame(), onBytes: (n) => live.out(n) }, seed: remote ? [{ name: "Camera 1", source: remote.cameras[0]! }] : [] }); | |
| 227 | 236 | const leaders = options.leaders ?? new LeaderCalibrations(); |
| 228 | 237 | |
| 229 | 238 | // ------------------------------------------------------------------ the link |
| @@ -242,6 +251,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 242 | 251 | remote, |
| 243 | 252 | open: (url, timeoutMs) => open(url, timeoutMs), |
| 244 | 253 | timings: options.link, |
| 254 | onRtt: (ms) => live.rtt(ms), | |
| 245 | 255 | onStatus: (status) => onArm() && machine.status(status), |
| 246 | 256 | onMachine: (entries, run) => onArm() && machine.arm(entries, run), |
| 247 | 257 | onChange: (state, previous) => { |
| @@ -626,6 +636,10 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 626 | 636 | if (refused) return json(res, refused.status, { error: refused.error }); |
| 627 | 637 | by = who!.name; |
| 628 | 638 | } |
| 639 | if (req.method === "GET" && url.pathname === "/metrics.prom") { | |
| 640 | res.writeHead(200, { "content-type": "text/plain; version=0.0.4; charset=utf-8", "cache-control": "no-store" }); | |
| 641 | return res.end(live.prometheus()); | |
| 642 | } | |
| 629 | 643 | if (req.method === "GET" && url.pathname === "/link") return json(res, 200, link()); |
| 630 | 644 | if (req.method === "GET" && url.pathname === "/status") return await status(res, path); |
| 631 | 645 | |
| @@ -635,6 +649,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 635 | 649 | const id = Number(camera[1]); |
| 636 | 650 | const source = cameras.source(id); |
| 637 | 651 | if (source === null) return json(res, 404, { error: "no such camera" }); |
| 652 | if (camera[2] === "stream") watch("camera", res); | |
| 638 | 653 | if (camera[2] === "stream") return cameras.feed(id)!.serve(req, res, parseFps(url.searchParams.get("fps"))); |
| 639 | 654 | if (camera[2] === "state") return json(res, 200, { ok: cameras.list().find((c) => c.id === id)?.online ?? null }); |
| 640 | 655 | const target = source.replace(/\/[^/]*$/, "/snapshot") + url.search; |
| @@ -722,7 +737,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 722 | 737 | streams.delete(browser); |
| 723 | 738 | admission.close(); |
| 724 | 739 | }); |
| 725 | arm.on("message", (data, binary) => browser.readyState === WebSocket.OPEN && browser.send(data, { binary })); | |
| 740 | arm.on("message", (data, binary) => browser.readyState === WebSocket.OPEN && (live.out(frameBytes(data)), browser.send(data, { binary }))); | |
| 726 | 741 | const cap = new FrameCap(); |
| 727 | 742 | let seeded = false; |
| 728 | 743 | let asking = false; |
| @@ -750,27 +765,30 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 750 | 765 | let overLimit = false; |
| 751 | 766 | browser.on("message", (raw) => { |
| 752 | 767 | if (overLimit) return; |
| 768 | live.command(); | |
| 753 | 769 | if (frameBytes(raw) > MAX_FRAME_BYTES) { |
| 754 | 770 | overLimit = true; |
| 771 | live.drop(); | |
| 755 | 772 | machine.note("clamp", "not passed on", { why: "frame too large", bytes: frameBytes(raw) }); |
| 756 | 773 | return browser.close(TOO_BIG_CLOSE, `a frame is at most ${MAX_FRAME_BYTES} bytes`); |
| 757 | 774 | } |
| 758 | 775 | const pace = rate.take(); |
| 759 | 776 | if (pace === "flood") { |
| 760 | 777 | overLimit = true; |
| 778 | live.drop(); | |
| 761 | 779 | machine.note("clamp", "not passed on", { why: "too many frames" }); |
| 762 | 780 | return browser.close(FLOOD_CLOSE, "too many frames: the stream was closed"); |
| 763 | 781 | } |
| 764 | if (pace === "drop") return; | |
| 782 | if (pace === "drop") return void live.drop(); | |
| 765 | 783 | if (driver !== null) { |
| 766 | 784 | // The turn may have ended while this frame was on its way, before the close reaches the page: it is not passed on. |
| 767 | if (lobby && !lobby.turns.isDriver(driver) && !lobby.local) return void machine.note("clamp", "not passed on", { why: "the turn ended" }); | |
| 785 | if (lobby && !lobby.turns.isDriver(driver) && !lobby.local) return void (live.drop(), machine.note("clamp", "not passed on", { why: "the turn ended" })); | |
| 768 | 786 | lobby?.activity(driver); |
| 769 | 787 | } |
| 770 | 788 | const verdict = admission.admit(String(raw)); |
| 771 | if (!verdict.ok) return void machine.note("clamp", "not passed on", { why: verdict.drop }); | |
| 772 | if (arm.readyState !== WebSocket.OPEN) return; | |
| 773 | if (real && mode() !== "live") return void machine.note("clamp", "not passed on", { why: RECONNECTING }); | |
| 789 | if (!verdict.ok) return void (live.drop(), machine.note("clamp", "not passed on", { why: verdict.drop })); | |
| 790 | if (arm.readyState !== WebSocket.OPEN) return void live.drop(); | |
| 791 | if (real && mode() !== "live") return void (live.drop(), machine.note("clamp", "not passed on", { why: RECONNECTING })); | |
| 774 | 792 | const data = JSON.stringify(Object.fromEntries(verdict.joints)); |
| 775 | 793 | const percent = lobby?.maxSpeedPercent ?? 100; |
| 776 | 794 | const text = String(data); |
| @@ -783,7 +801,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 783 | 801 | if (!seeded) seed(); |
| 784 | 802 | const capped = cap.cap(text, percent); |
| 785 | 803 | if (capped !== text) machine.note("clamp", "speed cap", { percent, asked: values(text), sent: capped === null ? {} : values(capped) }); |
| 786 | if (capped === null) return; | |
| 804 | if (capped === null) return void live.drop(); | |
| 787 | 805 | machine.note("sent", "follow frame", { values: values(capped) }); |
| 788 | 806 | arm.send(capped); |
| 789 | 807 | }); |
| @@ -809,6 +827,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 809 | 827 | if (url.pathname !== "/follow" && url.pathname !== "/machine" && !(url.pathname === "/live" && lobby)) return refuse(socket, "404 Not Found"); |
| 810 | 828 | if (!sameOrigin(req)) return refuse(socket, "403 Forbidden"); |
| 811 | 829 | socket.on("error", () => socket.destroy()); |
| 830 | watch(url.pathname === "/live" ? "live" : url.pathname === "/machine" ? "machine" : "follow", socket); | |
| 812 | 831 | if (url.pathname === "/live") return lobby!.upgrade(req, socket, head); |
| 813 | 832 | if (url.pathname === "/machine") return machine.upgrade(req, socket, head); |
| 814 | 833 | let driver: string | null = null; |
| @@ -861,6 +880,8 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 861 | 880 | if (lobby) follow(); |
| 862 | 881 | if (!armLink) connect(); |
| 863 | 882 | await (armLink as ArmLink | null)?.ready; // bounded by the first attempt's timeout: the first page already knows which arm it has |
| 883 | const summaryTimer = setInterval(() => console.log(live.summary()), options.summaryEveryMs ?? 60_000); | |
| 884 | summaryTimer.unref?.(); | |
| 864 | 885 | const timer = lobby ? setInterval(follow, followEveryMs) : undefined; |
| 865 | 886 | timer?.unref?.(); |
| 866 | 887 | const pusher = lobby ? setInterval(() => void push(), statusEveryMs) : undefined; |
| @@ -874,6 +895,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 874 | 895 | new Promise<void>((resolve) => { |
| 875 | 896 | clearInterval(timer); |
| 876 | 897 | clearInterval(pusher); |
| 898 | clearInterval(summaryTimer); | |
| 877 | 899 | settingsChanged?.(); |
| 878 | 900 | turnEnded?.(); |
| 879 | 901 | arrived?.(); |
new above
offline