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
1 change: 1 addition & 0 deletions js/net/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ export class Consumer {
hooks.expireGroup = (group, expiry) => {
group.#expiry = expiry;
};
hooks.groupExpiry = (group) => group.#expiry;
hooks.groupFailed = (group) => group.#failed();
hooks.recoverGroup = (group, recover) => {
group.#recover = recover;
Expand Down
12 changes: 10 additions & 2 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,11 @@ export const hooks: {
* subscriber's own record. Mirrors the Rust crate-private `track::Subscriber::record_mut`.
*/
wire: (subscriber: Subscriber) => WireRecord;
/** Whether this subscriber's budget gives up on `key`, judged by its successor; see {@link expireGroup}. */
staleAt: (subscriber: Subscriber, key: bigint) => Expiry;
/**
* Whether this subscriber's budget, or `budget` in milliseconds when given, gives up on `key`,
* judged by its successor; see {@link expireGroup}.
*/
staleAt: (subscriber: Subscriber, key: bigint, budget?: number) => Expiry;
/** A group the subscriber's track still caches, as a fresh read handle. */
cachedGroup: (subscriber: Subscriber, key: bigint, from?: number) => GroupConsumer | undefined;
/**
Expand Down Expand Up @@ -1009,6 +1012,8 @@ export const hooks: {
arriveGroup: (group: GroupConsumer, at: Instant) => void;
/** Keep applying a subscription's drift policy after it hands a group out. */
expireGroup: (group: GroupConsumer, expiry: Expiry) => void;
/** The drift policy a group carries, if any; see {@link expireGroup}. */
groupExpiry: (group: GroupConsumer) => Expiry | undefined;
/** The failure that ends a group's reader, as its `closed` would settle, without building that handle. */
groupFailed: (group: GroupConsumer) => Error | undefined;
/**
Expand Down Expand Up @@ -1416,6 +1421,9 @@ export const hooks: {
expireGroup: () => {
throw new Error("group.ts not loaded");
},
groupExpiry: () => {
throw new Error("group.ts not loaded");
},
groupFailed: () => {
throw new Error("group.ts not loaded");
},
Expand Down
95 changes: 92 additions & 3 deletions js/net/src/lite/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Manual } from "@moq/clock/test";
import { Signal } from "@moq/signals";
import type { Grant } from "../auth.ts";
import { Producer as BroadcastProducer } from "../broadcast.ts";
import { accept as acceptSession, connect as connectSession } from "../connection/index.ts";
import { fromTransport, ProtocolViolation, SessionCode, StreamCode, StreamError, toStreamCode } from "../error.ts";
import { Consumer as GroupConsumer, type Producer as GroupProducer } from "../group.ts";
import { randomHop } from "../hop.ts";
Expand Down Expand Up @@ -3470,6 +3471,7 @@ test("lite draft-07: a fetch sent whole resets as too old when it goes stale una
// The FIN is never acknowledged: the close stays pending until the stream resets.
const inner = accepted.value.writable.getWriter();
let reset: unknown;
const { promise: closing, resolve: closed } = Promise.withResolvers<void>();
const writable = new WritableStream<Uint8Array>({
start(controller) {
controller.signal.addEventListener("abort", () => {
Expand All @@ -3478,7 +3480,10 @@ test("lite draft-07: a fetch sent whole resets as too old when it goes stale una
});
},
write: (chunk) => inner.write(chunk),
close: () => new Promise(() => undefined),
close: () => {
closed();
return new Promise(() => undefined);
},
});
const server = new Stream({ readable: accepted.value.readable, writable, version: Version.DRAFT_07 });

Expand All @@ -3492,8 +3497,7 @@ test("lite draft-07: a fetch sent whole resets as too old when it goes stale una
});
const serving = publisher.runFetch(msg, server);
// The whole group and its FIN go out before any successor arrives.
await client.reader.u8();
await flush();
await closing;
expect(reset).toBeUndefined();

// Then the track runs a second past its successor, with the FIN still unacknowledged.
Expand All @@ -3512,6 +3516,91 @@ test("lite draft-07: a fetch sent whole resets as too old when it goes stale una
}
});

// A relay serving a FETCH for a broadcast it consumes over the wire judges the reply by the
// FETCH's own budget too, against a copy of the track a live reader subscribed, as a Rust relay
// judges the fetch it landed in that copy's cache.
test("lite draft-07: a relayed fetch sent whole resets as too old when it goes stale unacknowledged", async () => {
// Upstream: the broadcast's origin, consumed by the relay over a session.
const upstream = createMockTransportPair(ALPN_07_WIP);
const source = new OriginProducer();
const broadcast = publish(source, Path.from("live"));
const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null });
const frame = (ms: number) => ({ payload: new TextEncoder().encode("x"), timestamp: Timestamp.fromMillis(ms) });
const whole = track.createGroup(0n);
whole.writeFrame(frame(0));
whole.finish();
const url = new URL("https://localhost:4443/test");
const [session, upstreamServer] = await Promise.all([
connectSession({ url, transport: upstream.client }),
acceptSession({ transport: upstream.server, url, publish: source.consume() }),
]);
const front = new Front();
front.serve(wireOf(session).consume(Path.from("live")));

// Downstream: the relay's publisher, answering with the front.
const pair = createMockTransportPair(ALPN_07_WIP);
const origin = new OriginProducer();
const dynamic = origin.claim(Path.from("live"));
void (async () => {
for await (const request of dynamic.requested()) request.serve(front.consumer);
})();
const publisher = new Publisher(pair.server, Version.DRAFT_07, randomHop(), origin.consume());

const client = await Stream.open(pair.client, { version: Version.DRAFT_07 });
const incoming = pair.server.incomingBidirectionalStreams.getReader();
const accepted = await incoming.read();
incoming.releaseLock();
if (accepted.done) throw new Error("publisher never accepted the fetch stream");
// The FIN is never acknowledged: the close stays pending until the stream resets.
const inner = accepted.value.writable.getWriter();
let reset: unknown;
const { promise: closing, resolve: closed } = Promise.withResolvers<void>();
const writable = new WritableStream<Uint8Array>({
start(controller) {
controller.signal.addEventListener("abort", () => {
reset = controller.signal.reason;
void inner.abort(controller.signal.reason);
});
},
write: (chunk) => inner.write(chunk),
close: () => {
closed();
return new Promise(() => undefined);
},
});
const server = new Stream({ readable: accepted.value.readable, writable, version: Version.DRAFT_07 });

// Another downstream reader, whose own budget is far wider, keeps a copy subscribed.
const live = front.consumer.track("video").subscribe({ delay: { max: Duration.millis(3_600_000) } });
try {
const msg = new Fetch({ broadcast: Path.from("live"), track: "video", priority: 0, maxDelay: 500, group: 0n });
const serving = publisher.runFetch(msg, server);
// The whole group and its FIN go out before any successor arrives.
await closing;
expect(reset).toBeUndefined();

// Then the track runs a second past its successor upstream, with the FIN still unacknowledged.
for (const ms of [1000, 2000]) {
const group = track.createGroup(BigInt(ms / 1000));
group.writeFrame(frame(ms));
group.finish();
}
await serving;
expect(toStreamCode(fromTransport(reset))).toBe(StreamCode.Old);
} finally {
live.close();
publisher.close();
client.close();
dynamic.close();
front.consumer.close();
session.close();
upstreamServer.close();
broadcast.close();
source.close();
origin.close();
}
});

test.each([0, 1])("lite draft-07 reports the cached largest position when starting at time %s", async (start) => {
const startGroup = BigInt(start);
const version = Version.DRAFT_07;
Expand Down
59 changes: 59 additions & 0 deletions js/net/src/resume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,65 @@ test("a fetched group continues from the next route once its own fails", async (
b.close();
});

// A wire answers a FETCH with a bare group, which carries no budget of its own: the fetch's own
// budget judges it against a reader's copy of the route, as a Rust relay judges a fetch landed in
// that copy's cache. Nothing newer is known before a reader subscribes one, and a fetch never does.
test.each([
{ budget: "500ms", maxDelay: Duration.millis(500), stale: true, bounded: false },
{ budget: "none", maxDelay: undefined, stale: false, bounded: false },
// Judged against the whole track, as a fetch is, not the window of the reader it picked.
{ budget: "500ms, beside a bounded reader", maxDelay: Duration.millis(500), stale: true, bounded: true },
])(
"a group fetched over a wire goes stale by the fetch's own budget ($budget)",
async ({ maxDelay, stale, bounded }) => {
// Still, so only presentation time judges.
const { installed } = manual();
const front = new Front();
const a = new BroadcastProducer();
const ta = copy(a);
ta.write(0n);
const { route } = counted(a, (key) => {
const answer = new GroupProducer(key);
answer.writeFrame({ payload: enc.encode(`${key}.0`), timestamp: Timestamp.unscaled(answer.start) });
answer.finish();
return answer.consume();
});
front.serve(route);
try {
const group = await front.consumer.track("seq").fetchGroup(0n, { maxDelay });
ta.write(1000n);
await settle();
// Fetch-only: nothing pulls the track, so nothing newer is known here.
expect(ta.producer.demand().wanted.peek()).toBe(false);
expect(group.expired.peek()).toBeUndefined();
// Listening before any copy exists, so a copy subscribed later has to wake it.
const verdict = new Promise<Error>((resolve) => void group.expired.then(resolve));

// A reader of group 0 alone subscribes the first copy, and a live one whose own budget is
// far wider the next.
const first = bounded
? front.consumer.track("seq").subscribe({ ranges: [{ start: { time: 0n }, end: { time: 0n } }] })
: undefined;
const sub = front.consumer.track("seq").subscribe({ delay: { max: Duration.millis(3_600_000) } });
await settle();
expect(group.expired.peek()).toBeUndefined();

// The copy's live edge runs a second past group 0's successor.
ta.write(2000n);
if (stale) expect(await verdict).toMatchObject({ code: StreamCode.Old });
else expect(await isPending(verdict)).toBe(true);

first?.close();
sub.close();
group.close();
} finally {
front.consumer.close();
a.close();
installed.close();
}
},
);

test("a held fetch lets go of the route it resumed onto once its reader releases the group", async () => {
const front = new Front();
const a = new BroadcastProducer();
Expand Down
Loading