Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions rs/moq-net/src/model/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,18 @@ impl Consumer {
}

/// A watch-only handle to the broadcast's demand. See [`Demand`].
pub(crate) fn demand(&self) -> Demand {
///
/// The consumer-side sibling of [`Producer::demand`], for a holder that has
/// only a read handle: a relay pulling a broadcast from upstream owns no
/// producer for it (the ingesting session does), yet the question it has to
/// answer is whether anything downstream is still reading. Holding this
/// handle, or the [`Consumer`] it came from, is not itself demand.
///
/// Two endings a caller has to tell apart. Demand going away is
/// [`Demand::unused`] resolving, and means nobody downstream is reading.
/// The broadcast going away is [`Error::Dropped`], and here that is the
/// upstream producer, not the readers.
pub fn demand(&self) -> Demand {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Mirror the public demand API in JavaScript

Making this method public changes the Rust moq-net API, but js/net/src/broadcast.ts::Consumer still has no broadcast-demand observer, so JavaScript consumers cannot implement the idle-pull lifecycle described here. The repository requires rs/moq-net API changes to be reconciled with js/net and doc/concept; add the corresponding JS surface and documentation, or explain why this API is intentionally Rust-only. (Written by GPT-5.6 Sol)

AGENTS.md reference: AGENTS.md:L199-L206

Useful? React with 👍 / 👎.

Demand {
alive: self.alive.weak(),
state: self.state.clone(),
Expand Down Expand Up @@ -1164,8 +1175,8 @@ impl super::WeakEntry for WeakConsumer {

/// A cloneable, watch-only handle to a broadcast's subscriber demand.
///
/// Obtained from [`Producer::demand`]; the broadcast-level sibling of
/// [`track::Demand`](crate::track::Demand). Demand means live interest in the
/// Obtained from [`Producer::demand`] or [`Consumer::demand`]; the broadcast-level
/// sibling of [`track::Demand`](crate::track::Demand). Demand means live interest in the
/// broadcast's content: a subscribed spliced track on a route-fed broadcast, or
/// a pending track request / a consumed track on an ordinary one. A publisher
/// uses it to run expensive work only while someone is watching, and routing
Expand Down Expand Up @@ -1302,22 +1313,58 @@ mod test {
let producer = Producer::new_spliced(Info::new());
let consumer = producer.consume();
let demand = producer.demand();
// The read handle answers the same question, which is all a relay
// holding a pulled broadcast has.
let watched = consumer.demand();

assert!(!demand.is_used());
assert!(!watched.is_used());
let track = consumer.track("video").unwrap();
assert!(demand.is_used());
assert!(watched.is_used());

// Dropping the only consumer wakes a parked `unused`, even though the
// logical track itself stays cached in the broadcast.
let (unused, ()) = tokio::join!(expect(demand.unused()), async { drop(track) });
// logical track itself stays cached in the broadcast. Parked on the read
// handle: that edge is what tells a relay its pull has no readers left.
let (unused, ()) = tokio::join!(expect(watched.unused()), async { drop(track) });
unused.unwrap();
assert!(!demand.is_used());
assert!(!watched.is_used());

// A repeat consumer for the cached track counts again.
let _track = consumer.track("video").unwrap();
assert!(demand.is_used());
}

/// A read handle's demand reports the producer going away as `Dropped`,
/// distinct from its readers going away.
///
/// The distinction a relay has to act on: no readers means stop pulling,
/// while an upstream that vanished means the pull is over. Both arrive on
/// the same handle, so the two have to be told apart by their result rather
/// than by which one resolved.
#[tokio::test]
async fn a_read_handle_reports_a_dropped_producer_apart_from_lost_demand() {
let producer = Producer::new_spliced(Info::new());
let consumer = producer.consume();
let watched = consumer.demand();

let track = consumer.track("video").unwrap();
assert!(watched.is_used());

// Readers go, the broadcast stays: demand ends, and the handle keeps
// answering.
let (unused, ()) = tokio::join!(expect(watched.unused()), async { drop(track) });
unused.unwrap();

// The producer goes: the same handle now refuses rather than reporting
// no demand, which is what stops a relay retrying a pull that has no
// source left.
drop(producer);
assert!(matches!(watched.used().await, Err(Error::Dropped)));
assert!(matches!(watched.unused().await, Err(Error::Dropped)));
}

/// Subscribe and assert the result hasn't resolved yet (it stays pending until
/// a publisher accepts). Returns the pending subscription to resolve after accepting.
macro_rules! subscribe_pending {
Expand Down
Loading