From d56151bbf9bed93d4b7e81955e9abd2245271867 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 18:29:03 -0700 Subject: [PATCH 1/3] fix(net): stop idle viewer sessions from waking every 100ms The Session handle held its own send-bandwidth consumer, so the sampler never idled, and each open PROBE stream ticked every 100ms reading the transport stats. Together an idle viewer cost its server ~40 driver polls and ~20 path-stats reads per second. The handle now keeps a weak reference, so only held consumers are demand. PROBE serving follows the supervisor's estimate instead of a timer: the supervisor samples on driver polls that already happen (at most once per 100ms) while a PROBE stream is open, and ProbeServe wakes on a change or its age deadline. Reporting rules (thresholds, decay, max age) are unchanged. An idle viewer now costs one max-age refresh per 10s. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/benches/session.rs | 63 +++++- rs/moq-net/src/client.rs | 20 +- rs/moq-net/src/lite/publisher.rs | 302 ++++++++++++++++++++-------- rs/moq-net/src/lite/session.rs | 5 + rs/moq-net/src/model/bandwidth.rs | 19 ++ rs/moq-net/src/server.rs | 20 +- rs/moq-net/src/session.rs | 101 +++++++++- rs/moq-net/src/time.rs | 10 +- rs/moq-net/tests/idle_session.rs | 89 ++++++++ rs/moq-net/tests/support/harness.rs | 51 ++++- rs/moq-net/tests/support/mock.rs | 40 +++- 11 files changed, 599 insertions(+), 121 deletions(-) create mode 100644 rs/moq-net/tests/idle_session.rs diff --git a/rs/moq-net/benches/session.rs b/rs/moq-net/benches/session.rs index 6183aa58d93..a64cfc36897 100644 --- a/rs/moq-net/benches/session.rs +++ b/rs/moq-net/benches/session.rs @@ -23,6 +23,10 @@ //! one small frame per track per tick. Throughput counts tracks, so a flat //! elements/s across a sweep means the cost per track stays constant. //! +//! `session_idle` times one second of idle viewer sessions, swept over sessions. Each +//! viewer keeps a PROBE stream open, as a browser does, and nothing else happens, so +//! the time is whatever the sessions do on their own. Paused time skips the waiting. +//! //! `session_churn_*` times one request opened and closed by a session that already //! holds others open, swept over sessions and requests held per session. On //! moq-transport 14 every close is credited back through the MAX_REQUEST_ID window, @@ -44,6 +48,7 @@ use bytes::Bytes; use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use moq_net::{Hop, Timestamp, Version, broadcast, cache, group, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; +use support::mock::MockStats; /// A millisecond track: the frames written here carry timestamps. fn timed() -> track::Info { @@ -854,6 +859,62 @@ fn churn(c: &mut Criterion, name: &str, shapes: impl IntoIterator) group.finish(); } +/// Viewer sessions to one relay, each holding a PROBE stream and otherwise idle. +struct IdleRoom { + _viewers: Vec<(MockPair, moq_net::bandwidth::Consumer)>, +} + +impl IdleRoom { + async fn new(sessions: usize) -> Self { + let (relay, driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); + support::harness::spawn(driver); + let mut viewers = Vec::new(); + for _ in 0..sessions { + let mut options = MockConnectOptions::new("moq-lite-06".parse().unwrap()); + options.server_publish = Some(relay.consume()); + // A QUIC backend reports both, so the relay serves PROBE. + options.stats = MockStats { + estimated_send_rate: Some(1_000_000), + rtt: Some(Duration::from_millis(40)), + }; + let pair = connect_mock(options).await; + let mut probe = pair.client.recv_bandwidth().unwrap(); + probe.changed().await.unwrap(); + viewers.push((pair, probe)); + } + Self { _viewers: viewers } + } + + /// Let a second of protocol time pass per iteration. + async fn measure(&self, iters: u64) -> Duration { + let start = Instant::now(); + for _ in 0..iters { + tokio::time::sleep(Duration::from_secs(1)).await; + } + start.elapsed() + } +} + +fn idle(c: &mut Criterion) { + let mut group = c.benchmark_group("session_idle"); + for sessions in [1, 64, 1024] { + group.throughput(Throughput::Elements(sessions as u64)); + // Its own runtime, so its paused clock and its sessions end with the shape. + let rt = runtime(); + let mut room = None; + group.bench_function(BenchmarkId::from_parameter(format!("sessions={sessions}")), |b| { + let room = room.get_or_insert_with(|| { + rt.block_on(async { + tokio::time::pause(); + IdleRoom::new(sessions).await + }) + }); + b.iter_custom(|iters| rt.block_on(room.measure(iters))) + }); + } + group.finish(); +} + /// Withdraw a batch across a full mesh, sweeping both peers and prefixes. /// Paused protocol time lets the quiet-point timeout drain pending work without /// including a real wait in the measured duration. @@ -1043,5 +1104,5 @@ fn session(c: &mut Criterion) { ); } -criterion_group!(benches, session, withdrawal); +criterion_group!(benches, session, withdrawal, idle); criterion_main!(benches); diff --git a/rs/moq-net/src/client.rs b/rs/moq-net/src/client.rs index 3f7e00141c6..47241f4ce86 100644 --- a/rs/moq-net/src/client.rs +++ b/rs/moq-net/src/client.rs @@ -230,6 +230,7 @@ impl Client { goaway: start.goaway, auth: start.auth, setup: start.setup, + estimate: Some(start.estimate), }, )) } @@ -319,7 +320,12 @@ impl Client { v, None, crate::driver::Protocol::Ietf(protocol), - crate::session::Handles { goaway, auth, setup }, + crate::session::Handles { + goaway, + auth, + setup, + estimate: None, + }, )); } Some(ALPN_16) => { @@ -412,7 +418,7 @@ impl Client { .copied() .ok_or(Error::Version)?; - let (recv_bw, protocol, goaway, auth, setup) = match version { + let (recv_bw, protocol, goaway, auth, setup, estimate) = match version { Version::Lite(v) => { let stream = stream.with_version(v); let start = lite::start(lite::Config { @@ -439,6 +445,7 @@ impl Client { start.goaway, start.auth, start.setup, + Some(start.estimate), ) } Version::Ietf(v) => { @@ -479,7 +486,7 @@ impl Client { early_unis: Vec::new(), extensions: self.extensions, })?; - (None, crate::driver::Protocol::Ietf(protocol), goaway, auth, setup) + (None, crate::driver::Protocol::Ietf(protocol), goaway, auth, setup, None) } }; @@ -489,7 +496,12 @@ impl Client { version, recv_bw, protocol, - crate::session::Handles { goaway, auth, setup }, + crate::session::Handles { + goaway, + auth, + setup, + estimate, + }, )) } } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 8e839df7aff..46c9036e5fc 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -14,8 +14,6 @@ use std::{ time::Duration, }; -use crate::transport::Stats; - use crate::{ Error, Hop, Hops, coding::{Decode, DecodeError, Decoder, Encode, Stream, Writer}, @@ -48,6 +46,8 @@ pub(super) struct PublisherConfig { pub auth: crate::auth::Handle, /// What the default acceptor grants the peer's connection credential. pub peer_grant: crate::auth::Grant, + /// The supervisor's estimate, which a PROBE stream follows. + pub estimate: kio::ProducerWeak, /// Whether we dialed: only then does a publication outside our grant abort. pub client: bool, /// Subscriptions the peer may hold at once (`session::Limits::subscriptions`). @@ -83,6 +83,7 @@ struct Shared { // The send time an untimed track's frames carry, since no lite version can mark them // untimed yet (see `wire_timestamp`). runtime: crate::time::Clock, + estimate: kio::ProducerWeak, } /// Largest millisecond duration every implementation can carry losslessly. @@ -200,6 +201,7 @@ impl Publisher { owed: AtomicUsize::new(0), interests: Interests::new(config.subscriptions), runtime: config.runtime.clone(), + estimate: config.estimate, }), runtime: config.runtime, accept, @@ -613,17 +615,25 @@ impl Enforce { } } -/// Serves one PROBE stream: periodic bandwidth estimates until the peer closes -/// its side. +/// Serves one PROBE stream: bandwidth estimates until the peer closes its side. +/// +/// Reports when the supervisor's estimate moves enough to matter, or once the +/// last report ages out, rather than on a timer: an idle session costs nothing. struct ProbeServe { shared: Arc>, runtime: crate::time::Clock, stream: Option>, + /// The supervisor's estimate. Holding it is what keeps the supervisor sampling. + estimate: kio::Consumer, + /// The estimate last weighed, so only a new one wakes this stream. + seen: Option, last_sent: Option<(lite::Probe, crate::time::Instant)>, - next_probe: crate::time::Deadline, + /// When the unsent change becomes worth reporting by age alone. + due: crate::time::Deadline, } impl ProbeServe { + /// The shortest age the bitrate threshold decays from. const PROBE_INTERVAL: Duration = Duration::from_millis(100); const PROBE_MAX_AGE: Duration = Duration::from_secs(10); const PROBE_MAX_DELTA: f64 = 0.25; @@ -644,23 +654,39 @@ impl ProbeServe { } } - /// The bitrate change worth reporting, decaying to zero as the last report - /// ages: a stale estimate is worth refreshing for a smaller move. - fn bitrate_threshold(elapsed: Duration) -> f64 { - let t = elapsed - .as_secs_f64() - .clamp(Self::PROBE_INTERVAL.as_secs_f64(), Self::PROBE_MAX_AGE.as_secs_f64()); - let range = Self::PROBE_MAX_AGE.as_secs_f64() - Self::PROBE_INTERVAL.as_secs_f64(); - Self::PROBE_MAX_DELTA * (Self::PROBE_MAX_AGE.as_secs_f64() - t) / range + /// How old the last report must be before this bitrate change is worth + /// another. The threshold decays linearly from [`Self::PROBE_MAX_DELTA`] at + /// [`Self::PROBE_INTERVAL`] to zero at [`Self::PROBE_MAX_AGE`]: a stale estimate + /// is worth refreshing for a smaller move. + fn bitrate_due(prev: Option, next: Option) -> Duration { + let (Some(prev), Some(next)) = (prev, next) else { + return match prev == next { + true => Self::PROBE_MAX_AGE, + false => Duration::ZERO, + }; + }; + if prev == 0 { + return match next { + 0 => Self::PROBE_MAX_AGE, + _ => Duration::ZERO, + }; + } + let delta = (next as f64 - prev as f64).abs() / prev as f64; + if delta >= Self::PROBE_MAX_DELTA { + return Duration::ZERO; + } + let range = Self::PROBE_MAX_AGE - Self::PROBE_INTERVAL; + Self::PROBE_MAX_AGE - range.mul_f64(delta / Self::PROBE_MAX_DELTA) } fn new(shared: Arc>, runtime: crate::time::Clock, stream: Stream) -> Self { Self { + estimate: shared.estimate.consume(), + seen: None, shared, stream: Some(stream), last_sent: None, - // Send the first probe immediately, then keep an anchored cadence. - next_probe: crate::time::Deadline::at(&runtime, runtime.now()), + due: crate::time::Deadline::new(&runtime), runtime, } } @@ -681,65 +707,94 @@ impl ProbeServe { let mut cx = waiter.context(); loop { - // Deliver the previous estimate before ticking out the next one. + // Deliver the previous estimate before weighing the next one. ready!(stream.writer.poll_flush(&mut cx))?; - // Tick the probe interval, bailing as soon as the peer closes its side. + // Bail as soon as the peer closes its side. if let Poll::Ready(res) = stream.reader.poll_closed(&mut cx) { return Poll::Ready(res); } - ready!(self.next_probe.poll(waiter)); - let next = self - .next_probe - .deadline() - .and_then(|at| at.checked_add(Self::PROBE_INTERVAL)); - self.next_probe.set(next); + + let estimate = match self.seen { + // The first report reads the transport: the supervisor only samples + // for this stream from the next poll on. + None => { + // Mark the current value seen, so only the supervisor's next change wakes us. + self.seen = Some(*self.estimate.read()); + crate::session::Estimate::read(&self.shared.session) + } + Some(seen) => { + let changed = match self.estimate.poll(waiter, |current| match **current != seen { + true => Poll::Ready(**current), + false => Poll::Pending, + }) { + Poll::Ready(Ok(current)) => Some(current), + // The session is ending; age alone can still refresh a report. + Poll::Ready(Err(_)) | Poll::Pending => None, + }; + match changed { + Some(current) => { + self.seen = Some(current); + current + } + None => { + ready!(self.due.poll(waiter)); + *self.estimate.read() + } + } + } + }; // The two fields are independent on the wire, each using 0 for unknown, // so a transport that exposes only one still has something to report. // Anything this version can't carry is dropped here rather than by the // encoder, so it reads as unknown to every check below. - // Scoped so the borrowed stats handle is dropped before mutating the - // stream below. - let report = { - let stats = self.shared.session.stats(); - lite::Probe { - bitrate: stats.estimated_send_rate(), - rtt: self - .shared - .version - .has_probe_rtt() - .then(|| stats.rtt().map(|d| d.as_millis() as u64)) - .flatten(), - } + let report = lite::Probe { + bitrate: estimate.send_rate.map(crate::bandwidth::Rate::as_bps), + rtt: self + .shared + .version + .has_probe_rtt() + .then(|| estimate.rtt.map(|d| d.as_millis() as u64)) + .flatten(), }; // Nothing left to report. Say so once if it retracts a value the peer is // still holding, then stay quiet rather than repeating "unknown" every // time the max age comes around. - if report.bitrate.is_none() && report.rtt.is_none() { - let retracts = self + let unknown = report.bitrate.is_none() && report.rtt.is_none(); + if unknown + && !self .last_sent .as_ref() - .is_some_and(|(prev, _)| prev.bitrate.is_some() || prev.rtt.is_some()); - if !retracts { - continue; - } + .is_some_and(|(prev, _)| prev.bitrate.is_some() || prev.rtt.is_some()) + { + self.due.set(None); + continue; } - let should_send = match &self.last_sent { - None => true, + let now = self.runtime.now(); + let due = match &self.last_sent { + None => Some(now), Some((prev, at)) => { - let elapsed = self.runtime.now().duration_since(*at); - elapsed >= Self::PROBE_MAX_AGE - || Self::moved(prev.bitrate, report.bitrate, Self::bitrate_threshold(elapsed)) - || Self::moved(prev.rtt, report.rtt, Self::PROBE_RTT_DELTA) + let wait = match Self::moved(prev.rtt, report.rtt, Self::PROBE_RTT_DELTA) { + true => Duration::ZERO, + false => Self::bitrate_due(prev.bitrate, report.bitrate), + }; + at.checked_add(wait) } }; - if should_send { + if due.is_some_and(|due| due <= now) { stream.writer.buffer(&report)?; - self.last_sent = Some((report, self.runtime.now())); + self.last_sent = Some((report, now)); + // A retraction is final until something is measured again. + self.due.set(match unknown { + true => None, + false => now.checked_add(Self::PROBE_MAX_AGE), + }); + } else { + self.due.set(due); } } } @@ -5089,6 +5144,7 @@ mod tests { peer_grant: Default::default(), client: false, subscriptions: Default::default(), + estimate: kio::Producer::default().weak(), }); let serving = kio::wait(|waiter| publisher.shared.poll_serving_origin(waiter)).await; @@ -5226,11 +5282,7 @@ mod tests { /// Decode the PROBE messages the publisher wrote. The publisher only replies on /// a stream the subscriber opened, so there is no leading ControlType here. - fn decode_probes(bytes: &[u8]) -> Vec { - decode_probes_version(bytes, Version::Lite05) - } - - fn decode_probes_version(bytes: &[u8], version: Version) -> Vec { + fn decode_probes(bytes: &[u8], version: Version) -> Vec { use crate::coding::Decode as _; let mut slice = bytes; let mut out = Vec::new(); @@ -5240,11 +5292,13 @@ mod tests { out } - /// Build the poll-based PROBE server around a stream opened by the test peer. + /// Build the poll-based PROBE server around a stream opened by the test peer, + /// following `estimate` as it would the supervisor's. fn probe_server( session: SinkSession, stream: Stream, version: Version, + estimate: &kio::Producer, ) -> ProbeServe { let origin = Hop::random().produce(); let (_, goaway) = crate::goaway::Handle::new(true); @@ -5260,10 +5314,58 @@ mod tests { peer_grant: Default::default(), client: false, subscriptions: Default::default(), + estimate: estimate.weak(), }); ProbeServe::new(publisher.shared.clone(), publisher.runtime.clone(), stream) } + /// A PROBE stream served against a transport reporting `stats`. + struct Probing { + estimate: kio::Producer, + log: crate::lite::test_transport::Log, + server: ProbeServe, + version: Version, + _gate: kio::Producer, + } + + impl Probing { + async fn new(stats: crate::lite::test_transport::SinkStats, version: Version) -> Self { + let gate = kio::Producer::new(true); + let mut session = SinkSession::gated_bi(gate.consume()).with_stats(stats); + let log = session.log.clone(); + let stream = Stream::open(&mut session, version).await.unwrap(); + // The supervisor's construction-time snapshot. + let estimate = kio::Producer::new(crate::session::Estimate::read(&session)); + let server = probe_server(session, stream, version, &estimate); + Self { + estimate, + log, + server, + version, + _gate: gate, + } + } + + /// Publish a new estimate, as the supervisor does when a sample moves. + fn set(&self, send_rate: Option, rtt: Option) { + let Ok(mut estimate) = self.estimate.write() else { + panic!("the estimate outlives the stream"); + }; + *estimate = crate::session::Estimate { + send_rate: send_rate.map(crate::bandwidth::Rate::from_bps), + rtt, + }; + } + + /// Run until everything parks at `after` from now, returning every report so far. + async fn reports(&mut self, after: Duration) -> Vec { + let server = &mut self.server; + let run = kio::wait(|waiter| server.poll_probe(waiter)); + assert!(moq_net_sim::timeout(after, run).await.is_err(), "the stream never ends"); + decode_probes(&self.log.writes.lock().unwrap(), self.version) + } + } + /// Drive the PROBE state machine against a transport reporting `stats`, and /// return whatever it wrote before parking. async fn probe_writes(stats: crate::lite::test_transport::SinkStats) -> Vec { @@ -5272,21 +5374,10 @@ mod tests { /// As above, on a specific negotiated version. async fn probe_writes_version(stats: crate::lite::test_transport::SinkStats, version: Version) -> Vec { - let gate = kio::Producer::new(true); - let mut session = SinkSession::gated_bi(gate.consume()).with_stats(stats); - let log = session.log.clone(); - let stream = Stream::open(&mut session, version).await.unwrap(); - - let mut server = probe_server(session, stream, version); - let mut run = std::pin::pin!(kio::wait(|waiter| server.poll_probe(waiter))); - // The loop reports on a 100ms cadence, so let the first tick land before - // reading what it wrote. It parks on the next tick either way. - assert!(futures::poll!(run.as_mut()).is_pending()); - moq_net_sim::sleep(Duration::from_millis(150)).await; - assert!(futures::poll!(run.as_mut()).is_pending()); - - let writes = log.writes.lock().unwrap().clone(); - decode_probes_version(&writes, version) + Probing::new(stats, version) + .await + .reports(Duration::from_millis(150)) + .await } /// A transport that exposes an RTT but no send-rate estimate must still report. @@ -5350,29 +5441,15 @@ mod tests { /// holding the last value we sent. But only one, however long the stream runs. #[moq_net_sim::test] async fn a_bitrate_going_unknown_is_retracted_once() { - let gate = kio::Producer::new(true); let stats = crate::lite::test_transport::SinkStats::default().with_send_rate(1_000_000); - let mut session = SinkSession::gated_bi(gate.consume()).with_stats(stats); - let log = session.log.clone(); - let stream = Stream::open(&mut session, Version::Lite05).await.unwrap(); - - let mut server = probe_server(session.clone(), stream, Version::Lite05); - let mut run = std::pin::pin!(kio::wait(|waiter| server.poll_probe(waiter))); - assert!(futures::poll!(run.as_mut()).is_pending()); - moq_net_sim::sleep(Duration::from_millis(150)).await; - assert!(futures::poll!(run.as_mut()).is_pending()); + let mut probing = Probing::new(stats, Version::Lite05).await; + assert_eq!(probing.reports(Duration::from_millis(150)).await.len(), 1); // The transport stops measuring. Everything after this is unknown. - session.set_stats(crate::lite::test_transport::SinkStats::default()); + probing.set(None, None); // Well past PROBE_MAX_AGE, so a stale-report timer would have fired repeatedly. - for _ in 0..3 { - moq_net_sim::sleep(Duration::from_secs(11)).await; - assert!(futures::poll!(run.as_mut()).is_pending()); - } - - let writes = log.writes.lock().unwrap().clone(); - let probes = decode_probes(&writes); + let probes = probing.reports(Duration::from_secs(33)).await; assert_eq!( probes.len(), 2, @@ -5382,6 +5459,48 @@ mod tests { assert_eq!(probes[1].bitrate, None); } + /// An estimate that never moves is refreshed once per PROBE_MAX_AGE, and costs + /// nothing in between. + #[moq_net_sim::test] + async fn an_unchanged_estimate_refreshes_at_max_age() { + let stats = crate::lite::test_transport::SinkStats::default().with_send_rate(1_000_000); + let mut probing = Probing::new(stats, Version::Lite05).await; + assert_eq!(probing.reports(Duration::from_millis(9_900)).await.len(), 1); + assert_eq!(probing.reports(Duration::from_millis(200)).await.len(), 2); + assert_eq!(probing.reports(Duration::from_millis(9_800)).await.len(), 2); + assert_eq!(probing.reports(Duration::from_millis(200)).await.len(), 3); + } + + /// A move past PROBE_MAX_DELTA is reported as soon as the estimate changes. + #[moq_net_sim::test] + async fn a_large_move_reports_at_once() { + let stats = crate::lite::test_transport::SinkStats::default().with_send_rate(1_000_000); + let mut probing = Probing::new(stats, Version::Lite05).await; + assert_eq!(probing.reports(Duration::from_secs(1)).await.len(), 1); + + probing.set(Some(2_000_000), None); + let probes = probing.reports(Duration::from_millis(1)).await; + assert_eq!(probes.len(), 2); + assert_eq!(probes[1].bitrate, Some(2_000_000)); + } + + /// A smaller move waits for the threshold to decay to it, even though the + /// estimate does not change again. + #[moq_net_sim::test] + async fn a_small_move_waits_for_the_threshold() { + let stats = crate::lite::test_transport::SinkStats::default().with_send_rate(1_000_000); + let mut probing = Probing::new(stats, Version::Lite05).await; + assert_eq!(probing.reports(Duration::from_secs(1)).await.len(), 1); + + // 10% clears the threshold once it decays to 0.1: 10s - 0.4 * 9.9s = 6.04s + // after the first report. + probing.set(Some(1_100_000), None); + assert_eq!(probing.reports(Duration::from_millis(4_990)).await.len(), 1); + let probes = probing.reports(Duration::from_millis(100)).await; + assert_eq!(probes.len(), 2); + assert_eq!(probes[1].bitrate, Some(1_100_000)); + } + /// Where a request waits when its requester leaves. #[derive(Clone, Copy, Debug)] enum Wait { @@ -5436,6 +5555,7 @@ mod tests { peer_grant: Default::default(), client: false, subscriptions: Default::default(), + estimate: kio::Producer::default().weak(), }); let mut script = Vec::new(); @@ -5556,6 +5676,7 @@ mod tests { peer_setup.set(crate::lite::Setup::default()); let (_, goaway) = crate::goaway::Handle::new(true); let publisher = Publisher::new(PublisherConfig { + estimate: kio::Producer::default().weak(), runtime: crate::time::Clock::sim(), session: ScriptedSession::new(Vec::new()), origin: origin.consume(), @@ -5646,6 +5767,7 @@ mod tests { peer_grant: Default::default(), client: false, subscriptions: Default::default(), + estimate: kio::Producer::default().weak(), }); let mut script = Vec::new(); @@ -5724,6 +5846,7 @@ mod tests { let (_, goaway) = crate::goaway::Handle::new(true); let version = Version::Lite07; let publisher = Publisher::new(PublisherConfig { + estimate: kio::Producer::default().weak(), runtime: crate::time::Clock::sim(), session: ScriptedSession::new(Vec::new()), origin: origin.consume(), @@ -5875,6 +5998,7 @@ mod tests { let origin = Hop::random().produce(); let (_, goaway) = crate::goaway::Handle::new(true); let mut publisher = Publisher::new(PublisherConfig { + estimate: kio::Producer::default().weak(), runtime: crate::time::Clock::sim(), session, origin: origin.consume(), diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index b4c713a09d6..47152ccc241 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -13,6 +13,8 @@ use super::{ pub(crate) struct SessionStart { pub recv_bandwidth: Option, + /// The estimate PROBE reports, published by the session's supervisor. + pub estimate: kio::Producer, /// The session's protocol machine, named so its `Send`-ness stays inferred /// from the transport instead of being fixed by a box. pub driver: Driver, @@ -299,7 +301,9 @@ where false => None, }; + let estimate = kio::Producer::default(); let publisher = Publisher::new(PublisherConfig { + estimate: estimate.weak(), runtime: runtime.clone(), session: session.clone(), origin: publish, @@ -354,6 +358,7 @@ where Ok(SessionStart { recv_bandwidth: recv_bw_consumer, + estimate, driver, goaway: goaway_handle, auth, diff --git a/rs/moq-net/src/model/bandwidth.rs b/rs/moq-net/src/model/bandwidth.rs index 816719aaa26..d3f2782a781 100644 --- a/rs/moq-net/src/model/bandwidth.rs +++ b/rs/moq-net/src/model/bandwidth.rs @@ -104,6 +104,11 @@ impl Producer { } } + /// A handle that mints consumers without being one, so holding it is not demand. + pub(crate) fn weak(&self) -> Weak { + Weak(self.state.weak()) + } + /// Close the producer with an error, notifying all consumers. pub fn abort(&self, err: Error) -> Result<()> { let mut state = self.modify()?; @@ -161,6 +166,20 @@ impl Default for Producer { } } +/// Mints [`Consumer`]s on demand without counting as one, nor keeping the channel open. +#[derive(Clone)] +pub(crate) struct Weak(kio::ProducerWeak); + +impl Weak { + /// A new consumer, which reports the closure once the producer is gone. + pub(crate) fn consume(&self) -> Consumer { + Consumer { + inner: Inner::Whole(self.0.consume()), + last: None, + } + } +} + /// Divides one connection's bandwidth estimate among the tracks sharing it. /// /// Every sender on a connection reads the same estimate, so N senders each diff --git a/rs/moq-net/src/server.rs b/rs/moq-net/src/server.rs index 4ad650eca42..42f680efc63 100644 --- a/rs/moq-net/src/server.rs +++ b/rs/moq-net/src/server.rs @@ -197,6 +197,7 @@ impl Server { goaway: start.goaway, auth: start.auth, setup: start.setup, + estimate: Some(start.estimate), }, )) } @@ -644,7 +645,12 @@ where version.into(), None, crate::driver::Protocol::Ietf(protocol), - crate::session::Handles { goaway, auth, setup }, + crate::session::Handles { + goaway, + auth, + setup, + estimate: None, + }, )) } .maybe_boxed() @@ -717,7 +723,7 @@ where }; stream.writer.encode(&server_setup).await?; - let (recv_bw, protocol, goaway, auth, setup) = match version { + let (recv_bw, protocol, goaway, auth, setup, estimate) = match version { Version::Lite(v) => { let stream = stream.with_version(v); // Pre-lite-05: no Setup Stream, so nothing to advertise or seed. @@ -741,6 +747,7 @@ where start.goaway, start.auth, start.setup, + Some(start.estimate), ) } Version::Ietf(v) => { @@ -766,7 +773,7 @@ where early_unis: Vec::new(), extensions: server.extensions, })?; - (None, crate::driver::Protocol::Ietf(protocol), goaway, auth, setup) + (None, crate::driver::Protocol::Ietf(protocol), goaway, auth, setup, None) } }; @@ -776,7 +783,12 @@ where version, recv_bw, protocol, - crate::session::Handles { goaway, auth, setup }, + crate::session::Handles { + goaway, + auth, + setup, + estimate, + }, )) } .maybe_boxed() diff --git a/rs/moq-net/src/session.rs b/rs/moq-net/src/session.rs index c3ca782f07a..38702fe7d79 100644 --- a/rs/moq-net/src/session.rs +++ b/rs/moq-net/src/session.rs @@ -99,6 +99,31 @@ pub struct Stats { pub packets_lost: Option, } +/// The congestion controller's view of the connection, as PROBE reports it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) struct Estimate { + /// The congestion controller's send rate. + pub send_rate: Option, + /// The smoothed round-trip time. + pub rtt: Option, +} + +impl Estimate { + /// Read the transport's current estimate. + pub(crate) fn read(session: &S) -> Self { + Self::from(&snapshot(session)) + } +} + +impl From<&Stats> for Estimate { + fn from(stats: &Stats) -> Self { + Self { + send_rate: stats.estimated_send_rate, + rtt: stats.rtt, + } + } +} + /// A MoQ transport session, wrapping a WebTransport connection. /// /// Returned with a [`Driver`](crate::Driver) by [`crate::Client::connect`] and @@ -123,7 +148,8 @@ pub struct Session { closed: kio::Consumer>, stats: kio::Shared, version: Version, - send_bandwidth: Option, + // Weak, so the sampler runs only while a caller holds a consumer. + send_bandwidth: Option, recv_bandwidth: Option, goaway: Arc, auth: auth::Handle, @@ -140,7 +166,7 @@ impl Session { /// /// Returns `None` if the QUIC backend doesn't support bandwidth estimation. pub fn send_bandwidth(&self) -> Option { - self.send_bandwidth.clone() + self.send_bandwidth.as_ref().map(bandwidth::Weak::consume) } /// Returns a consumer for the estimated receive bitrate (from PROBE). @@ -318,6 +344,9 @@ pub(super) struct Handles { pub goaway: goaway::Handle, pub auth: auth::Handle, pub setup: Setup, + /// Where the driver publishes [`Estimate`] changes, for a protocol that reports + /// them to the peer (moq-lite PROBE). Sampled only while a consumer exists. + pub estimate: Option>, } impl Session { @@ -332,13 +361,23 @@ impl Session { where S: crate::transport::poll::Session, { - let Handles { goaway, auth, setup } = handles; + let Handles { + goaway, + auth, + setup, + estimate, + } = handles; let sample = snapshot(&session); + if let Some(estimate) = &estimate + && let Ok(mut current) = estimate.write() + { + *current = Estimate::from(&sample); + } // Send bandwidth is version-agnostic: it depends on QUIC backend support. let (send_bandwidth, send_producer) = if sample.estimated_send_rate.is_some() { let producer = bandwidth::Producer::new(); - (Some(producer.consume()), Some(producer)) + (Some(producer.weak()), Some(producer)) } else { (None, None) }; @@ -361,6 +400,8 @@ impl Session { closed, stats: stats.clone(), send_bandwidth: send_producer, + estimate, + sampled: None, mode: SamplerMode::Idle, drain: Drain::Idle, }; @@ -413,6 +454,10 @@ pub(crate) struct Supervisor { /// The send-rate estimate channel, when the backend reports one. `None` /// also once every consumer is gone for good. send_bandwidth: Option, + /// Published on change while a consumer exists; see [`Self::poll_sampler`]. + estimate: Option>, + /// When the last sample was taken. + sampled: Option, mode: SamplerMode, drain: Drain, } @@ -531,9 +576,10 @@ impl Supervisor { true } - /// Take one sample and arm the next deadline. - fn sample(&mut self) { + /// Take one sample, publishing it to the bandwidth and estimate channels. + fn sample(&mut self) -> Stats { let sample = snapshot(&self.session); + self.sampled = Some(self.runtime.now()); if let Some(producer) = &self.send_bandwidth { // An error means every consumer is gone for good; the stats cell // still wants the sample. @@ -541,6 +587,21 @@ impl Supervisor { self.send_bandwidth = None; } } + if let Some(estimate) = &self.estimate + && let Ok(mut current) = estimate.write() + { + let next = Estimate::from(&sample); + // Only a change wakes the consumers. + if *current != next { + *current = next; + } + } + sample + } + + /// Take one sample for the readers that asked, and arm the next deadline. + fn sample_demanded(&mut self) { + let sample = self.sample(); let mut stats = self.stats.lock(); stats.sample = sample; stats.demanded = false; @@ -551,6 +612,30 @@ impl Supervisor { } fn poll_sampler(&mut self, waiter: &kio::Waiter) { + self.poll_demanded(waiter); + + // An estimate consumer (a PROBE stream) samples only when the driver runs + // anyway, never on a timer of its own: a session nothing wakes is sending + // nothing, so its estimate has nothing new to say. The driver polls this + // before the protocol, so the estimate a consumer reads is at most one + // interval old. + let Some(estimate) = &self.estimate else { return }; + if estimate.poll_used(waiter).is_pending() { + return; + } + let now = self.runtime.now(); + if self + .sampled + .is_none_or(|at| now.duration_since(at) >= Self::POLL_INTERVAL) + { + // Not into the stats cell: writing it would wake this sampler's own + // registration there, and stats readers demand their own samples. + self.sample(); + } + } + + /// Sample on an interval while a bandwidth consumer or a stats reader wants it. + fn poll_demanded(&mut self, waiter: &kio::Waiter) { loop { match &mut self.mode { SamplerMode::Idle => { @@ -576,7 +661,7 @@ impl Supervisor { if !demanded { return; } - self.sample(); + self.sample_demanded(); } SamplerMode::Polling { deadline } => { if deadline.poll(waiter).is_pending() { @@ -588,7 +673,7 @@ impl Supervisor { self.mode = SamplerMode::Idle; continue; } - self.sample(); + self.sample_demanded(); // Loop so the fresh deadline registers the waiter. } } diff --git a/rs/moq-net/src/time.rs b/rs/moq-net/src/time.rs index 2bfcd5fffda..839199e251e 100644 --- a/rs/moq-net/src/time.rs +++ b/rs/moq-net/src/time.rs @@ -182,13 +182,6 @@ impl Deadline { } } - /// A deadline armed for `at`. - pub(crate) fn at(clock: &Clock, at: Instant) -> Self { - let mut deadline = Self::new(clock); - deadline.set(Some(at)); - deadline - } - /// A deadline armed for `duration` past the clock's [`now`](Clock::now). /// /// A duration the clock cannot represent (e.g. [`std::time::Duration::MAX`]) @@ -271,7 +264,8 @@ mod tests { let now = Instant::now(); let clock = Clock::new(now); let at = now + Duration::from_secs(3); - let mut deadline = Deadline::at(&clock, at); + let mut deadline = Deadline::new(&clock); + deadline.set(Some(at)); assert_eq!(clock.timeout(), Some(at)); assert!(deadline.poll(&kio::Waiter::noop()).is_pending()); clock.advance(at); diff --git a/rs/moq-net/tests/idle_session.rs b/rs/moq-net/tests/idle_session.rs new file mode 100644 index 00000000000..15c6084c3e2 --- /dev/null +++ b/rs/moq-net/tests/idle_session.rs @@ -0,0 +1,89 @@ +//! An idle viewer session costs its server nothing per tick: no driver wakes and +//! no transport stats reads while nothing moves. A server holds thousands of these, +//! so a fixed-interval timer per session adds up to whole cores. + +mod support; + +use std::{sync::atomic::Ordering, time::Duration}; + +use moq_net::{Hop, Version, bandwidth::Rate, origin}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; +use support::mock::MockStats; + +const VERSIONS: [&str; 2] = ["moq-lite-06", "moq-lite-07-wip"]; + +const IDLE: Duration = Duration::from_secs(60); + +/// PROBE refreshes an unchanged report this often. +const PROBE_MAX_AGE: Duration = Duration::from_secs(10); + +const STATS: MockStats = MockStats { + estimated_send_rate: Some(1_000_000), + rtt: Some(Duration::from_millis(40)), +}; + +/// A viewer session whose server publishes `origin`, with the viewer's PROBE +/// stream open (as a browser viewer keeps one) and its first report received. +async fn viewer(version: &str, origin: &origin::Producer) -> (MockPair, moq_net::bandwidth::Consumer) { + let mut options = MockConnectOptions::new(version.parse::().unwrap()); + options.server_publish = Some(origin.consume()); + options.client_subscribe = Some(origin::Producer::new(origin::Config::new(Hop::new(2).unwrap())).0); + options.stats = STATS; + let pair = connect_mock(options).await; + + let mut probe = pair.client.recv_bandwidth().expect("lite carries PROBE"); + assert_eq!(probe.changed().await.unwrap(), Some(Rate::from_bps(1_000_000))); + moq_net_sim::sleep(Duration::from_secs(1)).await; + (pair, probe) +} + +fn origin() -> origin::Producer { + let (origin, driver) = origin::Producer::new(origin::Config::new(Hop::new(1).unwrap())); + support::harness::spawn(driver); + origin +} + +#[moq_net_sim::test] +async fn idle_viewer_does_not_wake_the_server() { + for version in VERSIONS { + let origin = origin(); + let (pair, _probe) = viewer(version, &origin).await; + + let (polls, reads) = (&pair.server_polls, &pair.server_transport); + let (start_polls, start_reads) = (polls.load(Ordering::Relaxed), reads.stats_reads()); + moq_net_sim::sleep(IDLE).await; + let polls = polls.load(Ordering::Relaxed) - start_polls; + let reads = reads.stats_reads() - start_reads; + + // Nothing moves, so the only work left is the max-age refresh: one sample, + // and the deadline's poll plus the re-poll its own wake schedules. + let refreshes = (IDLE.as_secs() / PROBE_MAX_AGE.as_secs()) as usize; + assert!(reads <= refreshes, "{version}: {reads} stats reads over {IDLE:?}"); + assert!(polls <= 2 * refreshes, "{version}: {polls} driver polls over {IDLE:?}"); + } +} + +/// The flip side: a session the server is doing work for samples as it goes, so a +/// moved estimate reaches the viewer with that work, not at the next max age. +#[moq_net_sim::test] +async fn busy_viewer_sees_the_estimate_move() { + for version in VERSIONS { + let origin = origin(); + let (pair, mut probe) = viewer(version, &origin).await; + + pair.server_transport.set_stats(MockStats { + estimated_send_rate: Some(2_000_000), + ..STATS + }); + // Server work for this session: announcing a broadcast to the viewer. + let broadcast = origin.create_broadcast("live").unwrap(); + broadcast.announce(Default::default()).unwrap(); + + let moved = moq_net_sim::timeout(Duration::from_millis(1), probe.changed()).await; + assert_eq!( + moved.expect("reported with the work").unwrap(), + Some(Rate::from_bps(2_000_000)), + "{version}" + ); + } +} diff --git a/rs/moq-net/tests/support/harness.rs b/rs/moq-net/tests/support/harness.rs index df4df72ad29..b30e2e0814c 100644 --- a/rs/moq-net/tests/support/harness.rs +++ b/rs/moq-net/tests/support/harness.rs @@ -7,9 +7,14 @@ #![allow(dead_code)] +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; + use moq_net::{Client, Server, Session, Version, origin}; -use super::mock::{MockSession, create_mock_session_pair}; +use super::mock::{MockSession, MockStats, create_mock_session_pair}; /// The current instant: simulated under `moq_net_sim`, tokio's in the benches. pub fn now() -> moq_net::time::Instant { @@ -21,13 +26,40 @@ pub fn now() -> moq_net::time::Instant { /// Run a protocol driver in the background: on `moq_net_sim` in tests, on tokio in /// the benches. -pub fn spawn(mut driver: D) { +pub fn spawn(driver: D) { + spawn_counted(driver); +} + +/// [`spawn`], returning how many times the driver has been polled. +pub fn spawn_counted(driver: D) -> Arc { + let polls = Arc::new(AtomicUsize::new(0)); + let mut driver = Counted { + driver, + polls: polls.clone(), + }; match moq_net_sim::is_running() { true => drop(moq_net_sim::spawn(moq_net_sim::drive(move |now, waiter| { - driver.poll(now, waiter) + moq_net::time::Driver::poll(&mut driver, now, waiter) }))), false => drop(tokio::spawn(moq_net::time::run(driver))), } + polls +} + +struct Counted { + driver: D, + polls: Arc, +} + +impl moq_net::time::Driver for Counted { + fn poll( + &mut self, + now: moq_net::time::Instant, + waiter: &kio::Waiter, + ) -> Result, moq_net::Error> { + self.polls.fetch_add(1, Ordering::Relaxed); + self.driver.poll(now, waiter) + } } /// Wait for a subscription to reach `track`. Demand alone does not say one has: on @@ -59,6 +91,8 @@ pub struct MockConnectOptions { pub server_extensions: moq_net::setup::Extensions, /// The price the client puts on the link, when not the default. pub cost: Option, + /// What both transports report as their connection statistics. + pub stats: MockStats, } impl MockConnectOptions { @@ -75,6 +109,7 @@ impl MockConnectOptions { client_extensions: Default::default(), server_extensions: Default::default(), cost: None, + stats: MockStats::default(), } } } @@ -88,6 +123,8 @@ pub struct MockPair { pub client_transport: MockSession, /// The server's end of the mock transport, for steering delivery. pub server_transport: MockSession, + /// How many times the server's driver has been polled. + pub server_polls: Arc, } /// Run the MoQ handshake over the mock transport, returning connected sessions. @@ -103,6 +140,8 @@ pub async fn connect_mock(opts: MockConnectOptions) -> MockPair { let protocol = opts.version.alpn(); let (client_transport, server_transport) = create_mock_session_pair(Some(protocol)); client_transport.set_latency(opts.latency); + client_transport.set_stats(opts.stats); + server_transport.set_stats(opts.stats); let transports = (client_transport.clone(), server_transport.clone()); let mut client = Client::new() @@ -148,16 +187,16 @@ pub async fn connect_mock(opts: MockConnectOptions) -> MockPair { .accept(now(), server_transport) .await .expect("server handshake failed"); - spawn(driver); - session + (session, spawn_counted(driver)) }; - let (client_session, server_session) = futures::join!(client_fut, server_fut); + let (client_session, (server_session, server_polls)) = futures::join!(client_fut, server_fut); MockPair { client: client_session, server: server_session, client_transport: transports.0, server_transport: transports.1, + server_polls, } } diff --git a/rs/moq-net/tests/support/mock.rs b/rs/moq-net/tests/support/mock.rs index 71a872f3b1c..c5ce6f8642c 100644 --- a/rs/moq-net/tests/support/mock.rs +++ b/rs/moq-net/tests/support/mock.rs @@ -481,6 +481,29 @@ struct SessionSide { withhold_bidi_fins: Arc, /// The first byte of each bidi stream this side opened. bidi_types: StreamTypes, + /// What [`poll::Session::stats`] reports. + stats: Mutex, + /// How many times the stats were read. + stats_reads: AtomicUsize, +} + +/// The connection statistics a [`MockSession`] reports. Unavailable by default. +#[derive(Clone, Copy, Default)] +pub struct MockStats { + /// The congestion controller's send-rate estimate, in bits per second. + pub estimated_send_rate: Option, + /// The smoothed round-trip time. + pub rtt: Option, +} + +impl moq_net::transport::Stats for MockStats { + fn estimated_send_rate(&self) -> Option { + self.estimated_send_rate + } + + fn rtt(&self) -> Option { + self.rtt + } } /// An in-memory mock WebTransport session. @@ -620,7 +643,8 @@ impl poll::Session for MockSession { } fn stats(&self) -> impl moq_net::transport::Stats { - moq_net::transport::StatsUnavailable + self.side.stats_reads.fetch_add(1, Ordering::Relaxed); + *self.side.stats.lock().unwrap() } } @@ -632,6 +656,16 @@ impl MockSession { self.side.conn.finishes.load(Ordering::Relaxed) } + /// Report `stats` from now on. + pub fn set_stats(&self, stats: MockStats) { + *self.side.stats.lock().unwrap() = stats; + } + + /// How many times the stats were read. + pub fn stats_reads(&self) -> usize { + self.side.stats_reads.load(Ordering::Relaxed) + } + /// Drop the FIN of the bidi streams this side opens, keeping them open. pub fn withhold_bidi_fins(&self) { self.side.withhold_bidi_fins.store(true, Ordering::Relaxed); @@ -814,6 +848,8 @@ pub fn create_mock_session_pair(protocol: Option<&'static str>) -> (MockSession, lossy: Mutex::default(), withhold_bidi_fins: Arc::default(), bidi_types: StreamTypes::default(), + stats: Mutex::default(), + stats_reads: AtomicUsize::default(), }); let server_side = Arc::new(SessionSide { @@ -834,6 +870,8 @@ pub fn create_mock_session_pair(protocol: Option<&'static str>) -> (MockSession, lossy: Mutex::default(), withhold_bidi_fins: Arc::default(), bidi_types: StreamTypes::default(), + stats: Mutex::default(), + stats_reads: AtomicUsize::default(), }); let new = |side| MockSession { From 3a72812b395313c97efafaf8c0c561ed7fa4033f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 18:29:03 -0700 Subject: [PATCH 2/3] docs(quest): plan PROBE on demand in Rust and js/net Co-Authored-By: Claude Opus 5.5 --- quest/m1/README.md | 6 ++++++ quest/m1/probe-on-demand-js.md | 28 +++++++++++++++++++++++++++ quest/m1/probe-on-demand-rs.md | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 quest/m1/probe-on-demand-js.md create mode 100644 quest/m1/probe-on-demand-rs.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 40e50da469f..e295645a1d4 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -43,6 +43,10 @@ bench all require its crate. Added 2026-10-10 by maintainer priority: HTTP fetch stats, ranked just above session outcomes, and embedder refusals inside session outcomes. +Added 2026-10-12: PROBE on demand in Rust and js/net, ranked after the viewer +up-switch, from the idle-session wake fix that found every client holds PROBE +open. + Imported 2026-10-12 from GitHub issues: the 0.17 warm FETCH stall and the moq-lite malformed-input closes rank just below Dropped sources, and #5347 folded into Dropped sources. @@ -121,6 +125,8 @@ folded into Dropped sources. - [Simulcast rung disable](/quest/m1/simulcast-rung-disable.md) - JS, Rust/FFI, and OBS publishers stop encoding the top renditions their grant cannot fund, advertise `enabled: false`, and re-enable with hysteresis - [Discover media headroom](/quest/m1/quic-probe.md) - test useful-media pacing before adding redundant probe traffic - [Viewer up-switch](/quest/m1/viewer-upswitch.md) - a viewer capped by its small rendition finds headroom through PROBE and moves up +- [Rust PROBE on demand](/quest/m1/probe-on-demand-rs.md) - a Rust client opens PROBE only while a `recv_bandwidth()` consumer is held, and `Stats` drops `estimated_recv_rate` +- [JS PROBE on demand](/quest/m1/probe-on-demand-js.md) - js/net opens PROBE only while something reads the estimate - [Own the QUIC stack](/quest/m1/quic/README.md) - quinn hard-forked in-tree as `moq-quic`, carrying BBR, reliable reset, hierarchical scheduling, peer limits, and endpoint sharding - [QoS](/quest/m1/qos/README.md) - broadcast health: relay starvation and timeliness histograms - [HTTP fetch stats](/quest/m1/http-fetch-stats.md) - each HTTP `/fetch` counts one `requests` and its bytes under the token's tier and root, not a session diff --git a/quest/m1/probe-on-demand-js.md b/quest/m1/probe-on-demand-js.md new file mode 100644 index 00000000000..0b17b0ae51f --- /dev/null +++ b/quest/m1/probe-on-demand-js.md @@ -0,0 +1,28 @@ +# [M] js/net opens PROBE only while something reads it + +## Goal + +A js/net connection opens its PROBE stream only while something observes the +receive estimate, and closes it when the last reader goes. A `@moq/watch` that +is playing still gets the estimate (its sync RTT and ABR read it); a connection +nothing reads holds no PROBE stream. The PROBE wire is unchanged. + +## Plan + +Today `js/net/src/lite/connection.ts` runs `subscriber.runProbe()` for the +whole connection, unconditionally. + +Decided (2026-10-12, quest-plan): demand comes from readers, either through +`@moq/signals` observation tracking (subscriber count, mirroring kio's +used/unused) or through an explicit consumer handle mirroring Rust's +`Session::recv_bandwidth()`. Either is acceptable; pick the simpler one once in +the code. With observation, `peek()` does not count, so the watch stats panel +subscribes while it is open. A consumer handle has no `Drop` in JS, so it must +be released by the effect that holds it. + +Reconnects (`connection/reload.ts`) carry demand to the new connection. + +## Related + +- [Rust PROBE on demand](/quest/m1/probe-on-demand-rs.md) - the same outcome for Rust clients +- [Viewer up-switch](/quest/m1/viewer-upswitch.md) - also sends PROBE targets, so it reads the estimate while choosing renditions diff --git a/quest/m1/probe-on-demand-rs.md b/quest/m1/probe-on-demand-rs.md new file mode 100644 index 00000000000..b514ed62f1a --- /dev/null +++ b/quest/m1/probe-on-demand-rs.md @@ -0,0 +1,35 @@ +# [L] Rust clients open PROBE only while a consumer reads it + +## Goal + +A Rust client opens a PROBE stream only while some `Session::recv_bandwidth()` +consumer is held, and closes it when the last one drops. The handle itself is +never demand, and `Stats` no longer carries `estimated_recv_rate`: the consumer +is the only way to the receive estimate. The PROBE wire is unchanged. + +## Plan + +Today `moq_net::Session` keeps its own `recv_bandwidth` consumer, so the lite +subscriber's `RecvBandwidth` always sees demand and every session keeps a PROBE +stream open, and moq-tokio's `Connection` holds another across reconnects. The +send side had the same bug: `fix/idle-session-wakes` made the handle hold a weak +reference (`bandwidth::Weak`), and the receive side should follow it. + +Decided (2026-10-12, quest-plan): +- Only a held consumer is demand. A `stats()` read is not, because a stats field + that silently reads `None` unless something else holds a consumer is a trap. +- Remove `Stats::estimated_recv_rate`. That breaks the API, so this lands on + `dev`. moq-ffi and moq-c replace their `estimated_recv_rate` stats field with + a receive-bandwidth consumer handle, and every binding and `doc/lib` page + follows (see the Cross-Package Sync table). +- moq-tokio's `Connection` forwards demand across reconnects instead of holding + a consumer: it consumes the inner session's estimate only while its own + consumer is used. + +Test: a session nobody holds a consumer for opens no PROBE stream, and one that +holds then drops a consumer opens and then resets it. Reuse the mock transport +stats and `idle_session` harness from `fix/idle-session-wakes`. + +## Related + +- [JS PROBE on demand](/quest/m1/probe-on-demand-js.md) - the same outcome for js/net From d5e5cbeef58a9d134ec58213b81ed2e7381a3a63 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 19:21:37 -0700 Subject: [PATCH 3/3] docs(quest): point PROBE on demand at main and the idle_session test Co-Authored-By: Claude Opus 5.5 --- quest/m1/probe-on-demand-rs.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/quest/m1/probe-on-demand-rs.md b/quest/m1/probe-on-demand-rs.md index b514ed62f1a..d4b66c3988a 100644 --- a/quest/m1/probe-on-demand-rs.md +++ b/quest/m1/probe-on-demand-rs.md @@ -12,14 +12,14 @@ is the only way to the receive estimate. The PROBE wire is unchanged. Today `moq_net::Session` keeps its own `recv_bandwidth` consumer, so the lite subscriber's `RecvBandwidth` always sees demand and every session keeps a PROBE stream open, and moq-tokio's `Connection` holds another across reconnects. The -send side had the same bug: `fix/idle-session-wakes` made the handle hold a weak -reference (`bandwidth::Weak`), and the receive side should follow it. +send side had the same bug, fixed by having the handle hold a weak reference +(`bandwidth::Weak`), and the receive side should follow it. Decided (2026-10-12, quest-plan): - Only a held consumer is demand. A `stats()` read is not, because a stats field that silently reads `None` unless something else holds a consumer is a trap. -- Remove `Stats::estimated_recv_rate`. That breaks the API, so this lands on - `dev`. moq-ffi and moq-c replace their `estimated_recv_rate` stats field with +- Remove `Stats::estimated_recv_rate`. That breaks the API, which `main` + allows. moq-ffi and moq-c replace their `estimated_recv_rate` stats field with a receive-bandwidth consumer handle, and every binding and `doc/lib` page follows (see the Cross-Package Sync table). - moq-tokio's `Connection` forwards demand across reconnects instead of holding @@ -28,7 +28,7 @@ Decided (2026-10-12, quest-plan): Test: a session nobody holds a consumer for opens no PROBE stream, and one that holds then drops a consumer opens and then resets it. Reuse the mock transport -stats and `idle_session` harness from `fix/idle-session-wakes`. +stats and the harness in `rs/moq-net/tests/idle_session.rs`. ## Related