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
4 changes: 4 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ jobs:
if: ${{ !cancelled() }}
run: nix develop --command just js bench broadcasts

- name: JS request ID admission benchmark
if: ${{ !cancelled() }}
run: nix develop --command just js bench request-ids

# A route above several interest heads lands once per head, so its re-price
# should expose any slope over heads times received routes.
- name: JS origin announce forwarding benchmark
Expand Down
4 changes: 4 additions & 0 deletions drafts/draft-lcurley-moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ Sent when terminating the session, via the transport's session close.
| ------- | ------------- | ----------- |
| 0x3 | PROTOCOL_VIOLATION | The peer violated this specification. |
| ------- | ------------- | ----------- |
| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused or violated that draft’s order or parity rules; moq-lite has no request IDs. |
| ------- | ------------- | ----------- |
| 0x6 | KEY_VALUE_FORMATTING_ERROR | A key-value pair was malformed, or repeated more than allowed. |
| ------- | ------------- | ----------- |
| 0x7 | TOO_MANY_REQUESTS | The peer held more announcements or subscriptions than the endpoint allows. |
Expand Down Expand Up @@ -1526,6 +1528,8 @@ The `Message Length` describes the payload size on the wire.

## moq-lite-07

- Listed the shared INVALID_REQUEST_ID (0x4) session code used by moq-transport drafts 14 through 16.

- SUBSCRIBE_OK `Group` names where delivery starts when the subscription resolves, which need not be the first group sent. It is not a new floor. A subscriber does not wait for a group between the requested floor and it, but a publisher still delivers one that arrives later within Subscriber Max Age.
- Corrected the SUBSCRIBE note that offset `Group Start` by 1. Only `Group End` and `Frame End` are offset so 0 can mean absent. `Group Start` is an absolute sequence, and 0 is group 0.
- A subscription's range bounds datagrams like groups, and FETCH never returns a datagram.
Expand Down
2 changes: 1 addition & 1 deletion js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test:
bun test common/declarations.test.ts common/deps.test.ts common/worklet.test.ts
bun run --filter='*' --elide-lines=0 test

# Run one net benchmark: broadcasts, forward, reader, frames, track, varint, or lite-varint.
# Run one net benchmark: broadcasts, forward, reader, frames, track, varint, lite-varint, or request-ids.
bench name:
bun install --frozen-lockfile
bun net/bench/{{ name }}.ts
Expand Down
80 changes: 80 additions & 0 deletions js/net/bench/request-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Request-ID admission cost across independent sessions and reordered windows.
import { ControlStreamAdapter } from "../src/ietf/adapter.ts";
import { Version } from "../src/ietf/version.ts";
import { Stream } from "../src/stream.ts";

/**
* Every ID in the window: in order, reversed (one skip to the top, then each fill
* shortens it), or scattered (the top first, then every other ID descending, so each
* splits the lowest skipped run and the remembered runs grow to half the window).
*/
function order(mode: string, window: number): bigint[] {
const all = Array.from({ length: window }, (_, id) => BigInt(id * 2));
if (mode === "ordered") return all;
if (mode === "reordered") return all.reverse();
const top = all[window - 1];
const odd = all.filter((id, index) => index % 2 === 1 && id !== top).reverse();
const even = all.filter((id, index) => index % 2 === 0 && id !== top).reverse();
return [top, ...odd, ...even];
}

const debug = console.debug;
console.debug = () => undefined;
try {
console.log("mode,sessions,window,admit_us,accepted");
// Up to the production window, REQUEST_WINDOW.
for (const mode of ["ordered", "reordered", "scattered"]) {
for (const sessions of [1, 8, 32]) {
for (const window of [8, 128, 220_000]) {
const ids = order(mode, window);
// Fewer rounds for the large window, keeping each sweep to a similar budget.
const rounds = Math.max(1, Math.floor(8192 / window));
let elapsed = 0;
let accepted = 0;
for (let round = 0; round < rounds; round++) {
const fixtures = Array.from({ length: sessions }, () => {
const stream = new Stream({
readable: new ReadableStream(),
writable: new WritableStream(),
version: Version.DRAFT_16,
});
const quic = { close() {} } as unknown as WebTransport;
return {
stream,
adapter: new ControlStreamAdapter(
quic,
stream,
Version.DRAFT_16,
0n,
false,
BigInt(window),
),
};
});
const start = performance.now();
for (const { adapter } of fixtures) {
for (const id of ids) {
try {
adapter.admitRequest(id);
accepted++;
} catch {
/* Count refusals in baseline comparisons. */
}
}
}
elapsed += performance.now() - start;
for (const { stream, adapter } of fixtures) {
adapter.close();
stream.close();
}
await Promise.resolve();
}
console.log(
`${mode},${sessions},${window},${((elapsed * 1000) / rounds).toFixed(2)},${accepted / rounds}`,
);
}
}
}
} finally {
console.debug = debug;
}
5 changes: 5 additions & 0 deletions js/net/src/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ test("the error type names the registry the code came from", () => {
expect(Number(stream.code)).toBe(Number(session?.code));
});

test("invalid request IDs keep their session registry code", () => {
expect(Number(SessionCode.InvalidRequestId)).toBe(0x4);
expect(fromClose({ closeCode: 0x4 })?.code).toBe(SessionCode.InvalidRequestId);
});

test("the code types reject the other registry", () => {
new StreamError(StreamCode.DeliveryTimeout);
new SessionError(SessionCode.Unauthorized);
Expand Down
2 changes: 2 additions & 0 deletions js/net/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const SessionCode = Object.freeze(
Unauthorized: 0x2 as SessionCode,
/** A protocol rule was broken; the session is unusable. */
ProtocolViolation: 0x3 as SessionCode,
/** A request ID was reused or violated the negotiated draft’s order or parity rules. */
InvalidRequestId: 0x4 as SessionCode,
/** A key-value pair was malformed or repeated more than allowed. */
KeyValueFormatting: 0x6 as SessionCode,
/** The peer went past what the session allows: a request ID past MAX_REQUEST_ID, or too many announcements or subscriptions. */
Expand Down
174 changes: 159 additions & 15 deletions js/net/src/ietf/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GoAway } from "./goaway.ts";
import { PublishNamespace, PublishNamespaceCancel, PublishNamespaceDone } from "./publish_namespace.ts";
import { initialMaxRequestId, MaxRequestId, RequestError } from "./request.ts";
import { Subscribe, SubscribeUpdate } from "./subscribe.ts";
import { SubscribeNamespaceLegacy } from "./subscribe_namespace.ts";
import { TrackStatusRequest } from "./track.ts";
import { ALPN, type IetfVersion, Version } from "./version.ts";

Expand Down Expand Up @@ -391,10 +392,10 @@ test("a request id with the wrong parity closes the session", async () => {

test("a second use of an open request id closes the session", async () => {
for (const [version, alpn] of WINDOW_DRAFTS) {
const { pair, peer, running } = await windowed(version, alpn, 2n, false);
const { pair, peer, running } = await windowed(version, alpn, 1n, false);
await trackStatus(peer, version, 0n);
await trackStatus(peer, version, 0n);
await expect(running).rejects.toThrow("duplicate request id");
await expect(running).rejects.toThrow("request id was already spent or out of sequence");
expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID);
}
});
Expand Down Expand Up @@ -434,9 +435,9 @@ test("drafts 14 to 16 route an update to its second request id", async () => {
for (const [version, alpn] of WINDOW_DRAFTS) {
const { adapter, peer, running } = await windowed(version, alpn, 4n, false);
const state = settled(running);
const target = await subscribe(adapter, peer, version, 2n);
await update(peer, version, 6n, 2n);
expect(await updated(target, version)).toBe(2n);
const target = await subscribe(adapter, peer, version, 0n);
await update(peer, version, 2n, 0n);
expect(await updated(target, version)).toBe(0n);
expect(await state()).toBeUndefined();
}
});
Expand Down Expand Up @@ -488,24 +489,45 @@ test("an update spending an open request id closes the session", async () => {
await trackStatus(peer, version, heldId);
if (!(await adapter.acceptBi())) throw new Error("no stream");
await update(peer, version, heldId, heldId);
await expect(running).rejects.toThrow("duplicate request id");
await expect(running).rejects.toThrow("request id was already spent or out of sequence");
expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID);
}
}
});

/** Repeated updates raise the maximum, but the peer still cannot hold more than the window open. */
test("reused update ids cannot hold more than the window", async () => {
/** Request IDs stay spent after their stream or update retires. */
test("retired and lower request ids close the session", async () => {
for (const [version, alpn] of WINDOW_DRAFTS) {
for (const client of [false, true]) {
const parity = client ? 1n : 0n;
for (const [first, later] of [
[parity, parity],
[parity + 4n, parity + 2n],
] as const) {
const { pair, adapter, peer, running } = await windowed(version, alpn, 8n, client);
for (let id = parity; id <= first; id += 2n) {
await trackStatus(peer, version, id);
const stream = await adapter.acceptBi();
if (!stream) throw new Error("no stream");
stream.close();
}
await trackStatus(peer, version, later);
await expect(running).rejects.toThrow("request id was already spent or out of sequence");
expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID);
}
}
}
});

test("an update id stays spent after the update retires", async () => {
for (const [version, alpn] of WINDOW_DRAFTS) {
const { pair, adapter, peer, running } = await windowed(version, alpn, 2n, false);
const { pair, adapter, peer, running } = await windowed(version, alpn, 4n, false);
await trackStatus(peer, version, 0n);
if (!(await adapter.acceptBi())) throw new Error("no stream");
for (let i = 0; i < 8; i++) await update(peer, version, 2n, 0n);
await trackStatus(peer, version, 4n);
if (!(await adapter.acceptBi())) throw new Error("no stream");
await trackStatus(peer, version, 6n);
await expect(running).rejects.toThrow("too many open requests");
expect((await pair.client.closed).closeCode).toBe(SessionCode.TooManyRequests);
await update(peer, version, 2n, 0n);
await update(peer, version, 2n, 0n);
await expect(running).rejects.toThrow("request id was already spent or out of sequence");
expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID);
}
});

Expand Down Expand Up @@ -543,6 +565,128 @@ test("more requests than the setup window complete on one session", async () =>
}
});

// Both requests and updates allocate IDs, even when an update's target is gone.
test("skipped request and update ids close the session", async () => {
for (const [version, alpn] of WINDOW_DRAFTS.filter(([version]) => version !== Version.DRAFT_16)) {
for (const client of [false, true]) {
for (const prefix of [0n, 1n]) {
for (const isUpdate of [false, true]) {
const { pair, peer, running } = await windowed(version, alpn, 8n, client);
const first = client ? 1n : 0n;
if (prefix) await trackStatus(peer, version, first);
const skipped = first + 2n * prefix + 2n;
if (isUpdate) await update(peer, version, skipped, 100n);
else await trackStatus(peer, version, skipped);
await expect(running).rejects.toThrow("request id was already spent or out of sequence");
expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId);
}
}
}
}
});

// SUBSCRIBE_NAMESPACE uses an independent bidi stream in draft 16, sharing the ID space.
test("draft 16 accepts reordered native and control IDs but never reuses them", async () => {
for (const client of [false, true]) {
for (const nativeFirst of [false, true]) {
const version = Version.DRAFT_16;
const { pair, adapter, peer, running } = await windowed(version, ALPN.DRAFT_16, 8n, client);
void running.catch(() => undefined);
const first = client ? 1n : 0n;
const native = nativeFirst ? first + 2n : first;
const control = nativeFirst ? first : first + 2n;
try {
if (nativeFirst) adapter.admitRequest(native);
await trackStatus(peer, version, control);
const stream = await adapter.acceptBi();
if (!stream) throw new Error("no reordered control request");
if (!nativeFirst) adapter.admitRequest(native);
stream.close();
adapter.releaseRequest(native);
await trackStatus(peer, version, first);
await expect(running).rejects.toThrow("request id was already spent");
expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId);
} finally {
peer.close();
adapter.close();
}
}
}
});

// Draft 16 remembers a skipped stretch as one run, so a jump costs nothing per skipped ID.
test("draft 16 tracks a jump across a production window as one run", async () => {
const window = 220_000n;
const { adapter, peer } = await windowed(Version.DRAFT_16, ALPN.DRAFT_16, window, false);
const top = 2n * (window - 1n);
try {
for (const id of [top, 100n, 0n, top - 2n, 2n, 98n]) adapter.admitRequest(id);
for (const id of [0n, 2n, 98n, 100n, top - 2n, top]) {
expect(() => adapter.admitRequest(id)).toThrow("request id was already spent");
}
} finally {
peer.close();
adapter.close();
}
});

// Splitting runs in descending order spans many chunks; every ID is admitted exactly once.
test("draft 16 admits a scattered window once across run chunks", async () => {
const window = 4096n;
const { adapter, peer } = await windowed(Version.DRAFT_16, ALPN.DRAFT_16, window, false);
const top = 2n * (window - 1n);
const below = Array.from({ length: Number(window - 1n) }, (_, i) => 2n * (window - 2n - BigInt(i)));
const ids = [top, ...below.filter((id) => (id / 2n) % 2n === 1n), ...below.filter((id) => (id / 2n) % 2n === 0n)];
try {
for (const id of ids) adapter.admitRequest(id);
for (const id of [0n, 2n, 1024n, top - 2n, top]) {
expect(() => adapter.admitRequest(id)).toThrow("request id was already spent");
}
} finally {
peer.close();
adapter.close();
}
});

// Allocation precedes an asynchronous encode, so a shorter later request can finish first.
test("drafts 14 and 15 send concurrent requests in request id order", async () => {
for (const [version, alpn] of [
[Version.DRAFT_14, ALPN.DRAFT_14],
[Version.DRAFT_15, ALPN.DRAFT_15],
] as const) {
const pair = createMockTransportPair(alpn);
const control = await Stream.open(pair.server, { version });
const sender = new ControlStreamAdapter(pair.server, control, version, 100n, true);
void sender.run().catch(() => undefined);
const peer = await Stream.accept(pair.client, version);
if (!peer) throw new Error("no control stream");
const receiver = new ControlStreamAdapter(pair.client, peer, version, 100n, false, 8n);
void receiver.run().catch(() => undefined);
try {
const request = async (namespace: Path.Valid) => {
const requestId = await sender.nextRequestId();
if (requestId === undefined) throw new Error("no request id");
const stream = sender.openBi();
await stream.writer.u53(SubscribeNamespaceLegacy.id);
await new SubscribeNamespaceLegacy({ namespace, requestId }).encode(stream.writer, version);
};
await Promise.all([request(Path.from("a/b/c/d/e/f/g/h")), request(Path.from("a"))]);

const received: bigint[] = [];
for (let i = 0; i < 2; i++) {
const stream = await receiver.acceptBi();
if (!stream) throw new Error("the receiver refused a request");
expect(await stream.reader.u53()).toBe(SubscribeNamespaceLegacy.id);
received.push((await SubscribeNamespaceLegacy.decode(stream.reader, version)).requestId);
}
expect(received).toEqual([0n, 2n]);
} finally {
sender.close();
receiver.close();
}
}
});

// Canceling the wait must remove the allocation itself, not just stop awaiting it.
test("a canceled request ID wait never spends a later grant", async () => {
for (const [version, alpn] of WINDOW_DRAFTS) {
Expand Down
Loading
Loading