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
2 changes: 2 additions & 0 deletions js/net/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface AcceptProps {
consume?: Producer_4;
implementation?: string;
publish?: Consumer_5;
signal?: AbortSignal;
timeout?: Milli;
transport: WebTransport;
url: URL;
version?: number;
Expand Down
56 changes: 56 additions & 0 deletions js/net/bench/compression-fanout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { Producer as Group } from "../src/group.ts";
import { hooks } from "../src/internal.ts";
import { Producer as Track } from "../src/track.ts";

/** Sweep compressed publishers and public group readers, sharing one inflated copy per group. */
export function fanout() {
const payload = new TextEncoder().encode('{"value":123,"status":"unchanged"}\n'.repeat(64));
const frames = 32;
const rounds = 10;
const rows: { publishers: number; readers: number; phase: string; us: number; size_bytes: number }[] = [];
for (const publishers of [1, 4, 16]) {
for (const readers of [1, 4, 16]) {
let publish = 0;
let read = 0;
let stored = 0;
let inflated = 0;
for (let round = 0; round < rounds; round++) {
const tracks = Array.from({ length: publishers }, () => new Track("bench").accept({ compress: true }));
const groups: Group[] = tracks.map((track) => track.appendGroup());
const consumers = groups.map((group) => Array.from({ length: readers }, () => group.consume()));
let started = performance.now();
for (const group of groups) {
for (let index = 0; index < frames; index++) group.writeFrame({ payload });
group.finish();
}
publish += performance.now() - started;
stored = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0);
started = performance.now();
for (const subscribers of consumers) {
let shared: Uint8Array | undefined;
for (const consumer of subscribers) {
for (let index = 0; index < frames; index++) {
const frame = consumer.tryReadFrame();
if (!frame || frame.payload.length !== payload.length) throw new Error("missing payload");
if (index === 0) {
if (shared && shared !== frame.payload) throw new Error("fanout duplicated inflation");
shared = frame.payload;
}
}
consumer.close();
}
}
read += performance.now() - started;
inflated = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0) - stored;
if (inflated !== publishers * frames * payload.length)
throw new Error("inflated bytes scale with readers");
for (const track of tracks) track.finish();
for (const track of tracks) track.close();
}
rows.push({ publishers, readers, phase: "publish", us: (publish * 1000) / rounds, size_bytes: stored });
rows.push({ publishers, readers, phase: "read", us: (read * 1000) / rounds, size_bytes: inflated });
}
}

return rows;
}
49 changes: 3 additions & 46 deletions js/net/bench/compression.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
/** Sweep compressed publishers x plain readers, reporting one shared inflated copy per group, then compressed datagram retention. */
import { gzipSync } from "node:zlib";
import { Encoder } from "../src/flate/encoder.ts";
import type { Producer as Group } from "../src/group.ts";
import { hooks } from "../src/internal.ts";
import { Producer as Track } from "../src/track.ts";
import { fanout } from "./compression-fanout.ts";

const payload = new TextEncoder().encode('{"value":123,"status":"unchanged"}\n'.repeat(64));
const frames = 32;
const rounds = 10;
console.log("publishers,readers,phase,us,size_bytes");
for (const publishers of [1, 4, 16]) {
for (const readers of [1, 4, 16]) {
let publish = 0;
let read = 0;
let stored = 0;
let inflated = 0;
for (let round = 0; round < rounds; round++) {
const tracks = Array.from({ length: publishers }, () => new Track("bench").accept({ compress: true }));
const groups: Group[] = tracks.map((track) => track.appendGroup());
const consumers = groups.map((group) => Array.from({ length: readers }, () => group.mirror()));
let started = performance.now();
for (const group of groups) {
for (let index = 0; index < frames; index++) group.writeFrame({ payload });
group.finish();
}
publish += performance.now() - started;
stored = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0);
started = performance.now();
for (const subscribers of consumers) {
let shared: Uint8Array | undefined;
for (const consumer of subscribers) {
for (let index = 0; index < frames; index++) {
const frame = consumer.tryReadFrame();
if (!frame || frame.payload.length !== payload.length) throw new Error("missing payload");
if (index === 0) {
if (shared && shared !== frame.payload) throw new Error("fanout duplicated inflation");
shared = frame.payload;
}
}
consumer.close();
}
}
read += performance.now() - started;
inflated = groups.reduce((bytes, group) => bytes + hooks.groupCacheBytes(group), 0) - stored;
if (inflated !== publishers * frames * payload.length) throw new Error("inflated bytes scale with readers");
for (const track of tracks) track.finish();
for (const track of tracks) track.close();
}
console.log([publishers, readers, "publish", ((publish * 1000) / rounds).toFixed(1), stored].join(","));
console.log([publishers, readers, "read", ((read * 1000) / rounds).toFixed(1), inflated].join(","));
}
}
for (const row of fanout())
console.log([row.publishers, row.readers, row.phase, row.us.toFixed(1), row.size_bytes].join(","));

// Received compressed datagrams of zeros, each inflating to the largest payload, swept over
// tracks x queued datagrams. A fast reader takes each one while a slow reader never reads, so
Expand Down
48 changes: 44 additions & 4 deletions js/net/src/connection/accept.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Milli, Nano } from "@moq/clock";
import { race } from "@moq/signals";
import { ConnectTimeout, error, fromClose, ProtocolViolation } from "../error.ts";
import * as Ietf from "../ietf/index.ts";
import * as Message from "../ietf/message.ts";
import { IMPLEMENTATION } from "../implementation.ts";
import * as Lite from "../lite/index.ts";
import type { Consumer as OriginConsumer, Producer as OriginProducer } from "../origin.ts";
import { Stream } from "../stream.ts";
import { untilAborted } from "../util/abort.ts";
import { after } from "../util/timer.ts";
import type { Established } from "./established.ts";
import * as Extensions from "./extensions.ts";
import { DISCOVERY_REMOVED, forwardAnnounced } from "./forward.ts";
import { exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts";
import { DEFAULT_TIMEOUT, exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts";

/** Options for {@link accept}. */
export interface AcceptProps {
Expand All @@ -16,6 +21,11 @@ export interface AcceptProps {
/** The request URL associated with the transport. */
url: URL;

/** Bound SETUP in milliseconds (default: 30000; zero waits until aborted). Throws ConnectTimeout. */
timeout?: Milli;
/** Abort the handshake and release the transport; ignored after acceptance completes. */
signal?: AbortSignal;

/** Version to select during SETUP negotiation (for non-ALPN paths). */
version?: number;

Expand Down Expand Up @@ -63,9 +73,39 @@ type SessionProps = {
*/
export async function accept({ transport, url, ...props }: AcceptProps): Promise<Established> {
if ("discovery" in props) throw new TypeError(DISCOVERY_REMOVED);
const connection = await acceptInner(transport, url, props);
if (props.consume) forwardAnnounced(connection, props.consume);
return connection;
props.signal?.throwIfAborted();
const limit = props.timeout ?? DEFAULT_TIMEOUT;
const duration = Nano.fromMilli(Milli(limit));
const deadline = new AbortController();
const signal = props.signal ? AbortSignal.any([props.signal, deadline.signal]) : deadline.signal;
const disarm = limit > 0 ? after(duration, () => deadline.abort(new ConnectTimeout(limit))) : () => {};
const pending = acceptInner(transport, url, props);
// As on connect, a dead transport must end a SETUP whose peer sends nothing more.
const terminal = transport.closed.then(fromClose, error).then((cause) => {
throw cause ?? new Error("session closed during SETUP");
});
try {
const connection = await untilAborted(race([pending, terminal]), signal);
if (signal.aborted) throw signal.reason;
if (props.consume) forwardAnnounced(connection, props.consume);
return connection;
} catch (failure) {
try {
transport.close(
failure instanceof ProtocolViolation ? { closeCode: failure.code, reason: failure.message } : undefined,
);
} catch {
/* Already closed. */
}
// A handshake that resolves after cancellation must release its connection too.
void pending.then(
(connection) => connection.abort(),
() => {},
);
throw props.signal?.aborted ? props.signal.reason : failure;
} finally {
disarm();
}
}

async function acceptInner(
Expand Down
5 changes: 1 addition & 4 deletions js/net/src/connection/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ import { isWebTransportSupported } from "./browser.ts";
import type { Established } from "./established.ts";
import * as Extensions from "./extensions.ts";
import { DISCOVERY_REMOVED, forwardAnnounced } from "./forward.ts";
import { exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts";
import { DEFAULT_TIMEOUT, exchangeSetup, handsOff, legacyPeerImplementation } from "./handshake.ts";
import { type Credential, credential, fallback, hold } from "./tokens.ts";

// Default head start for WebTransport before attempting the WebSocket fallback.
const DEFAULT_WEBSOCKET_DELAY_MS = Milli(500);

// Default bound on one connect, the dial and the MoQ handshake; matches the Rust client.
const DEFAULT_TIMEOUT = Milli(30_000);

// How long the first WebTransport dial may stay unopened before a second one dials alongside
// it. The same moment as the WebSocket's default head start, so a fresh WebTransport flow
// races the fallback rather than losing to it, which would leave the session on WebSocket for
Expand Down
4 changes: 4 additions & 0 deletions js/net/src/connection/handshake.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Milli } from "@moq/clock";
import { closeReason, ProtocolViolation, SessionCode, StreamError } from "../error.ts";
import { type Hop, randomHop } from "../hop.ts";
import * as Ietf from "../ietf/index.ts";
Expand All @@ -6,6 +7,9 @@ import * as Lite from "../lite/index.ts";
import { Reader, Stream, UnexpectedEnd, Writer } from "../stream.ts";
import * as Extensions from "./extensions.ts";

/** Default bound on a connection attempt, including its SETUP exchange. @internal */
export const DEFAULT_TIMEOUT = Milli(30_000);

/**
* Draft-17+ SETUP exchange. Each side opens a uni stream, writes its Setup
* message, and reads the peer's Setup off an incoming uni stream. The two
Expand Down
36 changes: 20 additions & 16 deletions js/net/src/connection/reload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Producer as OriginProducer } from "../origin.ts";
import * as Path from "../path.ts";
import { Stream } from "../stream.ts";
import { Timescale } from "../time.ts";
import { closeUnwanted } from "../util/announce.ts";
import { nextTask } from "../util/yield.ts";
import { wireOf } from "../wire.ts";
import type { Established } from "./established.ts";
Expand Down Expand Up @@ -1012,13 +1013,12 @@ test("closing the loop keeps the tokens until the session drains", async () => {
test("closing an announce consumer during upstream teardown does not append retractions", async () => {
const { Producer } = await import("../announced.ts");
const upstream = new Producer();
const reload = new Reload({ enabled: false });
reload.established.set({
probe: new Signal(undefined),
announced: () => upstream.consume(),
stats: async () => undefined,
} as unknown as import("./established.ts").Established);
const consumer = reload.announced();
const producer = new Producer();
const pump = new Effect((effect) => pumpAnnounced(effect, upstream.consume(), producer));
const consumer = producer.consume();
// The same lifecycle as Connection.announced: the last consumer closing closes the producer and its pump.
closeUnwanted(producer);
void producer.closed.then(() => pump.close());
const errors = spyOn(console, "error").mockImplementation(() => {});
try {
upstream.append({
Expand All @@ -1033,21 +1033,23 @@ test("closing an announce consumer during upstream teardown does not append retr
await Promise.resolve();
consumer.close();
await settle();
expect(producer.closed.peek()).not.toBeUndefined();
expect(errors.mock.calls).toEqual([]);
} finally {
consumer.close();
reload.close();
pump.close();
producer.close();
errors.mockRestore();
}
});

test("announced accepts a non-prefix scope before any session exists", () => {
const reload = new Reload({ enabled: false });
const connection = new Connection({ enabled: false });
try {
const announced = reload.announced(Path.Pattern.parse("room/*"));
const announced = connection.announced(Path.Pattern.parse("room/*"));
announced.close();
} finally {
reload.close();
connection.close();
}
});

Expand Down Expand Up @@ -1080,14 +1082,15 @@ test("a watcher syncs with the server's initial set, and again after a reconnect
globalThis.WebTransport = stub as unknown as typeof WebTransport;

const clientOrigin = new OriginProducer();
const reload = new Reload({
const props = {
enabled: true,
url,
websocket: { enabled: false },
delay: { initial: Milli(10), multiplier: 1, max: Milli(10) },
consume: viaOrigin ? clientOrigin : undefined,
});
const watching = viaOrigin ? clientOrigin.announced() : reload.announced();
};
const reload = viaOrigin ? new Reload({ ...props, consume: clientOrigin }) : undefined;
const connection = viaOrigin ? undefined : new Connection({ ...props, share: false });
const watching = connection ? connection.announced() : clientOrigin.announced();
const seen = new Set<string>();
void (async () => {
for await (const event of watching) {
Expand All @@ -1108,7 +1111,8 @@ test("a watcher syncs with the server's initial set, and again after a reconnect
expect([...seen].sort()).toEqual(["a", "b"]);
} finally {
watching.close();
reload.close();
reload?.close();
connection?.close();
clientOrigin.close();
for (const broadcast of published) broadcast.close();
serverOrigin.close();
Expand Down
50 changes: 2 additions & 48 deletions js/net/src/connection/reload.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Instant, Milli, Nano, now, sleep } from "@moq/clock";
import { Effect, type GetPromise, type Getter, Once, Signal } from "@moq/signals";
import * as Announce from "../announced.ts";
import type * as Announce from "../announced.ts";
import { Handle as AuthHandle } from "../auth.ts";
import { ended } from "../auth_session.ts";
import { Allocator } from "../bandwidth.ts";
import { error, reason, SessionCode, SessionError } from "../error.ts";
import type { Consumer as OriginConsumer, Producer as OriginProducer } from "../origin.ts";
import * as Path from "../path.ts";
import { closeUnwanted } from "../util/announce.ts";
import { Follow } from "../util/observed.ts";
import { wireOf } from "../wire.ts";
import {
Expand All @@ -20,7 +19,7 @@ import {
import type { Established } from "./established.ts";
import { DISCOVERY_REMOVED } from "./forward.ts";
import { DEFAULT_HANDOVER, type Drain, dialed, type GoawayProps, handover, pinnedTransport, target } from "./goaway.ts";
import type { Probe, Stats } from "./stats.ts";
import type { Probe } from "./stats.ts";
import { heldOf } from "./tokens.ts";

function sameTokens(a: readonly string[], b: readonly string[]): boolean {
Expand Down Expand Up @@ -824,51 +823,6 @@ export class Reload {
this.#retire();
}

/**
* Subscribe to broadcast announcements matching `scope`, spanning reconnects.
*
* The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active
* is retracted (an `end` event) whenever the connection drops and re-announced on
* reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.
*/
announced(scope: Path.Pattern = Path.Pattern.all(), options?: Announce.Options): Announce.Consumer {
// With a consume origin the table already spans reconnects (the forwarder retracts
// a dead session's entries), so its stream is the same thing with less machinery.
if (this.consume) return this.consume.announced(scope, options);

const producer = new Announce.Producer();
const consumer = producer.consume();

const pump = new Effect();
pump.run((effect) => {
const conn = effect.get(this.established);
if (!conn) {
// The same hold as a consume origin's: behind while enabled with no session.
if (effect.get(this.enabled)) producer.behind();
else producer.synced();
return;
}

// A dropped connection retracts what it announced, so a per-broadcast watcher tears
// down instead of clinging to the dead route.
pumpAnnounced(effect, conn.announced(scope, options), producer);
});

this.#signals.cleanup(() => pump.close());
closeUnwanted(producer);
void producer.closed.then(() => pump.close());

return consumer;
}

/**
* Snapshot the live connection's transport counters, or undefined while disconnected.
* See {@link Established.stats}.
*/
async stats(): Promise<Stats | undefined> {
return this.established.peek()?.stats();
}

/** Stop reconnecting, drain and close the current connection, and settle {@link Reload.closed}. Idempotent. */
close(abort?: Error) {
this.#signals.close();
Expand Down
Loading
Loading