Repository navigation
feat(net): js/net reports a lite-07 route's rtt #8553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dbecf95
d55e551
fe454ec
b0f7bf0
e431c99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { Duration } from "@moq/clock"; | ||
| import { LinkRtt } from "./link_rtt.ts"; | ||
|
|
||
| // Mirrors the tests of `route/rtt.rs` in rs/moq-net. | ||
|
|
||
| const ms = (n: number) => Duration.millis(n); | ||
| const asMs = (rtt: Duration | undefined) => rtt?.asMillis(); | ||
| const feed = (link: LinkRtt, reads: (Duration | undefined)[]) => reads.map((rtt) => asMs(link.read(rtt))); | ||
|
|
||
| test("the first read reports at once", () => { | ||
| expect(asMs(new LinkRtt().read(ms(30)))).toBe(30); | ||
| }); | ||
|
|
||
| test("a queue shorter than the window reports nothing", () => { | ||
| const link = new LinkRtt(); | ||
| link.read(ms(30)); | ||
| const queued = Array.from({ length: 29 }, (_, i) => ms(30 + (i + 1) * 3)); | ||
| expect(feed(link, queued).every((report) => report === 30)).toBe(true); | ||
| expect(asMs(link.read(ms(31)))).toBe(30); | ||
| }); | ||
|
|
||
| test("a rise reports once the window passes it", () => { | ||
| const link = new LinkRtt(); | ||
| link.read(ms(30)); | ||
| const reports = feed(link, Array(30).fill(ms(55))); | ||
| expect(reports.slice(0, 29).every((report) => report === 30)).toBe(true); | ||
| expect(reports[29]).toBe(55); | ||
| }); | ||
|
|
||
| test("a drop reports at once", () => { | ||
| const link = new LinkRtt(); | ||
| link.read(ms(80)); | ||
| expect(asMs(link.read(ms(40)))).toBe(40); | ||
| }); | ||
|
|
||
| test("a change within the band reports nothing", () => { | ||
| let link = new LinkRtt(); | ||
| link.read(ms(10)); | ||
| expect(asMs(link.read(ms(5)))).toBe(10); | ||
| expect(asMs(link.read(ms(4)))).toBe(4); | ||
| link = new LinkRtt(); | ||
| link.read(ms(100)); | ||
| expect(asMs(link.read(ms(75)))).toBe(100); | ||
| expect(asMs(link.read(ms(74)))).toBe(74); | ||
| }); | ||
|
|
||
| test("a transport without rtt reports none and starts over", () => { | ||
| const link = new LinkRtt(); | ||
| link.read(ms(80)); | ||
| expect(link.read(undefined)).toBeUndefined(); | ||
| expect(asMs(link.read(ms(90)))).toBe(90); | ||
| }); | ||
|
|
||
| test("a window without rtt stops reading", () => { | ||
| const link = new LinkRtt(); | ||
| feed(link, Array(29).fill(undefined)); | ||
| expect(link.reading).toBe(true); | ||
| link.read(undefined); | ||
| expect(link.reading).toBe(false); | ||
| }); | ||
|
|
||
| test("a fraction of a millisecond rounds up", () => { | ||
| expect(asMs(new LinkRtt().read(Duration.micros(300)))).toBe(1); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { Duration } from "@moq/clock"; | ||
|
|
||
| /** How many reads the minimum covers. */ | ||
| const WINDOW = 30; | ||
| /** The smallest change reported. */ | ||
| const FLOOR = Duration.millis(5); | ||
|
|
||
| /** | ||
| * A link's min RTT, as the route layer adds it to the routes learned over the link. | ||
| * Mirrors `route::LinkRtt` in rs/moq-net. | ||
| * | ||
| * Reads a link's smoothed RTT once per {@link LinkRtt.INTERVAL} and reports the lowest of | ||
| * the last 30 reads in whole milliseconds, changed only once it moves past the larger of | ||
| * 5 ms and a quarter of the value reported. | ||
| * | ||
| * The smoothed RTT follows every queue, and a lifetime minimum never rises after a path | ||
| * change; a windowed minimum ignores a queue shorter than the window and turns a path | ||
| * change into one new value. | ||
| * | ||
| * @internal | ||
| */ | ||
| export class LinkRtt { | ||
| /** How often to {@link read} the smoothed RTT. */ | ||
| static readonly INTERVAL = Duration.secs(1); | ||
|
|
||
| #reads: Duration[] = []; | ||
| #reported: Duration | undefined; | ||
| /** Reads in a row that found no RTT. */ | ||
| #misses = 0; | ||
|
|
||
| /** The min RTT last reported. */ | ||
| get reported(): Duration | undefined { | ||
| return this.#reported; | ||
| } | ||
|
|
||
| /** | ||
| * Whether to keep reading: false once a window of reads found no RTT, since a transport | ||
| * that reports none that long, past a first ping, never will. | ||
| */ | ||
| get reading(): boolean { | ||
| return this.#misses < WINDOW; | ||
| } | ||
|
|
||
| /** | ||
| * Take one read of the smoothed RTT, `undefined` when the transport reports none, and | ||
| * return the min RTT to report. | ||
| */ | ||
| read(rtt: Duration | undefined): Duration | undefined { | ||
| if (rtt === undefined) { | ||
| this.#reads = []; | ||
| this.#reported = undefined; | ||
| this.#misses += 1; | ||
| return undefined; | ||
| } | ||
| this.#misses = 0; | ||
| if (this.#reads.length === WINDOW) this.#reads.shift(); | ||
| this.#reads.push(rtt); | ||
| const lowest = this.#reads.reduce((a, b) => a.min(b)); | ||
| const min = Duration.millis(Math.ceil(Math.floor(lowest.asMicros()) / 1000)); | ||
| const reported = this.#reported; | ||
| if (!reported || min.absDiff(reported).gt(reported.div(4).max(FLOOR))) this.#reported = min; | ||
| return this.#reported; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| import { Duration, yieldNow } from "@moq/clock"; | ||
| import { Duration, sleep, yieldNow } from "@moq/clock"; | ||
| import { type Dispose, Effect, type GetPromise, type Getter, race, Signal } from "@moq/signals"; | ||
| import * as announce from "../announced.ts"; | ||
| import type { Grant } from "../auth.ts"; | ||
| import type { AuthSession } from "../auth_session.ts"; | ||
| import * as broadcast from "../broadcast.ts"; | ||
| import type { Drain } from "../connection/goaway.ts"; | ||
| import type { Probe as ProbeStats } from "../connection/stats.ts"; | ||
| import { type Probe as ProbeStats, transportStats } from "../connection/stats.ts"; | ||
| import { BroadcastCache } from "../consume.ts"; | ||
| import type * as datagram from "../datagram.ts"; | ||
| import * as DatagramStream from "../datagram_stream.ts"; | ||
|
|
@@ -74,6 +74,7 @@ import { closeViolation } from "./error.ts"; | |
| import { Fetch as FetchMessage } from "./fetch.ts"; | ||
| import { type Carrier, type Claim, Floors } from "./floors.ts"; | ||
| import { type Group as GroupMessage, readFrames } from "./group.ts"; | ||
| import { LinkRtt } from "./link_rtt.ts"; | ||
| import { Probe } from "./probe.ts"; | ||
| import { ProbeLevel, type Setup } from "./setup.ts"; | ||
| import { StreamId } from "./stream.ts"; | ||
|
|
@@ -933,6 +934,54 @@ export class Subscriber { | |
| let synced = false; | ||
| // Set once the stream lets go of its routes, which then show nothing anew. | ||
| let closing = false; | ||
| // This link's min RTT, read once a second while the peer has a route, as Rust's | ||
| // `Routed::sample`: no route, no timer, and a pause restarts the window. | ||
| let link = new LinkRtt(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| let linkRtt: Duration | undefined; | ||
| let sampler: AbortController | undefined; | ||
| // The running sampler's first read, settled once it landed or the sampler stopped. | ||
| let firstRead = Promise.resolve(); | ||
| // Returns whether it started a sampler. | ||
| const sample = (): boolean => { | ||
| if (routes.size === 0 && sampler) { | ||
| sampler.abort(); | ||
| sampler = undefined; | ||
| link = new LinkRtt(); | ||
| linkRtt = undefined; | ||
| } else if (routes.size > 0 && !sampler) { | ||
| const ctrl = new AbortController(); | ||
| sampler = ctrl; | ||
| const first = Promise.withResolvers<void>(); | ||
| firstRead = first.promise; | ||
| void readLink(ctrl.signal, link, first.resolve); | ||
| return true; | ||
| } | ||
| return false; | ||
| }; | ||
| const readLink = async (signal: AbortSignal, window: LinkRtt, read: () => void) => { | ||
| try { | ||
| while (!signal.aborted && window.reading) { | ||
| const rtt = (await transportStats(this.#quic)).rtt; | ||
| if (signal.aborted || closing || announced.closed.peek() !== undefined) return; | ||
| // A transport reporting 0 has not measured one. | ||
| const now = window.read(rtt?.isZero() ? undefined : rtt); | ||
| if (now !== linkRtt) { | ||
| linkRtt = now; | ||
| // RTT never selects a route, so the consume cache keeps its picks: only the | ||
| // reported routes change, each path once. | ||
| const paths = new Set<Path.Valid>(); | ||
| for (const wire of routes.values()) for (const path of wire.view.claims.keys()) paths.add(path); | ||
| for (const path of paths) refresh(path, false); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This refresh only reaches the session's internal route stream. Technical details# rtt-only updates are invisible to the public listing
## Affected sites
- `js/net/src/lite/subscriber.ts:961-968` — an RTT change is announced only as a `RouteEvent` of kind `update`, with `available` unchanged.
- `js/net/src/announced.ts:358-361` — `Consumer.next()` continues on any `update`/`restart` whose `available` matches the last report, so the event never becomes a public `Event`.
- `js/net/src/connection/pool.ts:361` — `Session.announced()` is `list(routes())`, so the public connection listing sees the filtered stream; `routes()` is `@internal`.
- `js/net/src/hop.ts:115-123` — `RouteInfo.rtt` reads as a live value, but the only public reader sees it as of `present`.
- `rs/moq-net/src/lite/subscriber/routed.rs:1029` — Rust reads the link synchronously before the first claims are served, so `AnnounceEvent::Present` already carries the rtt.
## Required outcome
- A `Connection.announced()` consumer sees the rtt for a route whose claims arrive before the first `getStats()` resolves, without waiting for an unrelated availability change.
- Behaviour stays equal to Rust: either the value is known by the first `present` (Rust's approach) or the listing reports route changes (Rust deliberately does not).
## Suggested approach (optional)
- Read the link once when the announce stream starts, or await the first read before the first `refresh` of a route's claims: that mirrors Rust's synchronous first read without adding a timer for peers with no route.
- If rtt is meant to stay a present-time snapshot, say so in `RouteInfo.rtt` and in the PR description instead of promising an update, and test what the public listing actually sees (the current `announced.test.ts` assertion only checks that the key exists).
## Open questions for the human (optional)
- Should a route-info change become a public listing event (a new or widened `Announce` event), or should the listing stay availability-only as in Rust?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in e431c99. Confirmed: (Written by Claude Fable 5 with high) |
||
| } | ||
| read(); | ||
| await sleep(LinkRtt.INTERVAL, { signal }); | ||
| } | ||
| } catch { | ||
| // Aborted: the routes went or the stream ended. | ||
| } finally { | ||
| read(); | ||
| } | ||
| }; | ||
|
|
||
| const route = (id: bigint): Route => { | ||
| const found = routes.get(id); | ||
|
|
@@ -966,6 +1015,11 @@ export class Subscriber { | |
| cost: claim.cost + wire.advert.metric + 1n, | ||
| node: wire.node, | ||
| available: !wire.held, | ||
| // The relay's sum plus this link's own, unknown while either is. | ||
| rtt: | ||
| wire.advert.rtt !== undefined && linkRtt !== undefined | ||
| ? linkRtt.checkedAdd(Duration.millis(Number(wire.advert.rtt))) | ||
| : undefined, | ||
| }), | ||
| ); | ||
|
|
||
|
|
@@ -1090,8 +1144,14 @@ export class Subscriber { | |
| owner: {}, | ||
| }; | ||
| routes.set(id, wire); | ||
| const started = sample(); | ||
| nodes.add(msg.node); | ||
| this.#floors.join(msg.node, wire.carrier); | ||
| // The link's first read lands before the route's first claims show, as Rust | ||
| // reads the link before it serves them: a listing reports a path's route when | ||
| // it turns present and not again for an RTT, so a read landing after would | ||
| // never reach it. | ||
| if (started) await race([firstRead, announced.closed]); | ||
| break; | ||
| } | ||
| case "routeUpdate": { | ||
|
|
@@ -1115,6 +1175,7 @@ export class Subscriber { | |
| claims.delete(id); | ||
| } | ||
| routes.delete(msg.id); | ||
| sample(); | ||
| nodes.delete(wire.node); | ||
| // What its last floor certified stays, so no other stream's lagging claim revives. | ||
| this.#floors.leave(wire.node, wire.carrier); | ||
|
|
@@ -1220,6 +1281,7 @@ export class Subscriber { | |
| } finally { | ||
| stopDrain?.(); | ||
| closing = true; | ||
| sampler?.abort(); | ||
| for (const wire of routes.values()) { | ||
| this.#floors.leave(wire.node, wire.carrier); | ||
| for (const path of wire.view.claims.keys()) this.#consumes.withdraw(path, wire.owner); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pre-existing comment directly above (line 4067) documents the
a lite-07 stream reports each node's claim with claimstest at line 4092; this insertion now leaves it heading the rtt test instead. Move it down with the test it describes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e431c99: the comment is back above the claims test.
(Written by Claude Fable 5 with high)