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
12 changes: 9 additions & 3 deletions doc/concept/moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ A subscriber watching under a root sees advertisements named relative to that
root. The pattern scope filters which prefixes are visible without changing a
route's prefix. Announce events carry the covered path, captures, and what
happened to it: Rust
`announce::Update { path, captures: Option<Vec<Pattern>>, route, kind }` and
TypeScript `Announce.Update { path, captures, route, kind }`, where the kind is
`announce::Update { prefix, captures: Option<Vec<Pattern>>, route, kind }` and
TypeScript `Announce.Update { prefix, captures, route, kind }`, where the kind is
announced, updated (a reprice in place), or retracted. Captures are present when
the announced prefix pins every wildcard in the most-specific matching scope
member. The Rust consumer is a `Stream` and the TypeScript one an async iterable.
Expand All @@ -145,7 +145,13 @@ Announcements are hints; requests are the authority. When a subscriber asks
for a covered path the advertiser will not serve, the advertiser refuses that
request rather than narrowing the claim, and no message narrows a route. Token
scope is any pattern union; the session asks for each member's literal head on
the prefix-only wire and filters locally.
the prefix-only wire and filters locally. In Rust and TypeScript,
`origin.scope(root, patterns)` narrows the handle's permissions and presents paths
relative to `root`. Nested scopes intersect with their parent. A session receiving
into that scoped origin asks for the literal heads of its allowed patterns,
coalescing duplicate or nested heads. An unscoped origin still asks for the empty
prefix, covering every namespace. These subscriptions include hidden routes;
each local announcement reader decides whether to show them.

```typescript
import { Path } from "@moq/net";
Expand Down
13 changes: 9 additions & 4 deletions js/net/bench/broadcasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ const updates = 32;
let checksum = 0;

console.log("scope,routes,observers,update_us");
for (const scope of ["unscoped", "distinct-scopes"] as const) {
for (const scope of ["unscoped", "distinct-scopes", "producer-scopes", "rooted-producers"] as const) {
for (const routeCount of routeCounts) {
for (const observerCount of observerCounts) {
const origin = new Producer();
const handles = Array.from({ length: routeCount }, (_, index) =>
origin.dynamic(Path.from(`room/${index}`)),
);
const touched = Path.from("room/0");
const touched = Path.from(scope === "rooted-producers" ? "0" : "room/0");
let notifications = 0;
const disposes = Array.from({ length: observerCount }, (_, index) =>
origin
.broadcasts(scope === "unscoped" ? undefined : Path.Pattern.parse(`room/**/tag-${index}`))
(scope === "producer-scopes"
? origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse(`room/**/tag-${index}`)]))
: scope === "rooted-producers"
? origin.scope(Path.from("room"), new Path.Patterns([Path.Pattern.parse(`**/tag-${index}`)]))
: origin
)
.broadcasts(scope === "distinct-scopes" ? Path.Pattern.parse(`room/**/tag-${index}`) : undefined)
.subscribe((routes) => {
if (routes.get(touched) === undefined) throw new Error("touched route disappeared");
notifications++;
Expand Down
97 changes: 96 additions & 1 deletion js/net/src/connection/forward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FakeSession {
});
}

announced(): Announce.Consumer {
announced(_scope?: Path.Pattern, _options?: Announce.Options): Announce.Consumer {
return this.announces.consume();
}

Expand Down Expand Up @@ -188,3 +188,98 @@ test("a request replaced across one coalesced wakeup still gets answered", async
second.close();
origin.close();
});

test("scoped origins subscribe to disjoint literal heads with hidden routes", () => {
const origin = new OriginProducer().scope(
Path.empty(),
new Path.Patterns(["room/*/chat", "room/nested/**", "other/**"].map(Path.Pattern.parse)),
);
const session = new FakeSession();
const interests: { scope: string | undefined; hidden: boolean | undefined }[] = [];
session.announced = (scope?: Path.Pattern, options?: Announce.Options) => {
interests.push({ scope: scope?.text, hidden: options?.hidden });
return session.announces.consume();
};
forwardAnnounced(session.session, origin);
expect(interests).toEqual([
{ scope: "other/**", hidden: true },
{ scope: "room/**", hidden: true },
]);
session.die();
origin.close();
});

test("an unscoped origin retains its subscription to every namespace", () => {
const origin = new OriginProducer();
const session = new FakeSession();
const interests: string[] = [];
session.announced = (scope?: Path.Pattern) => {
interests.push(scope?.text ?? "**");
return session.announces.consume();
};
forwardAnnounced(session.session, origin);
expect(interests).toEqual(["**"]);
session.die();
origin.close();
});

test("a scoped session filters announcements and blind requests under its root", async () => {
const origin = new OriginProducer();
const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*/chat")]));
const session = new FakeSession();
forwardAnnounced(session.session, scoped);
for (const prefix of ["room/alice/video", "room/alice/chat"]) {
session.announces.append({
prefix: Path.from(prefix),
captures: undefined,
kind: "announced",
route: Route.default,
});
}
const allowed = origin.request(Path.from("tenant/room/bob/chat"));
const denied = origin.request(Path.from("tenant/room/bob/video"));
await settle();
expect([...origin.broadcasts().peek().keys()]).toEqual([Path.from("tenant/room/alice/chat")]);
expect(session.consumes(Path.from("room/bob/chat"))).toBe(1);
expect(session.consumes(Path.from("room/bob/video"))).toBe(0);
const routed = origin.request(Path.from("tenant/room/alice/chat"));
await settle();
expect(session.consumes(Path.from("room/alice/chat"))).toBe(1);
expect(routed.active.peek()).toBeDefined();
allowed.close();
denied.close();
routed.close();
session.die();
origin.close();
});

test("one failed scoped interest leaves the other routes live until the session ends", async () => {
const origin = new OriginProducer().scope(
Path.empty(),
new Path.Patterns(["a/**", "b/**"].map(Path.Pattern.parse)),
);
const session = new FakeSession();
const sources = new Map<string, Announce.Producer>();
session.announced = (scope?: Path.Pattern) => {
const producer = new Announce.Producer();
sources.set(scope?.text ?? "**", producer);
return producer.consume();
};
forwardAnnounced(session.session, origin);
for (const name of ["a", "b"]) {
sources
.get(`${name}/**`)
?.append({ prefix: Path.from(name), captures: undefined, kind: "announced", route: Route.default });
}
await settle();
expect(origin.broadcasts().peek().size).toBe(2);
sources.get("a/**")?.close(new Error("namespace rejected"));
await settle();
expect([...origin.broadcasts().peek().keys()]).toEqual([Path.from("b")]);
expect(origin.discovery.peek()).toBe(false);
session.die();
await settle();
expect(origin.broadcasts().peek().size).toBe(0);
expect(origin.discovery.peek()).toBeUndefined();
origin.close();
});
101 changes: 53 additions & 48 deletions js/net/src/connection/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { type Dispose, race } from "@moq/signals";
import { isActive } from "../announced.ts";
import type { Dynamic, Producer as OriginProducer, RequestSlot } from "../origin.ts";
import type * as Path from "../path.ts";
import * as Path from "../path.ts";
import { wireOf } from "../wire.ts";
import type { Established } from "./established.ts";

Expand Down Expand Up @@ -35,6 +35,7 @@ export function forwardAnnounced(conn: Established, origin: OriginProducer): voi
let detach = originWire.attach(conn.discovery);

let dead = false;
let discovering = conn.discovery;
void conn.closed.then(() => {
dead = true;
detach();
Expand All @@ -47,57 +48,61 @@ export function forwardAnnounced(conn: Established, origin: OriginProducer): voi
return;
}

// Hidden routes are mirrored too; each local reader opts in on its own.
const announced = conn.announced(undefined, { hidden: true });
const inserted = new Map<Path.Valid, Dynamic>();

// End the stream the moment the session closes rather than waiting for the wire to
// error it, so the retractions below land promptly.
void conn.closed.then(() => announced.close());

void (async () => {
let failure: unknown;
try {
for (;;) {
const event = await announced.next();
if (!event) break;

if (isActive(event.kind)) {
const existing = inserted.get(event.prefix);
if (existing) {
existing.update(event.route);
for (const prefix of originWire.interests()) {
// Hidden routes are mirrored too; each local reader opts in on its own.
const announced = conn.announced(Path.Pattern.subtree(prefix), { hidden: true });
const inserted = new Map<Path.Valid, Dynamic>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Deduplicate broad routes across interest streams

With multiple disjoint heads and a peer advertising a prefix above them, such as "" for a/** and b/**, every conn.announced stream emits the same advertisement, but this per-stream map creates a separate Dynamic and drive task for each copy. The IETF subscriber explicitly sends one broad advertisement to every matching consumer in #attachAnnounce, so route storage and repricing work grow with heads times routes; if one stream ends, closing the selected duplicate can also rematerialize active requests through an identical entry. Share or reference-count received prefixes across these streams, and benchmark interest heads against routes. (Written by GPT-5.6 Sol)

AGENTS.md reference: AGENTS.md:L35-L35

Useful? React with 👍 / 👎.


// End the stream the moment the session closes rather than waiting for the wire to
// error it, so the retractions below land promptly.
void conn.closed.then(() => announced.close());

void (async () => {
let failure: unknown;
try {
for (;;) {
const event = await announced.next();
if (!event) break;
if (!originWire.accepts(event.prefix)) continue;

if (isActive(event.kind)) {
const existing = inserted.get(event.prefix);
if (existing) {
existing.update(event.route);
} else {
const handle = originWire.receive(event.prefix, event.route);
inserted.set(event.prefix, handle);
void drive(handle, conn);
}
} else {
const handle = originWire.receive(event.prefix, event.route);
inserted.set(event.prefix, handle);
void drive(handle, conn);
const handle = inserted.get(event.prefix);
inserted.delete(event.prefix);
handle?.close();
}
} else {
const handle = inserted.get(event.prefix);
inserted.delete(event.prefix);
handle?.close();
}
} catch (err) {
// The session died mid-stream, or the relay refused or reset the stream. The
// cleanup below retracts everything this stream fed either way.
failure = err;
} finally {
for (const handle of inserted.values()) handle.close();
inserted.clear();
announced.close();

// Discovery ended while the session lives, and nothing reopens the stream on this
// connection. Downgrade the attachment rather than leaving the origin claiming a
// discovery that no longer works: announcement-gated consumers would wait forever
// on a table this session can no longer fill. Now they fall back to standing
// requests, which this session still answers.
if (!dead && discovering) {
discovering = false;
console.warn("broadcast discovery failed; broadcasts resolve on request only.", failure);
detach();
detach = originWire.attach(false);
}
}
} catch (err) {
// The session died mid-stream, or the relay refused or reset the stream. The
// cleanup below retracts everything this stream fed either way.
failure = err;
} finally {
for (const handle of inserted.values()) handle.close();
inserted.clear();
announced.close();

// Discovery ended while the session lives, and nothing reopens the stream on this
// connection. Downgrade the attachment rather than leaving the origin claiming a
// discovery that no longer works: announcement-gated consumers would wait forever
// on a table this session can no longer fill. Now they fall back to standing
// requests, which this session still answers.
if (!dead) {
console.warn("broadcast discovery failed; broadcasts resolve on request only.", failure);
detach();
detach = originWire.attach(false);
}
}
})();
})();
}
}

/**
Expand Down
55 changes: 55 additions & 0 deletions js/net/src/connection/scope.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { expect, test } from "bun:test";
import * as Ietf from "../ietf/index.ts";
import * as Lite from "../lite/index.ts";
import { createMockTransportPair } from "../mock.ts";
import { Producer } from "../origin.ts";
import * as Path from "../path.ts";
import { withTimeout } from "../util/timeout.ts";
import { accept } from "./accept.ts";
import { connect } from "./connect.ts";

for (const protocol of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_19]) {
test(`scoped origins discover multiple prefixes over ${protocol}`, async () => {
const pair = createMockTransportPair(protocol);
const url = new URL("https://localhost/test");
const source = new Producer();
const destination = new Producer();
const publish = source.scope(Path.from("server"), new Path.Patterns([Path.Pattern.all()]));
const consume = destination.scope(
Path.from("client"),
new Path.Patterns(["room/**", "other/**"].map(Path.Pattern.parse)),
);
const broadcasts = ["room/live", "room/.hidden", "other/live", "outside"].map((path) => {
const broadcast = publish.createBroadcast(Path.from(path));
broadcast.announce();
return broadcast;
});
const [client, server] = await Promise.all([
connect({ url, transport: pair.client, consume }),
accept({ url, transport: pair.server, publish: publish.consume() }),
]);
const announced = destination.announced(undefined, { hidden: true });
try {
const received: string[] = [];
for (let index = 0; index < 3; index++) {
const update = await withTimeout(announced.next(), 1000, "scoped announcement did not arrive");
if (!update) throw new Error("announcement stream ended before replay");
expect(update.kind).toBe("announced");
received.push(update.prefix);
}
expect(received.sort()).toEqual(["client/other/live", "client/room/.hidden", "client/room/live"]);
expect([...consume.broadcasts().peek().keys()].sort()).toEqual([
Path.from("other/live"),
Path.from("room/live"),
]);
} finally {
announced.close();
client.close();
server.close();
for (const broadcast of broadcasts) broadcast.close();
source.close();
destination.close();
await Promise.all([client.closed, server.closed]);
}
});
}
Loading
Loading