diff --git a/js/justfile b/js/justfile index d71a6cdf9da..b5148be5616 100644 --- a/js/justfile +++ b/js/justfile @@ -55,7 +55,7 @@ test-changed list: coverage package="": python3 ../sh/js/coverage.py '{{ package }}' -# Run one net benchmark: broadcasts, forward, requests, reader, frames, track, resume, interests, observed, drift, varint, lite-varint, priority, request-ids, subscribe-drop, subscribe-tracks, refresh, or compression. +# Run one net benchmark: broadcasts, forward, requests, reader, frames, track, resume, observed, drift, varint, lite-varint, priority, request-ids, subscribe-drop, subscribe-tracks, refresh, or compression. bench name: bun install --frozen-lockfile bun net/bench/{{ name }}.ts diff --git a/js/net/bench/interests.ts b/js/net/bench/interests.ts deleted file mode 100644 index e3d872c79fa..00000000000 --- a/js/net/bench/interests.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** Sweep broadcast and track counts for acquiring and releasing a paired interest. */ -import { Interests, Slots } from "../src/util/limits.ts"; - -const counts = [1, 16, 256]; -const reps = 7; -const operations = 10_000; -// Charging one track must not cost more as the session holds more broadcasts and tracks, or the -// accounting has gone back to scanning them. The sweep spans 65,536x, so a scan lands well past -// this; the margin is loose because the machine is noisy. -const maxSlope = 5; - -let baseline: number | undefined; -for (const broadcasts of counts) { - for (const tracks of counts) { - const interests = new Interests(new Slots(broadcasts * tracks + 1, { reason: "bench" })); - const held = []; - for (let broadcast = 0; broadcast < broadcasts; broadcast++) { - for (let track = 0; track < tracks; track++) - held.push(interests.acquire(`${broadcast}`, `${track}`, "track")); - } - const times = []; - for (let rep = 0; rep < reps; rep++) { - const start = performance.now(); - // The SUBSCRIBE pairs with a held TRACK. A fresh track is left out: inserting and - // deleting one key costs O(size) near a hash table's capacity in both V8 and JSC, which - // would measure the engine rather than the accounting. - for (let operation = 0; operation < operations; operation++) { - interests.acquire("0", "0", "subscribe").release(); - } - times.push(((performance.now() - start) * 1000) / operations); - } - times.sort((a, b) => a - b); - const us = times[Math.floor(reps / 2)] ?? 0; - console.log(`broadcasts=${broadcasts} tracks=${tracks} acquire+release=${us.toFixed(3)} us`); - baseline ??= us; - if (us > baseline * maxSlope) { - throw new Error( - `${broadcasts}x${tracks} held interests cost ${us.toFixed(3)} us, over ${maxSlope}x ${baseline.toFixed(3)}`, - ); - } - for (const interest of held) interest.release(); - } -} diff --git a/js/net/src/error.ts b/js/net/src/error.ts index 1b861a5fa7f..bdbb445f6ba 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -43,7 +43,7 @@ export const SessionCode = Object.freeze( 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. */ + /** The peer went past what the session allows: a request ID past MAX_REQUEST_ID, or too many announcements. */ TooManyRequests: 0x7 as SessionCode, /** A PATH the peer may not send, such as a server sending one at all. */ InvalidPath: 0x8 as SessionCode, diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 715e565e758..4455b6e2b47 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -331,9 +331,8 @@ export class Publisher { #quic: WebTransport; #session: Session; #requiresSolicitation: boolean; - // Subscriptions the peer holds live on this session. + // Subscriptions the peer holds, PUBLISHes for its listings capped by `Limits.requests`. #subscriptions: Slots; - #fetches: Slots; #listings = new Set(); #trackClaims = new Map(); @@ -424,8 +423,7 @@ export class Publisher { limits?: Limits; }) { this.#quic = quic; - this.#subscriptions = new Slots(limits.subscriptions, { quic, reason: "too many subscriptions" }); - this.#fetches = new Slots(limits.fetches, { quic, reason: "too many fetches" }); + this.#subscriptions = new Slots(limits.requests, { reason: "too many requests" }); this.#grant = grant; this.#gate = new Gate(grant); this.#auth = auth; @@ -482,9 +480,9 @@ export class Publisher { return; } // Only a subscription we would serve holds a slot, as in Rust: a refused one ends at - // once. A peer past the cap loses the session. + // once. Stream credit bounds them, so none is refused here. const charged = !msg.rangeFilters && this.#gate.allows(msg.trackNamespace); - if (charged) this.#subscriptions.acquire(); + if (charged) this.#subscriptions.hold(); if (claimed) this.#trackClaims.set(key, ((this.#trackClaims.get(key) as number | undefined) ?? 0) + 1); try { await this.#serveSubscribe(msg, request); @@ -2836,20 +2834,15 @@ export class Publisher { async #runFetch(msg: Fetch, request: RequestStream) { this.#pinned(msg.epoch); - this.#fetches.acquire(); - try { - if (msg.trackNamespace !== undefined && Namespace.reserved(msg.trackNamespace, this.#session.version)) { - await refuseReserved(request); - return; - } - await request.refuse("fetch", { - requestId: msg.requestId, - condition: msg.rangeFilters ? "invalid_filter" : "not_supported", - reasonPhrase: msg.rangeFilters ? "Range Filters exceed MAX_FILTER_RANGES=0" : "FETCH is not supported", - }); - } finally { - this.#fetches.release(); + if (msg.trackNamespace !== undefined && Namespace.reserved(msg.trackNamespace, this.#session.version)) { + await refuseReserved(request); + return; } + await request.refuse("fetch", { + requestId: msg.requestId, + condition: msg.rangeFilters ? "invalid_filter" : "not_supported", + reasonPhrase: msg.rangeFilters ? "Range Filters exceed MAX_FILTER_RANGES=0" : "FETCH is not supported", + }); } /** What a request that failed to resolve is refused with. */ diff --git a/js/net/src/ietf/subscribe_tracks.test.ts b/js/net/src/ietf/subscribe_tracks.test.ts index 1c7b9dcfe71..011cf85942a 100644 --- a/js/net/src/ietf/subscribe_tracks.test.ts +++ b/js/net/src/ietf/subscribe_tracks.test.ts @@ -20,7 +20,7 @@ import { ALPN, type IetfVersion, Version } from "./version.ts"; const drafts = [Version.DRAFT_18, Version.DRAFT_19, Version.DRAFT_20, Version.DRAFT_21, Version.DRAFT_22]; -function fixture(version: IetfVersion, subscriptions = 10, grant = new Signal(undefined)) { +function fixture(version: IetfVersion, requests = 10, grant = new Signal(undefined)) { const pair = createMockTransportPair(ALPN.DRAFT_22); const origin = new Origin(); const session = new NativeSession(pair.server, version, false); @@ -30,7 +30,7 @@ function fixture(version: IetfVersion, subscriptions = 10, grant = new Signal[] = []; const streams: Stream[] = []; diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index d16c1830437..a0d7180b5ef 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -20,7 +20,6 @@ import { type Timescale, Timestamp } from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { nextChange } from "../util/change.ts"; -import { Interests, LIMITS, type Limits, Slots } from "../util/limits.ts"; import { nextTask } from "../util/yield.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; import { AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from "./announce.ts"; @@ -422,10 +421,6 @@ export class Publisher { #quic: WebTransport; - // Subscriptions and open TRACK streams the peer holds live on this session. - #interests: Interests; - #fetches: Slots; - // The one writer for the outbound datagram stream (getWriter locks it), acquired once at // construction when this version + transport carry datagrams, released in close(). Its // presence is the gate: undefined means datagrams aren't served on this connection. All @@ -464,7 +459,6 @@ export class Publisher { * @param publish - The origin whose broadcasts this session serves; omit to publish nothing * @param auth - The union of our tokens' grants, which bounds what we publish, and when the * setup tokens are answered - * @param limits - How many live requests the peer may hold * * @internal */ @@ -474,11 +468,8 @@ export class Publisher { hop: Hop, publish?: OriginConsumer, auth?: { grant: Getter; ready: Promise }, - limits: Limits = LIMITS, ) { this.#quic = quic; - this.#interests = new Interests(new Slots(limits.subscriptions, { quic, reason: "too many subscriptions" })); - this.#fetches = new Slots(limits.fetches, { quic, reason: "too many fetches" }); this.#grant = auth?.grant; this.#gate = new Gate(auth?.grant); this.#ready = auth?.ready ?? Promise.resolve(); @@ -798,8 +789,6 @@ export class Publisher { * @internal */ async runSubscribe(msg: Subscribe, stream: Stream) { - // Charged before anything else, as in Rust. A peer past the cap loses the session. - const interest = this.#interests.acquire(msg.broadcast, msg.track, "subscribe"); // Serve only what our grant lets us publish, and stop once it no longer does. The // watch is armed before the first check and held to the end, so a shrink while the // broadcast resolves is never missed. @@ -816,7 +805,6 @@ export class Publisher { }); } finally { watch.dispose(); - interest.release(); } } @@ -999,30 +987,25 @@ export class Publisher { * @internal */ async runFetch(msg: Fetch, stream: Stream) { - this.#fetches.acquire(); + if (!hasTrackStream(this.version)) { + stream.writer.reset(new Error("fetch requires moq-lite-05 or newer")); + return; + } + // Like a subscription, the fetch holds its watch until the last frame: a group can stay + // open as long as its track, so a check at accept alone would keep serving after a shrink. + const revoked = unauthorized(msg.broadcast); + let fetched: group.Consumer | undefined; + const watch = this.#gate.watch(msg.broadcast, () => { + console.debug(`fetch revoked: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group}`); + fetched?.close(revoked); + stream.abort(revoked); + }); try { - if (!hasTrackStream(this.version)) { - stream.writer.reset(new Error("fetch requires moq-lite-05 or newer")); - return; - } - // Like a subscription, the fetch holds its watch until the last frame: a group can stay - // open as long as its track, so a check at accept alone would keep serving after a shrink. - const revoked = unauthorized(msg.broadcast); - let fetched: group.Consumer | undefined; - const watch = this.#gate.watch(msg.broadcast, () => { - console.debug(`fetch revoked: broadcast=${msg.broadcast} track=${msg.track} group=${msg.group}`); - fetched?.close(revoked); - stream.abort(revoked); + await this.#serveFetch(msg, stream, watch, (group) => { + fetched = group; }); - try { - await this.#serveFetch(msg, stream, watch, (group) => { - fetched = group; - }); - } finally { - watch.dispose(); - } } finally { - this.#fetches.release(); + watch.dispose(); } } @@ -1448,11 +1431,7 @@ export class Publisher { * @internal */ async runTrackInfo(msg: TrackMessage, stream: Stream) { - // Charged before anything else and held with the track, as in Rust. A peer past the - // cap loses the session. - const interest = this.#interests.acquire(msg.broadcast, msg.track, "track"); const hold = new AbortController(); - hold.signal.addEventListener("abort", () => interest.release(), { once: true }); // Armed before the first check and held until the answer is acknowledged, so a grant // that shrinks while the answer is blocked on flow control resets it, never sends it. const watch = this.#gate.watch(msg.broadcast, () => { @@ -1510,9 +1489,6 @@ export class Publisher { * @internal */ async runTracks(msg: TracksMessage, stream: Stream) { - // Charged before anything else and held until the listing ends, as in Rust. A peer past - // the cap loses the session. - const interest = this.#interests.acquire(msg.broadcast, "", "tracks"); const cancel = new AbortController(); const watch = this.#gate.watch(msg.broadcast, () => { console.debug(`tracks revoked: broadcast=${msg.broadcast}`); @@ -1565,7 +1541,6 @@ export class Publisher { } finally { tracks?.close(); watch.dispose(); - interest.release(); } } diff --git a/js/net/src/lite/tracks.test.ts b/js/net/src/lite/tracks.test.ts index 78d3fbc1901..dd7c6a09c28 100644 --- a/js/net/src/lite/tracks.test.ts +++ b/js/net/src/lite/tracks.test.ts @@ -223,7 +223,7 @@ test("a peer's malformed listing closes the session", async () => { test("a listing past the announces cap is reset, and the session stays", async () => { const cap = 3; - const { subscriber, listed, closing } = handPlayed({ limits: { announces: cap, subscriptions: 10, fetches: 10 } }); + const { subscriber, listed, closing } = handPlayed({ limits: { announces: cap, requests: 10 } }); const tracks = subscriber.consume(Path.from("room")).tracks(); const peer = await listed(); for (let i = 0; i <= cap; i++) await peer.send({ kind: "added", name: `t${i}`, info }); diff --git a/js/net/src/session-limits.test.ts b/js/net/src/session-limits.test.ts index f6883d3fe8f..eed9170df27 100644 --- a/js/net/src/session-limits.test.ts +++ b/js/net/src/session-limits.test.ts @@ -1,168 +1,20 @@ import { expect, spyOn, test } from "bun:test"; import { SessionCode, SessionError } from "./error.ts"; -import { HopSchema, randomHop } from "./hop.ts"; -import { NativeSession } from "./ietf/adapter.ts"; -import { Fetch as IetfFetch } from "./ietf/fetch.ts"; -import { Publisher as IetfPublisher } from "./ietf/publisher.ts"; +import { HopSchema } from "./hop.ts"; import { RequestOk } from "./ietf/request.ts"; -import { Subscribe as IetfSubscribe } from "./ietf/subscribe.ts"; import { SubscribeNamespaceEntry } from "./ietf/subscribe_namespace.ts"; import { Subscriber as IetfSubscriber } from "./ietf/subscriber.ts"; import { Version as IetfVersion } from "./ietf/version.ts"; import { AnnounceOk, encodeAnnounceBroadcast } from "./lite/announce.ts"; -import { Fetch as LiteFetch } from "./lite/fetch.ts"; -import { Publisher as LitePublisher } from "./lite/publisher.ts"; -import { Subscribe as LiteSubscribe } from "./lite/subscribe.ts"; import { Subscriber as LiteSubscriber } from "./lite/subscriber.ts"; import { Version as LiteVersion } from "./lite/version.ts"; import { createMockTransportPair } from "./mock.ts"; -import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import { Stream, Writer } from "./stream.ts"; -import { Timescale } from "./time.ts"; import type { Limits } from "./util/limits.ts"; -import { wireOf } from "./wire.ts"; // Small caps keep each case fast. -const limits: Limits = { announces: 8, subscriptions: 4, fetches: 4 }; - -// A publisher serving one announced broadcast whose tracks are never accepted, so every -// subscription stays live, waiting on the track, without timers or media. With `track`, the -// "video" track exists but never produces a group, so a FETCH waits on the group instead. -function pendingPublisher(protocol: "lite" | "ietf", options: { track?: boolean } = {}) { - const pair = createMockTransportPair(); - const closing = spyOn(pair.server, "close"); - const quiet = [ - spyOn(console, "warn").mockImplementation(() => {}), - spyOn(console, "debug").mockImplementation(() => {}), - ]; - const origin = new OriginProducer(); - const path = Path.from("test"); - const broadcast = origin.createBroadcast(path); - broadcast.announce(); - if (options.track) broadcast.createTrack("video", { timescale: Timescale.MILLI }); - // Serve tracks on demand, then never answer the request. - void wireOf(broadcast).requested(); - const version = protocol === "lite" ? LiteVersion.DRAFT_06 : IetfVersion.DRAFT_22; - const publisher = - protocol === "lite" - ? new LitePublisher(pair.server, LiteVersion.DRAFT_06, randomHop(), origin.consume(), undefined, limits) - : new IetfPublisher({ - quic: pair.server, - session: new NativeSession(pair.server, IetfVersion.DRAFT_22, false), - publish: origin.consume(), - requiresSolicitation: false, - limits, - }); - const pending: Promise[] = []; - let draining = false; - const writes: (() => void)[] = []; - // Shaped like the WebTransportError a requester's RESET_STREAM fails the read with. - const reset = Object.assign(new Error("cancelled"), { source: "stream", streamErrorCode: null }); - - // Subscribe once, returning a cancel that resets the requester's half. - const subscribe = (id: number): (() => void) => { - let requester!: ReadableStreamDefaultController; - const stream = new Stream({ - version, - readable: new ReadableStream({ - start(controller) { - requester = controller; - }, - }), - writable: new WritableStream(), - }); - const request = - publisher instanceof LitePublisher - ? publisher.runSubscribe( - new LiteSubscribe({ id: BigInt(id), broadcast: path, track: "video", priority: 0 }), - stream, - ) - : publisher.runSubscribe( - new IetfSubscribe({ - requestId: BigInt(id * 2), - trackNamespace: path, - trackName: "video", - subscriberPriority: 0, - }), - stream, - ); - pending.push(request.catch(() => {})); - return () => requester.error(reset); - }; - - // Each wire request has its own slot even when its track and group are identical. - const fetch = (id: number): (() => void) => { - let requester!: ReadableStreamDefaultController; - const stream = new Stream({ - version, - readable: new ReadableStream({ - start(controller) { - requester = controller; - }, - }), - writable: new WritableStream({ - write() { - if (draining) return; - return new Promise((resolve) => writes.push(resolve)); - }, - }), - }); - const request = - publisher instanceof LitePublisher - ? publisher.runFetch(new LiteFetch({ broadcast: path, track: "video", group: 0, priority: 0 }), stream) - : publisher.runFetch(new IetfFetch({ requestId: BigInt(id * 2) }), stream); - pending.push(request.catch(() => {})); - return () => requester.error(new Error("cancelled")); - }; - - const close = async () => { - draining = true; - for (const release of writes) release(); - broadcast.close(); - origin.close(); - await Promise.all(pending); - publisher.close(); - pair.client.close(); - pair.server.close(); - closing.mockRestore(); - for (const spy of quiet) spy.mockRestore(); - }; - - return { closing, pending, subscribe, fetch, close }; -} - -test.each(["lite", "ietf"] as const)("%s refuses the first subscription beyond the session cap", async (protocol) => { - const session = pendingPublisher(protocol); - try { - for (let id = 0; id < limits.subscriptions; id++) session.subscribe(id); - expect(session.closing).not.toHaveBeenCalled(); - session.subscribe(limits.subscriptions); - expect(session.closing).toHaveBeenCalledTimes(1); - expect(session.closing.mock.calls[0]?.[0]?.closeCode).toBe(SessionCode.TooManyRequests); - } finally { - await session.close(); - } -}); - -test.each(["lite", "ietf"] as const)( - "%s releases subscriptions cancelled before the track is answered", - async (protocol) => { - const session = pendingPublisher(protocol); - try { - const cancels = []; - for (let id = 0; id < limits.subscriptions; id++) cancels.push(session.subscribe(id)); - for (const cancel of cancels) cancel(); - // Every cancelled request ends without the track ever being answered. - await Promise.all(session.pending); - - session.subscribe(limits.subscriptions); - expect(session.closing).not.toHaveBeenCalled(); - } finally { - await session.close(); - } - }, -); +const limits: Limits = { announces: 8, requests: 4 }; test.each(["lite", "ietf"] as const)("%s closes on the first advertisement beyond its cap", async (protocol) => { const pair = createMockTransportPair(); @@ -249,35 +101,3 @@ test.each(["lite", "ietf"] as const)("%s closes on the first advertisement beyon debug.mockRestore(); } }); - -test.each(["lite", "ietf"] as const)("%s caps each FETCH before shared lookup", async (protocol) => { - const session = pendingPublisher(protocol); - try { - for (let id = 0; id < limits.fetches; id++) session.fetch(id); - expect(session.closing).not.toHaveBeenCalled(); - session.fetch(limits.fetches); - expect(session.closing).toHaveBeenCalledTimes(1); - expect(session.closing.mock.calls[0]?.[0]?.closeCode).toBe(SessionCode.TooManyRequests); - } finally { - await session.close(); - } -}); - -test.each([ - ["an unanswered", false], - ["a missing group's", true], -] as const)("lite returns FETCH slots when %s requester leaves", async (_, track) => { - const session = pendingPublisher("lite", { track }); - try { - for (let round = 0; round < 3; round++) { - const cancels = Array.from({ length: limits.fetches }, (_, id) => session.fetch(id)); - // Let each lookup settle into its wait first: the track's answer, or a group that never comes. - await new Promise((resolve) => setTimeout(resolve, 0)); - for (const cancel of cancels) cancel(); - await Promise.all(session.pending); - expect(session.closing).not.toHaveBeenCalled(); - } - } finally { - await session.close(); - } -}); diff --git a/js/net/src/util/limits.test.ts b/js/net/src/util/limits.test.ts index e338b7a9913..2f5c7ba0172 100644 --- a/js/net/src/util/limits.test.ts +++ b/js/net/src/util/limits.test.ts @@ -1,6 +1,6 @@ import { expect, mock, test } from "bun:test"; import { SessionCode } from "../error.ts"; -import { Interests, SlotMap, Slots } from "./limits.ts"; +import { SlotMap, Slots } from "./limits.ts"; test("a full budget closes the session, and released slots can be used again", () => { const close = mock(() => {}); @@ -28,59 +28,16 @@ test("a slot map charges each key once and releases on delete and clear", () => map.set("e", 1); }); -test.each(["track", "subscribe"] as const)("paired interests release the right charge when %s ends first", (first) => { - const interests = new Interests(new Slots(2, { reason: "full" })); - const track = interests.acquire("broadcast", "video", "track"); - const subscribe = interests.acquire("broadcast", "video", "subscribe"); - const other = interests.acquire("broadcast", "audio", "subscribe"); - expect(() => interests.acquire("broadcast", "video", "track")).toThrow(); - const held = { track, subscribe }; - held[first].release(); - expect(() => interests.acquire("other", "video", "subscribe")).toThrow(); - held[first === "track" ? "subscribe" : "track"].release(); - held[first].release(); - const replacement = interests.acquire("other", "video", "subscribe"); - expect(() => interests.acquire("third", "video", "track")).toThrow(); - other.release(); - replacement.release(); - interests.acquire("third", "video", "track").release(); -}); - -test("duplicate interests count individually, and a failed acquisition leaves no charge", () => { - const interests = new Interests(new Slots(2, { reason: "full" })); - const tracks = [interests.acquire("path", "track", "track"), interests.acquire("path", "track", "track")]; - const subscribes = [ - interests.acquire("path", "track", "subscribe"), - interests.acquire("path", "track", "subscribe"), - ]; - expect(() => interests.acquire("path", "track", "subscribe")).toThrow(); - tracks[0]?.release(); - subscribes[0]?.release(); - const other = interests.acquire("path", "other", "track"); - expect(() => interests.acquire("path", "track", "subscribe")).toThrow(); - for (const held of [...tracks, ...subscribes, other]) held.release(); - interests.acquire("path", "track", "subscribe").release(); -}); - -test("each listing is charged on top of the interest under the same name", () => { - const interests = new Interests(new Slots(3, { reason: "full" })); - const track = interests.acquire("room", "", "track"); - const subscribe = interests.acquire("room", "", "subscribe"); - const listing = interests.acquire("room", "", "tracks"); - // A replacement listing must fit under the cap too, until the older is gone. - const replacement = interests.acquire("room", "", "tracks"); - expect(() => interests.acquire("room", "", "tracks")).toThrow(); - listing.release(); - replacement.release(); - track.release(); - subscribe.release(); - for (const held of [0, 1, 2].map(() => interests.acquire("room", "", "tracks"))) held.release(); -}); - test("tryAcquire refuses at the cap without closing the session", () => { const slots = new Slots(1, { reason: "full" }); expect(slots.tryAcquire()).toBe(true); expect(slots.tryAcquire()).toBe(false); slots.release(); expect(slots.tryAcquire()).toBe(true); + // A held entry counts past the cap, so tryAcquire waits for it too. + slots.hold(); + slots.release(); + expect(slots.tryAcquire()).toBe(false); + slots.release(); + expect(slots.tryAcquire()).toBe(true); }); diff --git a/js/net/src/util/limits.ts b/js/net/src/util/limits.ts index 904a412edc1..e8c412db895 100644 --- a/js/net/src/util/limits.ts +++ b/js/net/src/util/limits.ts @@ -1,21 +1,19 @@ import { Session, SessionCode } from "../error.ts"; /** - * How many live requests a peer may hold on one session. Unlike moq-net's `session::Limits`, it - * still caps subscriptions and FETCHes: `@moq/qmux` returns stream credit on accept, not on close, - * so credit alone does not bound them here. @internal + * How many live requests a peer may hold on one session, mirroring moq-net's `session::Limits`. + * Subscriptions and FETCHes have no cap: each holds a bidi stream, so stream credit bounds them. + * @internal */ export interface Limits { /** Broadcasts (moq-lite) or namespaces (moq-transport) the peer may have announced to us. */ announces: number; - /** Subscriptions the peer may hold on our broadcasts, a TRACK stream sharing one with its SUBSCRIBE. */ - subscriptions: number; - /** FETCH requests the peer may hold, counted before requests for one group coalesce. */ - fetches: number; + /** Subscriptions and listing PUBLISHes a moq-transport peer may hold before listed tracks are skipped. */ + requests: number; } -/** The default limits, matching moq-net's announces. @internal */ -export const LIMITS: Readonly = Object.freeze({ announces: 100_000, subscriptions: 10_000, fetches: 10_000 }); +/** The default limits, matching moq-net's. @internal */ +export const LIMITS: Readonly = Object.freeze({ announces: 100_000, requests: 20_000 }); /** A session's count of live peer requests, which closes the session past its cap. @internal */ export class Slots { @@ -45,6 +43,11 @@ export class Slots { return true; } + /** Count one entry something else already bounds, past the cap too, so `tryAcquire` sees it. */ + hold() { + this.#live += 1; + } + /** Give back `count` slots. */ release(count = 1) { this.#live -= count; @@ -76,79 +79,3 @@ export class SlotMap extends Map { super.clear(); } } - -type Kind = "track" | "subscribe" | "tracks"; -type Counts = Record; - -// A TRACK stream and its SUBSCRIBE are one interest; each listing is charged on top. -function cost(counts: Counts): number { - return Math.max(counts.track, counts.subscribe) + counts.tracks; -} - -/** - * A session's subscription cap, charged per track as moq-net does. - * - * A TRACK stream held open and the SUBSCRIBE that follows it are one interest in the track, - * so a track costs the larger of its two counts: a peer at the cap can still subscribe to - * the tracks it holds. A TRACKS listing is charged under the empty track name on top of - * that, each until it is gone, replaced or not. - * - * @internal - */ -export class Interests { - #slots: Slots; - #tracks = new Map>(); - - constructor(slots: Slots) { - this.#slots = slots; - } - - /** Charge one request for `broadcast`'s `track` until the returned handle is released. */ - acquire(broadcast: string, track: string, kind: Kind): Interest { - let tracks = this.#tracks.get(broadcast); - const counts = tracks?.get(track) ?? { track: 0, subscribe: 0, tracks: 0 }; - const before = cost(counts); - counts[kind] += 1; - if (cost(counts) > before) { - try { - this.#slots.acquire(); - } catch (err) { - counts[kind] -= 1; - throw err; - } - } - if (!tracks) { - tracks = new Map(); - this.#tracks.set(broadcast, tracks); - } - tracks.set(track, counts); - return new Interest(() => this.#release(broadcast, track, kind)); - } - - #release(broadcast: string, track: string, kind: Kind) { - const tracks = this.#tracks.get(broadcast); - const counts = tracks?.get(track); - if (!tracks || !counts) throw new Error("interest already released"); - const before = cost(counts); - counts[kind] -= 1; - if (cost(counts) < before) this.#slots.release(); - if (cost(counts) === 0) { - tracks.delete(track); - if (tracks.size === 0) this.#tracks.delete(broadcast); - } - } -} - -/** One charged request, released at most once. @internal */ -export class Interest { - #release?: () => void; - - constructor(release: () => void) { - this.#release = release; - } - - release() { - this.#release?.(); - this.#release = undefined; - } -} diff --git a/js/qmux/MIGRATE.md b/js/qmux/MIGRATE.md index eaba7493ae0..1c718c56255 100644 --- a/js/qmux/MIGRATE.md +++ b/js/qmux/MIGRATE.md @@ -4,6 +4,10 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Accepted bidi streams hold credit until both halves end + +A peer's bidirectional stream returns its `MAX_STREAMS` credit only once it is accepted and both halves have ended, as in QUIC. Close or abort the writable of every accepted bidi stream, even one you never write to, or the peer eventually runs out of streams to open. + ### Only QMux draft-00, 01 and 02 are supported The legacy `webtransport` subprotocol and implicit fallback are removed. Offer a supported `qmux-NN` or `qmux-NN.` subprotocol. Reject upgrades when `selectSubprotocol` returns `undefined`; accepted sockets without a supported negotiated value now reject `ready` and `closed`. Pass `{ protocol }` to `Session.accept` if the host does not expose it on the socket. diff --git a/js/qmux/src/session.test.ts b/js/qmux/src/session.test.ts index 6441ad1eb13..5135e362c08 100644 --- a/js/qmux/src/session.test.ts +++ b/js/qmux/src/session.test.ts @@ -1647,6 +1647,69 @@ describe("Session integration (scripted peer)", () => { session.close(); }); + // Stream credit bounds open streams only if a bidi stream holds it until both halves end. + describe("a half-closed bidi stream keeps its stream-count credit", () => { + const id = Stream.Id.create(0n, Stream.Dir.Bi, true); + + async function accept() { + const { session, peer } = connect({ maxStreamsBidi: 1n }); + await session.ready; + peer.send({ type: "transport_parameters", params: peerParams() }); + peer.send({ type: "stream", id, data: new Uint8Array([1]), fin: false }); + const accepted = await session.incomingBidirectionalStreams.getReader().read(); + if (accepted.done || !accepted.value) throw new Error("expected an incoming stream"); + return { session, peer, stream: accepted.value }; + } + + // A ping round trip flushes any MAX_STREAMS the frames before it produced. + async function expectCredit(peer: FakePeer, count: number) { + const pongs = peer.count("ping_response"); + peer.send({ type: "ping_request", sequence: BigInt(pongs + 1) }); + await waitFor(() => peer.count("ping_response") > pongs); + expect(peer.count("max_streams_bidi")).toBe(count); + } + + test("until our send half finishes", async () => { + const { session, peer, stream } = await accept(); + peer.send({ type: "stream", id, offset: 1n, data: new Uint8Array(), fin: true }); + await expectCredit(peer, 0); + + await stream.writable.close(); + await waitFor(() => peer.count("max_streams_bidi") === 1); + session.close(); + }); + + test("until the peer's send half finishes", async () => { + const { session, peer, stream } = await accept(); + await stream.writable.close(); + await expectCredit(peer, 0); + + peer.send({ type: "stream", id, offset: 1n, data: new Uint8Array(), fin: true }); + await waitFor(() => peer.count("max_streams_bidi") === 1); + session.close(); + }); + + test("until our send half is reset", async () => { + const { session, peer, stream } = await accept(); + await stream.readable.cancel(); + await expectCredit(peer, 0); + + await stream.writable.abort(); + await waitFor(() => peer.count("max_streams_bidi") === 1); + session.close(); + }); + + test("until the peer stops our send half", async () => { + const { session, peer } = await accept(); + peer.send({ type: "reset_stream", id, code: VarInt.from(0), finalSize: 1n }); + await expectCredit(peer, 0); + + peer.send({ type: "stop_sending", id, code: VarInt.from(0) }); + await waitFor(() => peer.count("max_streams_bidi") === 1); + session.close(); + }); + }); + // Regression: a STREAM frame delivered by the read loop after the session has // closed used to hit the already-closed incoming-stream controllers, and the // throw escaped as an unhandled rejection because #handleStreamFrame was an diff --git a/js/qmux/src/session.ts b/js/qmux/src/session.ts index 426e2a4a77a..a3f355b26d5 100644 --- a/js/qmux/src/session.ts +++ b/js/qmux/src/session.ts @@ -199,8 +199,8 @@ interface PendingIncomingStream { * mark; `desiredSize` is only a backpressure signal. QMux cannot stop reading * its underlying byte stream, so keep new streams in this explicit queue and * transfer ownership only in response to an application read. The peer's - * MAX_STREAMS credit is replenished by `onAccept`, which bounds this pending - * queue to the stream count we advertised. + * MAX_STREAMS credit waits on `onAccept`, which bounds this pending queue to the + * stream count we advertised. */ class IncomingStreamQueue { readonly readable: ReadableStream; @@ -518,6 +518,8 @@ interface StreamFlowState { recvMax: bigint; recvOffset: bigint; recvConsumed: bigint; + /** Runs once when the send half ends, for a peer stream whose credit waits on it. */ + onSendEnd?: () => void; } /** A `WebTransport` session carried over a WebSocket, for hosts without native WebTransport. */ @@ -1372,12 +1374,20 @@ export default class Session implements WebTransport { // For send side on bidi: peer's bidi_local is our send limit const sendMax = frame.id.dir === Stream.Dir.Bi ? this.#peerParams.initialMaxStreamDataBidiLocal : 0n; + // Stream credit returns once the application has the stream and both halves have + // ended, as in QUIC, so it bounds the streams a peer holds open. + let holds = frame.id.dir === Stream.Dir.Bi ? 3 : 2; + const release = () => { + if (--holds === 0) this.#replenishStreamCredit(frame.id.dir); + }; + this.#streamFlow.set(streamId, { sendCredit: new Credit(sendMax), sendOffset: 0n, recvMax, recvOffset: 0n, recvConsumed: 0n, + onSendEnd: frame.id.dir === Stream.Dir.Bi ? release : undefined, }); // Validate recv flow control before accepting @@ -1387,23 +1397,6 @@ export default class Session implements WebTransport { return; } - let accepted = false; - let terminal = false; - let streamCreditReplenished = false; - const maybeReplenishStreamCredit = () => { - if (!accepted || !terminal || streamCreditReplenished) return; - streamCreditReplenished = true; - this.#replenishStreamCredit(frame.id.dir); - }; - const onAccept = () => { - accepted = true; - maybeReplenishStreamCredit(); - }; - const onTerminal = () => { - terminal = true; - maybeReplenishStreamCredit(); - }; - const recvStream = new RecvStream( (bytes) => { this.#accountStreamConsumed(streamId, bytes); @@ -1420,7 +1413,7 @@ export default class Session implements WebTransport { this.#recvStreams.delete(streamId); this.#maybeDeleteStreamFlow(streamId); }, - onTerminal, + release, ); this.#recvStreams.set(streamId, recvStream); recv = recvStream; @@ -1434,14 +1427,14 @@ export default class Session implements WebTransport { // Incoming bidirectional stream const writer = this.#sendWritable(frame.id, DEFAULT_SEND_ORDER); - if (!this.#incomingBidirectionalQueue.push({ readable: reader, writable: writer }, onAccept, onDrop)) { + if (!this.#incomingBidirectionalQueue.push({ readable: reader, writable: writer }, release, onDrop)) { // Keep the refused stream registered until FIN/RESET so in-flight frames // cannot repeatedly reopen it. RecvStream rejects subsequent pushes, // letting the receive path account them as deliberately discarded. onDrop(new Error("incoming stream acceptor cancelled")); } } else { - if (!this.#incomingUnidirectionalQueue.push(reader, onAccept, onDrop)) { + if (!this.#incomingUnidirectionalQueue.push(reader, release, onDrop)) { onDrop(new Error("incoming stream acceptor cancelled")); } } @@ -1548,7 +1541,6 @@ export default class Session implements WebTransport { const stopped = new StreamError(streamCode(frame.code.value), "STOP_SENDING"); stream.error(stopped); - this.#sendStreams.delete(streamId); this.#scheduler?.dropStream(streamId, stopped); // A write parked on stream credit isn't interrupted by error(); fail it here, // even though a live receive half keeps the flow state around. @@ -1561,6 +1553,13 @@ export default class Session implements WebTransport { finalSize: this.#sendFinalSize(streamId), }); + this.#endSend(streamId); + } + + /** End a stream's send half once its FIN or RESET_STREAM is sent. */ + #endSend(streamId: bigint) { + if (!this.#sendStreams.delete(streamId)) return; + this.#streamFlow.get(streamId)?.onSendEnd?.(); this.#maybeDeleteStreamFlow(streamId); } @@ -1827,8 +1826,7 @@ export default class Session implements WebTransport { finalSize: this.#sendFinalSize(streamId), }); - this.#sendStreams.delete(streamId); - this.#maybeDeleteStreamFlow(streamId); + this.#endSend(streamId); }; const writable = new WritableStream({ start: (controller) => { @@ -1865,9 +1863,8 @@ export default class Session implements WebTransport { return; } - this.#sendStreams.delete(streamId); this.#scheduler?.forget(streamId); - this.#maybeDeleteStreamFlow(streamId); + this.#endSend(streamId); }, }); this.#attachSendOrder(writable, streamId, sendOrder); diff --git a/quest/m1/README.md b/quest/m1/README.md index fe110159436..4f0a362a51b 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -90,7 +90,6 @@ audit's receive-memory, priority, and lease bugs rank ahead of that reshape. - [SUBSCRIBE_TRACKS follows credit](/quest/m1/listing-credit.md) - a listing holds state in proportion to what the peer reads, not to the catalog - [Cancelled moq-transport requests stop](/quest/m1/ietf-request-cancel.md) - a cancelled request releases its state even when the peer withholds credit - [SUBSCRIBE setup sees a cancel](/quest/m1/ietf-setup-cancel.md) - a subscriber that cancels mid-setup releases upstream demand and its slot at once -- [JS qmux stream credit](/quest/m1/js-qmux-credit.md) - credit returns when both halves close, so `@moq/net` drops its caps - [Every close records its cause](/quest/m1/close-cause.md) - protocol-violation closes and abandoned SETUP replies record why - [Empty segments refused](/quest/m1/empty-segments.md) - `room//cam` fails loud instead of naming `room/cam` - [Readers follow restarts](/quest/m1/sink-restart-follow.md) - every moq-cli and moq-gst reader, moqsrc and `moq play` included, follows instances through `announced_broadcast` and an epochless `Restart` where its output can mark it; otherwise it stays on the old instance and fails once that route dies diff --git a/quest/m1/js-qmux-credit.md b/quest/m1/js-qmux-credit.md deleted file mode 100644 index 64ad8b2bed1..00000000000 --- a/quest/m1/js-qmux-credit.md +++ /dev/null @@ -1,19 +0,0 @@ -# [S] JS qmux returns stream credit when both halves close - -## Goal - -`@moq/qmux` returns MAX_STREAMS credit for a peer stream only once both of -its halves are terminal, as QUIC and Rust qmux do, so stream credit bounds -open streams. `@moq/net` then drops its own subscription and FETCH caps to -mirror Rust. - -## Plan - -`maybeReplenishStreamCredit` in `js/qmux/src/session.ts` fires once a stream -is accepted and its receive half is terminal. A bidirectional subscription -whose receive half ends while its send half stays open frees credit early. -Because of this, [moq-dev/moq#6467](https://github.com/moq-dev/moq/pull/6467) -kept the `@moq/net` caps. Fix the credit, then remove those caps, with a test -that a half-closed stream still holds its credit. - -Public API: `@moq/net` loses its subscription and FETCH caps. Wire: none. diff --git a/sh/gh/bench-report.py b/sh/gh/bench-report.py index 88319665f75..a524b5f4a49 100644 --- a/sh/gh/bench-report.py +++ b/sh/gh/bench-report.py @@ -134,13 +134,6 @@ def measurement_unit(column): def javascript_file(path): entries, headers = [], None for line_number, line in enumerate(path.read_text().splitlines(), 1): - # The interests sweep reports dimensions and elapsed time in one line. - interests = re.fullmatch(r'broadcasts=(\d+) tracks=(\d+) acquire\+release=([\d.eE+-]+) us', line) - if interests: - broadcasts, tracks, elapsed = interests.groups() - entries.append({'name': f'js/{path.stem}/broadcasts={broadcasts}/tracks={tracks}/acquire+release', - 'unit': 'us', 'value': value(float(elapsed))}) - continue row = [field.strip() for field in next(csv.reader([line]))] if len(row) > 1 and any(measurement_unit(field) for field in row): if len(set(row)) != len(row): diff --git a/sh/gh/bench-report.test.py b/sh/gh/bench-report.test.py index 5027052dd4f..334a9691285 100644 --- a/sh/gh/bench-report.test.py +++ b/sh/gh/bench-report.test.py @@ -35,14 +35,12 @@ def test_values_refuse_smoke_and_malformed_reports(self): with self.assertRaises(ValueError): report.checked(entries) - def test_js_multiple_schemas_and_interests(self): + def test_js_multiple_schemas(self): source = self.root / 'requests.csv' source.write_text('bun install v1.3\npaths,active_us,settled_us\n4,1.25,2.5\noperation,ns_per_op\nclose,0\n') entries = report.javascript_file(source) self.assertEqual([entry['value'] for entry in entries], [1.25, 2.5, 0]) self.assertEqual(entries[0]['name'], 'js/requests/paths=4/active_us') - source.write_text('broadcasts=32 tracks=8 acquire+release=1.250 us\n') - self.assertEqual(report.javascript_file(source)[0]['value'], 1.25) for text in ('Testing case\nSuccess\n', 'paths,time_us\n4\n', 'paths,time_us\n4,nan\n'): source.write_text(text) with self.assertRaises(ValueError): diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index b86d907d26b..78e279bc869 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -130,7 +130,7 @@ case "$1" in done ;; js) - for benchmark in broadcasts request-ids requests forward reader frames drift varint lite-varint track resume interests priority subscribe-drop subscribe-tracks refresh observed compression; do + for benchmark in broadcasts request-ids requests forward reader frames drift varint lite-varint track resume priority subscribe-drop subscribe-tracks refresh observed compression; do javascript "$benchmark" just js bench "$benchmark" done javascript catalog just js catalog-bench