Repository navigation
fix(net): reclaim an idle open group without waiting for a write #3419
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
5645bd5
c57b098
23ee416
193c3e0
53dd250
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 |
|---|---|---|
|
|
@@ -11,6 +11,10 @@ import { Timescale, type Timestamp } from "./time.ts"; | |
|
|
||
| export type { Datagram } from "./datagram.ts"; | ||
|
|
||
| // The largest delay setTimeout keeps: anything more truncates to a signed 32-bit int | ||
| // and fires right away. | ||
| const MAX_TIMEOUT_MS = 2 ** 31 - 1; | ||
|
|
||
| /** Default {@link Info.maxAge} window (milliseconds) when the publisher does not set one. */ | ||
| export const DEFAULT_MAX_AGE_MS = 5000; | ||
|
|
||
|
|
@@ -365,11 +369,16 @@ export class Producer { | |
| #sequence: TrackSequence = { next: 0 }; | ||
|
|
||
| // Recently written source groups, retained for replay to late subscribers and | ||
| // pruned once closed and older than the cache window. Each entry tracks the mirror | ||
| // pruned once idle for longer than the cache window. Each entry tracks the mirror | ||
| // it handed to every sink so eviction can drop them too: otherwise a slow consumer | ||
| // that never reads would pin old groups (and their frame bytes) forever. | ||
| #cache: CachedGroup[] = []; | ||
|
|
||
| // Wakeup for the next entry due to age out. Writes settle retention inline, but a | ||
| // publisher that stalls stops writing, so without this an abandoned group (and any | ||
| // reader parked in it) would wait for a write that never comes. | ||
| #pruneTimer?: ReturnType<typeof setTimeout>; | ||
|
|
||
| // One independent downstream state per live subscriber. | ||
| #sinks = new Set<TrackState>(); | ||
|
|
||
|
|
@@ -533,6 +542,11 @@ export class Producer { | |
|
|
||
| // Drop a cached group's mirror from every sink so no consumer can pin it. | ||
| #evict(entry: CachedGroup): void { | ||
| // Reclaiming a group the publisher never closed ends it as the gap it is, before | ||
| // the mirrors below would otherwise report a clean finish to a reader that had | ||
| // drained it. The usual case, an already-closed group aging out, keeps its own | ||
| // terminal state. | ||
| if (!entry.group.isClosed) entry.group.close(new Lagged()); | ||
| for (const [sink, mirror] of entry.mirrors) { | ||
| hooks.evictGroup(mirror); | ||
| sink.groups.mutate((groups) => { | ||
|
|
@@ -545,20 +559,65 @@ export class Producer { | |
| entry.mirrors.clear(); | ||
| } | ||
|
|
||
| // Evict cached groups that are closed and older than the cache window. | ||
| // The one group retention never takes: the newest, while it is still open. That is | ||
| // the live edge a publisher is appending to, and a track may legitimately keep it | ||
| // open across a long quiet stretch (a catalog snapshot, a JSON stream). Every other | ||
| // open group is an abandoned one, and ages out like a closed one. | ||
| #liveEdge(): GroupProducer | undefined { | ||
| const latest = this.#cache.at(-1)?.group; | ||
| return latest?.closed.peek() === undefined ? latest : undefined; | ||
|
Comment on lines
+566
to
+568
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.
When AGENTS.md reference: AGENTS.md:L152-L156 Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| // Evict cached groups idle for longer than the cache window. Idle means nothing | ||
| // written, so an abandoned open group ages out instead of pinning its buffer (and | ||
| // any reader parked in it) forever. | ||
| #prune(): void { | ||
| const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS; | ||
| const cutoff = performance.now() - maxAgeMs; | ||
| const live = this.#liveEdge(); | ||
|
|
||
| const retained: CachedGroup[] = []; | ||
| for (const entry of this.#cache) { | ||
| if (entry.time >= cutoff || entry.group.closed.peek() === undefined) { | ||
| if (entry.group === live || entry.group.activity >= cutoff) { | ||
|
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.
When an incoming non-latest group takes longer than AGENTS.md reference: AGENTS.md:L152-L156 Useful? React with 👍 / 👎. |
||
| retained.push(entry); | ||
| continue; | ||
| } | ||
| this.#evict(entry); | ||
| } | ||
| this.#cache = retained; | ||
| this.#schedulePrune(); | ||
| } | ||
|
|
||
| // Arm the wakeup for the next entry due to age out, replacing any pending one. | ||
| // Writes settle retention inline, so this only has to cover the case no write | ||
| // follows. Cheap to over-arm: an entry written since is retained and re-armed. | ||
| #schedulePrune(): void { | ||
| clearTimeout(this.#pruneTimer); | ||
| this.#pruneTimer = undefined; | ||
| if (this.#state.closed.peek() !== undefined) return; | ||
|
|
||
| // One pass, no intermediate arrays: this runs on every publish, and a spread | ||
| // over the cache would also cap how many groups a track can hold. | ||
| const live = this.#liveEdge(); | ||
| let oldest: number | undefined; | ||
| for (const entry of this.#cache) { | ||
| if (entry.group === live) continue; | ||
| if (oldest === undefined || entry.group.activity < oldest) oldest = entry.group.activity; | ||
| } | ||
| if (oldest === undefined) return; | ||
|
Comment on lines
+603
to
+607
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.
When the cache contains only its open live group, this skips the sole entry and returns without arming a timer. Normal helpers such as AGENTS.md reference: AGENTS.md:L152-L156 Useful? React with 👍 / 👎. |
||
|
|
||
| const maxAgeMs = this.#state.info.peek()?.maxAge ?? DEFAULT_MAX_AGE_MS; | ||
| // setTimeout truncates its delay to a signed 32-bit int, so a longer window | ||
| // would fire immediately and spin. Wake at the cap instead and re-arm: #prune | ||
| // retains anything still fresh, so the extra wakeups are the only cost. | ||
| const delay = Math.min(MAX_TIMEOUT_MS, Math.max(0, oldest + maxAgeMs - performance.now())); | ||
| const timer = setTimeout(() => { | ||
| this.#pruneTimer = undefined; | ||
| this.#prune(); | ||
| }, delay); | ||
| // A cache prune is never a reason to hold a Node/Bun process open. | ||
| (timer as unknown as { unref?: () => void }).unref?.(); | ||
| this.#pruneTimer = timer; | ||
| } | ||
|
|
||
| // Retain a source group and fan it out to every live sink. | ||
|
|
@@ -675,6 +734,8 @@ export class Producer { | |
| for (const sink of this.#sinks) sink.final = this.#state.final; | ||
| } | ||
| closeTrackState(this.#state, abort); | ||
| clearTimeout(this.#pruneTimer); | ||
| this.#pruneTimer = undefined; | ||
| for (const { group } of this.#cache) group.close(abort); | ||
| for (const sink of this.#sinks) { | ||
| for (const group of sink.groups.peek()) group.close(abort); | ||
|
|
||
This file was deleted.
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 updated relay documentation now promises write-independent sweeping, but the
--cache-durationhelp generated fromrs/moq-relay/src/cache.rs:57-61still says expiry runs only on writes and that stalled publishers retain their cache. Operators inspectingmoq-relay --helptherefore receive the opposite behavior from this page; update that field's doc comment along with this behavioral change. (Written by GPT-5.6 Sol)AGENTS.md reference: AGENTS.md:L128-L128
Useful? React with 👍 / 👎.
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.
Agreed, fixed. This is the Cross-Package Sync rule and I missed it:
doc/bin/relay/config.mdwas updated for the new behavior while the--helptext it mirrors was not, so the two gave opposite answers.rs/moq-relay/src/cache.rsnow says the same thing as the docs page: the window holds whether or not a publisher is still writing, since the relay sweeps every cached track on a wall-clock cadence, andcapacityis the budget that depends on writes. The live-edge caveat stays.(Written by claude-opus-5[1m])