From 0a3ec82b4900e40ae31a80f2f90aaf0d38f4dd22 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 18:42:47 -0700 Subject: [PATCH] fix(net): backport stop idle viewer sessions from waking every 100ms (#5382) 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. Ported by hand onto release's tokio-clock runtime and test harness. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/client.rs | 11 +- rs/moq-net/src/lite/publisher.rs | 299 +++++++++++++++++++--------- rs/moq-net/src/lite/session.rs | 5 + rs/moq-net/src/model/bandwidth.rs | 19 ++ rs/moq-net/src/runtime.rs | 8 +- rs/moq-net/src/server.rs | 11 +- rs/moq-net/src/session.rs | 94 ++++++++- rs/moq-net/tests/idle_session.rs | 89 +++++++++ rs/moq-net/tests/support/harness.rs | 46 ++++- rs/moq-net/tests/support/mock.rs | 45 ++++- 10 files changed, 511 insertions(+), 116 deletions(-) create mode 100644 rs/moq-net/tests/idle_session.rs diff --git a/rs/moq-net/src/client.rs b/rs/moq-net/src/client.rs index e4a6497ca55..072dfd578c3 100644 --- a/rs/moq-net/src/client.rs +++ b/rs/moq-net/src/client.rs @@ -212,6 +212,7 @@ impl Client { start.recv_bandwidth, crate::driver::Protocol::Lite(Box::new(start.driver)), start.goaway, + Some(start.estimate), )) } @@ -295,6 +296,7 @@ impl Client { None, crate::driver::Protocol::Ietf(protocol), goaway, + None, )); } Some(ALPN_16) => { @@ -380,7 +382,7 @@ impl Client { .copied() .ok_or(Error::Version)?; - let (recv_bw, protocol, goaway) = match version { + let (recv_bw, protocol, goaway, estimate) = match version { Version::Lite(v) => { let stream = stream.with_version(v); let start = lite::start(lite::Config { @@ -401,6 +403,7 @@ impl Client { start.recv_bandwidth, crate::driver::Protocol::Lite(Box::new(start.driver)), start.goaway, + Some(start.estimate), ) } Version::Ietf(v) => { @@ -435,11 +438,13 @@ impl Client { peer_declared: Some(peer_declared), early_unis: Vec::new(), })?; - (None, crate::driver::Protocol::Ietf(protocol), goaway) + (None, crate::driver::Protocol::Ietf(protocol), goaway, None) } }; - Ok(Session::new(runtime, session, version, recv_bw, protocol, goaway)) + Ok(Session::new( + runtime, session, version, recv_bw, protocol, goaway, estimate, + )) } } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index ba16afb3cac..3fe2eb07498 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -11,8 +11,6 @@ use std::{ time::Duration, }; -use web_transport_trait::Stats; - use crate::{ Error, Hop, Hops, coding::{Decode, DecodeError, Encode, Stream, Writer}, @@ -40,6 +38,8 @@ pub(super) struct PublisherConfig { /// The origin (hop) id assigned to the peer, used whenever the peer doesn't /// declare one itself. See `Client::with_peer_hop`. pub peer_hop: Option, + /// The supervisor's estimate, which a PROBE stream follows. + pub estimate: kio::ProducerWeak, } /// Context shared by every control-stream child. @@ -64,6 +64,7 @@ struct Shared { goaway: crate::goaway::Protocol, // Control streams still serving the peer data, which a draining close waits for. owed: AtomicUsize, + estimate: kio::ProducerWeak, } /// Largest millisecond duration every implementation can carry losslessly. @@ -163,6 +164,7 @@ impl Publisher { version: config.version, goaway: config.goaway, owed: AtomicUsize::new(0), + estimate: config.estimate, }), runtime: config.runtime, accept, @@ -349,17 +351,25 @@ impl Control { } } -/// 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::runtime::Instant)>, - next_probe: crate::runtime::Deadline, + /// When the unsent change becomes worth reporting by age alone. + due: crate::runtime::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; @@ -380,23 +390,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::runtime::Deadline::at(&runtime, runtime.now()), + due: crate::runtime::Deadline::new(&runtime), runtime, } } @@ -417,65 +443,94 @@ impl ProbeServe { let mut cx = Context::from_waker(waiter.waker()); 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); } } } @@ -3705,6 +3760,7 @@ mod tests { peer_setup, goaway, peer_hop: Some(assigned), + estimate: kio::Producer::default().weak(), }); let serving = kio::wait(|waiter| publisher.shared.poll_serving_origin(waiter)).await; @@ -3776,11 +3832,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(); @@ -3790,11 +3842,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); @@ -3806,10 +3860,58 @@ mod tests { peer_setup: crate::lite::PeerSetup::default(), goaway, peer_hop: None, + 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!(tokio::time::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 { @@ -3818,21 +3920,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()); - tokio::time::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. @@ -3896,29 +3987,15 @@ mod tests { /// holding the last value we sent. But only one, however long the stream runs. #[tokio::test(start_paused = true)] 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()); - tokio::time::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 { - tokio::time::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, @@ -3928,6 +4005,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. + #[tokio::test(start_paused = true)] + 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. + #[tokio::test(start_paused = true)] + 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. + #[tokio::test(start_paused = true)] + 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 { @@ -3978,6 +4097,7 @@ mod tests { peer_setup, goaway, peer_hop: None, + estimate: kio::Producer::default().weak(), }); let mut script = Vec::new(); @@ -4093,6 +4213,7 @@ mod tests { peer_setup, goaway, peer_hop: None, + estimate: kio::Producer::default().weak(), }); let mut script = Vec::new(); diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index 3a9120a13c4..e6b6b4a4254 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -18,6 +18,8 @@ pub(crate) struct SessionStart { pub driver: Driver, /// The session-side GOAWAY halves, stored on the public [`crate::Session`]. pub goaway: crate::goaway::Handle, + /// The estimate PROBE reports, published by the session's supervisor. + pub estimate: kio::Producer, } /// Server: read the peer's single SETUP message off its Setup Stream before starting @@ -164,7 +166,9 @@ where // Read out before the setup machine takes ownership below. let our_cost = our_setup.cost; + let estimate = kio::Producer::default(); let publisher = Publisher::new(PublisherConfig { + estimate: estimate.weak(), runtime: runtime.clone(), session: session.clone(), origin: publish, @@ -204,6 +208,7 @@ where recv_bandwidth: recv_bw_consumer, driver, goaway: goaway_handle, + estimate, }) } diff --git a/rs/moq-net/src/model/bandwidth.rs b/rs/moq-net/src/model/bandwidth.rs index 58977552ea3..ffc6610e9d4 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()?; @@ -167,6 +172,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/runtime.rs b/rs/moq-net/src/runtime.rs index a354c89539f..6960eeb374f 100644 --- a/rs/moq-net/src/runtime.rs +++ b/rs/moq-net/src/runtime.rs @@ -67,13 +67,6 @@ impl Deadline { } } - /// A deadline armed for `at`. - pub fn at(runtime: &R, at: Instant) -> Self { - let mut deadline = Self::new(runtime); - deadline.set(Some(at)); - deadline - } - /// A deadline armed for `duration` past the runtime's [`now`](Timers::now). /// /// A duration the clock cannot represent (e.g. [`std::time::Duration::MAX`]) @@ -95,6 +88,7 @@ impl Deadline { } /// The instant this fires at, or `None` while disarmed. + #[cfg(test)] pub fn deadline(&self) -> Option { self.at } diff --git a/rs/moq-net/src/server.rs b/rs/moq-net/src/server.rs index 0ba506db7c7..2c96e1d84a3 100644 --- a/rs/moq-net/src/server.rs +++ b/rs/moq-net/src/server.rs @@ -128,6 +128,7 @@ impl Server { start.recv_bandwidth, crate::driver::Protocol::Lite(Box::new(start.driver)), start.goaway, + Some(start.estimate), )) } @@ -544,6 +545,7 @@ where None, crate::driver::Protocol::Ietf(protocol), goaway, + None, )) } .maybe_boxed() @@ -603,7 +605,7 @@ where }; stream.writer.encode(&server_setup).await?; - let (recv_bw, protocol, goaway) = match version { + let (recv_bw, protocol, goaway, estimate) = match version { Version::Lite(v) => { let stream = stream.with_version(v); // Pre-lite-05: no Setup Stream, so nothing to advertise or seed. @@ -622,6 +624,7 @@ where start.recv_bandwidth, crate::driver::Protocol::Lite(Box::new(start.driver)), start.goaway, + Some(start.estimate), ) } Version::Ietf(v) => { @@ -644,11 +647,13 @@ where peer_declared: Some(peer_declared), early_unis: Vec::new(), })?; - (None, crate::driver::Protocol::Ietf(protocol), goaway) + (None, crate::driver::Protocol::Ietf(protocol), goaway, None) } }; - Ok(Session::new(runtime, session, version, recv_bw, protocol, goaway)) + Ok(Session::new( + runtime, session, version, recv_bw, protocol, goaway, estimate, + )) } .maybe_boxed() } diff --git a/rs/moq-net/src/session.rs b/rs/moq-net/src/session.rs index f68516b8785..1e4d9039fe7 100644 --- a/rs/moq-net/src/session.rs +++ b/rs/moq-net/src/session.rs @@ -4,7 +4,7 @@ use std::{sync::Arc, task::Poll, time::Duration}; use web_transport_trait::Stats as _; -use crate::{Error, SessionError, Version, bandwidth, goaway}; +use crate::{Error, SessionError, Version, bandwidth, goaway, runtime::Timers as _}; /// How long [`Session::close`] waits for queued data before closing anyway. const CLOSE_TIMEOUT: Duration = Duration::from_secs(1); @@ -74,6 +74,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 @@ -98,7 +123,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, } @@ -113,7 +139,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). @@ -254,16 +280,22 @@ impl Session { recv_bandwidth: Option, protocol: crate::driver::Protocol, goaway: goaway::Handle, + estimate: Option>, ) -> (Self, crate::Driver) where S: crate::transport::poll::Session, { 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) }; @@ -285,6 +317,8 @@ impl Session { closed, stats: stats.clone(), send_bandwidth: send_producer, + estimate, + sampled: None, mode: SamplerMode::Idle, drain: Drain::Idle, }; @@ -334,6 +368,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, } @@ -450,9 +488,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. @@ -460,6 +499,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; @@ -470,6 +524,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 => { @@ -495,7 +573,7 @@ impl Supervisor { if !demanded { return; } - self.sample(); + self.sample_demanded(); } SamplerMode::Polling { deadline } => { if deadline.poll(waiter).is_pending() { @@ -507,7 +585,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/tests/idle_session.rs b/rs/moq-net/tests/idle_session.rs new file mode 100644 index 00000000000..fe14eb82c02 --- /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))); + tokio::time::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())); + tokio::spawn(support::harness::run(driver)); + origin +} + +#[tokio::test(start_paused = true)] +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()); + tokio::time::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. +#[tokio::test(start_paused = true)] +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 = tokio::time::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 3b8b5683f2b..9b1793ef559 100644 --- a/rs/moq-net/tests/support/harness.rs +++ b/rs/moq-net/tests/support/harness.rs @@ -7,12 +7,43 @@ #![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}; pub use moq_net::time::run; +/// Spawn a protocol driver, returning how many times it has been polled. +pub fn spawn_counted(driver: D) -> Arc { + let polls = Arc::new(AtomicUsize::new(0)); + tokio::spawn(run(Counted { + driver, + polls: polls.clone(), + })); + 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) + } +} + pub fn now() -> moq_net::time::Instant { tokio::time::Instant::now().into_std() } @@ -31,6 +62,8 @@ pub struct MockConnectOptions { pub server_subscribe: Option, /// One-way delay for stream data in each direction. pub latency: std::time::Duration, + /// What both transports report as their connection statistics. + pub stats: MockStats, } impl MockConnectOptions { @@ -43,6 +76,7 @@ impl MockConnectOptions { server_publish: None, server_subscribe: None, latency: std::time::Duration::ZERO, + stats: MockStats::default(), } } } @@ -56,6 +90,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. @@ -71,6 +107,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().with_versions(opts.version.into()); @@ -106,16 +144,16 @@ pub async fn connect_mock(opts: MockConnectOptions) -> MockPair { .accept(now(), server_transport) .await .expect("server handshake failed"); - tokio::spawn(run(driver)); - session + (session, spawn_counted(driver)) }; - let (client_session, server_session) = tokio::join!(client_fut, server_fut); + let (client_session, (server_session, server_polls)) = tokio::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 e2813e1f4ba..20b2d656515 100644 --- a/rs/moq-net/tests/support/mock.rs +++ b/rs/moq-net/tests/support/mock.rs @@ -15,7 +15,10 @@ use std::{ future::Future, pin::Pin, - sync::{Arc, Mutex}, + sync::{ + Arc, Mutex, + atomic::{AtomicUsize, Ordering}, + }, task::{Context, Poll}, time::Duration, }; @@ -402,6 +405,29 @@ struct SessionSide { withheld: Mutex, /// Whether the datagrams this side sends are lost. lossy: Mutex, + /// 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 web_transport_trait::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. @@ -555,13 +581,24 @@ impl poll::Session for MockSession { } fn stats(&self) -> impl web_transport_trait::Stats { - web_transport_trait::StatsUnavailable + self.side.stats_reads.fetch_add(1, Ordering::Relaxed); + *self.side.stats.lock().unwrap() } } // Only some test binaries steer delivery. #[allow(dead_code)] impl MockSession { + /// 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) + } + /// Hold back the uni streams this side opens from now on. /// /// The peer's transport has them, so a FIN is acknowledged at once, but its application @@ -658,6 +695,8 @@ pub fn create_mock_session_pair(protocol: Option<&'static str>) -> (MockSession, held: Mutex::default(), withheld: Mutex::default(), lossy: Mutex::default(), + stats: Mutex::default(), + stats_reads: AtomicUsize::default(), }); let server_side = Arc::new(SessionSide { @@ -672,6 +711,8 @@ pub fn create_mock_session_pair(protocol: Option<&'static str>) -> (MockSession, held: Mutex::default(), withheld: Mutex::default(), lossy: Mutex::default(), + stats: Mutex::default(), + stats_reads: AtomicUsize::default(), }); let new = |side| MockSession {