Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions js/net/bench/qmux.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/** Compare eager and cold lazy fallback startup with real WebSocket negotiation. */
import { basename } from "node:path";
import { gzipSync } from "node:zlib";
import { selectSubprotocol } from "@moq/qmux";
import { Session, selectSubprotocol, type WebSocketStreamCloseEvent, type WebSocketStreamCloseInfo } from "@moq/qmux";
import { type Browser, chromium } from "playwright";

/** Per-connection state from the upgrade; `open` attaches the frame feed. */
interface Peer {
url: string;
protocol: string;
closed: PromiseWithResolvers<WebSocketStreamCloseEvent>;
incoming?: ReadableStreamDefaultController<Uint8Array>;
}

const build = await Bun.build({
entrypoints: [`${import.meta.dir}/qmux-eager.ts`, `${import.meta.dir}/qmux-lazy.ts`],
target: "browser",
Expand All @@ -16,14 +24,15 @@ const files = new Map(
build.outputs.map(async (file) => [basename(file.path), gzipSync(await file.arrayBuffer())] as const),
),
);
const server = Bun.serve({
const server = Bun.serve<Peer>({
port: 0,
fetch(request, server) {
const path = new URL(request.url).pathname;
if (path === "/qmux") {
const protocol = selectSubprotocol(request.headers.get("Sec-WebSocket-Protocol"), {});
if (!protocol) return new Response("unsupported QMux protocol", { status: 400 });
if (server.upgrade(request, { headers: { "Sec-WebSocket-Protocol": protocol } })) return;
const data: Peer = { url: request.url, protocol, closed: Promise.withResolvers() };
if (server.upgrade(request, { data, headers: { "Sec-WebSocket-Protocol": protocol } })) return;
return new Response("upgrade required", { status: 400 });
}
const code = files.get(path.slice(1));
Expand All @@ -34,7 +43,46 @@ const server = Bun.serve({
headers: { "Content-Type": "text/html" },
});
},
websocket: { message() {} },
websocket: {
// Accept the upgraded socket like a real server, so Session sends its transport parameters first.
open(ws) {
const peer = ws.data;
const session = Session.accept({
url: peer.url,
opened: Promise.resolve({
protocol: peer.protocol,
extensions: "",
readable: new ReadableStream<Uint8Array>({
start: (controller) => {
peer.incoming = controller;
},
}),
writable: new WritableStream<Uint8Array>({
write: (bytes) => {
ws.send(bytes);
},
}),
}),
closed: peer.closed.promise,
close: (info?: WebSocketStreamCloseInfo) => ws.close(info?.closeCode, info?.reason),
});
// The client only sees the socket close, so log the server-side cause, such as a handshake timeout.
session.closed.catch((err: unknown) => console.error("qmux fake peer:", err));
},
Comment thread
kixelated marked this conversation as resolved.
Resolved
message(ws, message) {
// QMux is binary-only. Close rather than throw, so the session ends with the close.
if (typeof message === "string") {
ws.close(1003, "QMux frames are binary");
return;
}
if (!ws.data.incoming) throw new Error("QMux frame before the socket opened");
ws.data.incoming.enqueue(message);
},
close(ws, code, reason) {
ws.data.incoming?.close();
ws.data.closed.resolve({ closeCode: code, reason });
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
},
});
let browser: Browser | undefined;
try {
Expand Down
1 change: 0 additions & 1 deletion quest/m0/nightly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ near the paired threshold (re-check if one repeats), a one-off
## Required

- [Nightly's macOS platform job passes](/quest/m0/nightly/macos.md) - `moq-audio`'s tests compile on macOS again and the websocket version test's timeout is fixed if it recurs
- [The qmux startup benchmark's fake peer sends transport parameters](/quest/m0/nightly/qmux-bench-peer.md) - Audio quality's WebSocket startup step stops timing out
- [The moq-net session bench completes moq-transport-22 at 16 broadcasts](/quest/m0/nightly/session-bench-handshake.md) - the bench workload stops panicking on a handshake timeout
- [Bisect the moq-audio bus and moq-mux parameter_sets regressions](/quest/m0/nightly/bench-regressions.md) - two real benchmark regressions are explained and fixed or accepted
- [Moxygen conformance cells pass again](/quest/m0/nightly/moxygen-conformance.md) - the first-object signal and integer extensions survive, or moxygen's divergence is skipped with an upstream link
23 changes: 0 additions & 23 deletions quest/m0/nightly/qmux-bench-peer.md

This file was deleted.