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: 1 addition & 1 deletion js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 0 additions & 43 deletions js/net/bench/interests.ts

This file was deleted.

2 changes: 1 addition & 1 deletion js/net/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 12 additions & 19 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path.Valid>();
#trackClaims = new Map<string, "publish" | number>();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions js/net/src/ietf/subscribe_tracks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Grant | undefined>(undefined)) {
function fixture(version: IetfVersion, requests = 10, grant = new Signal<Grant | undefined>(undefined)) {
const pair = createMockTransportPair(ALPN.DRAFT_22);
const origin = new Origin();
const session = new NativeSession(pair.server, version, false);
Expand All @@ -30,7 +30,7 @@ function fixture(version: IetfVersion, subscriptions = 10, grant = new Signal<Gr
publish: origin.consume(),
requiresSolicitation: true,
grant,
limits: { announces: 10, subscriptions, fetches: 10 },
limits: { announces: 10, requests },
});
const tasks: Promise<void>[] = [];
const streams: Stream[] = [];
Expand Down
57 changes: 16 additions & 41 deletions js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand All @@ -474,11 +468,8 @@ export class Publisher {
hop: Hop,
publish?: OriginConsumer,
auth?: { grant: Getter<Grant | undefined>; ready: Promise<void> },
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();
Expand Down Expand Up @@ -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.
Expand All @@ -816,7 +805,6 @@ export class Publisher {
});
} finally {
watch.dispose();
interest.release();
}
}

Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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}`);
Expand Down Expand Up @@ -1565,7 +1541,6 @@ export class Publisher {
} finally {
tracks?.close();
watch.dispose();
interest.release();
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/net/src/lite/tracks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
Loading
Loading