Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions rs/kio/src/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ impl<T> Weak<T> {
}
.produce()
}

/// Read the state while another handle keeps it allocated, even once the channel
/// closed. Counts as neither a producer nor a consumer. `None` once it was dropped.
pub fn read<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R> {
let state = self.state.upgrade()?;
let state = Ref { state: state.lock() };
Some(f(&state))
}
}

impl<T> Default for Weak<T> {
Expand Down Expand Up @@ -405,6 +413,22 @@ mod test {
assert!(weak.upgrade().is_none());
}

/// A closed channel stays readable through the weak handle while any handle keeps
/// it allocated, without that read reopening it or counting as demand.
#[test]
fn weak_reads_a_closed_channel() {
let producer = Producer::new(7u32);
let weak = producer.downgrade();
let consumer = producer.consume();

drop(producer);
assert_eq!(weak.read(|value| *value), Some(7));
assert!(weak.upgrade().is_none(), "reading does not reopen it");

drop(consumer);
assert_eq!(weak.read(|value| *value), None);
}

/// An upgrade racing the last producer's drop either loses (no handle) or wins
/// (a handle that is genuinely open). It must never hand back a producer for a
/// channel that the drop is about to close.
Expand Down
7 changes: 6 additions & 1 deletion rs/moq-json/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,16 @@ mod test {

*producer.modify().unwrap() = json!({ "big": "x".repeat(moq_net::group::MAX_CACHE_BYTES as usize + 1) });

// The publisher learns the cause at its next edit, the consumer from the aborted track.
// The publisher learns the cause at its next edit. The consumer drains the snapshot
// that finished, then learns it from the aborted track.
assert!(matches!(
producer.modify(),
Err(crate::Error::Net(moq_net::Error::FrameTooLarge))
));
assert!(matches!(
subscriber.poll_next_group(&kio::Waiter::noop()),
Poll::Ready(Ok(Some(_)))
));
assert!(matches!(
subscriber.poll_next_group(&kio::Waiter::noop()),
Poll::Ready(Err(moq_net::Error::FrameTooLarge))
Expand Down
24 changes: 16 additions & 8 deletions rs/moq-net/src/model/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
//! global eviction task.
//!
//! Cross-track ordering comes from one statistic: the mean last-access time of the
//! evictable population (every cached group except each track's protected latest).
//! evictable population (every cached group except each live track's protected latest).
//! A group accessed more recently than that mean is never evicted, so freshly read
//! or fetched content in one track can't die while another track holds staler
//! content, and a track
//! whose oldest group is staler than the mean accrues debt at double rate. Evicting
//! old entries and inserting new ones both advance the mean, so the eviction
//! frontier moves with cache turnover on its own.
//!
//! The pool also owns the wall-clock LRU window ([`Pool::expiry`]): a non-latest
//! group that nobody has read or written for that long is reclaimed, no matter what
//! retention its track advertises. Track retention
//! The pool also owns the wall-clock LRU window ([`Pool::expiry`]): a group that
//! nobody has read or written for that long is reclaimed, no matter what retention
//! its track advertises. Only a live track's latest group is exempt: once a track
//! ends, a stale consumer can't pin any of it. Track retention
//! ([`max_age`](crate::track::Info::max_age)) is measured in media timestamps, so a
//! congestion stall can't age content out; the pool's expiry is the orthogonal
//! wall-clock bound that keeps unwatched content from pinning RAM.
Expand Down Expand Up @@ -101,8 +102,8 @@ impl Config {

/// Set the wall-clock LRU window. `None` disables idle reclamation.
///
/// A non-latest cached group that nobody reads or writes for this long is
/// reclaimed, surfacing to any remaining reader as
/// A cached group (other than a live track's latest) that nobody reads or writes
/// for this long is reclaimed, surfacing to any remaining reader as
/// [`Error::Old`](crate::Error::Old). This is independent of track retention:
/// [`max_age`](crate::track::Info::max_age) uses media timestamps, while this
/// window keeps idle content from pinning memory. Reclaiming without a write behind
Expand Down Expand Up @@ -269,7 +270,7 @@ impl Pool {
/// Call after polling and at the returned deadline, including when idle.
/// Calls before that deadline only advance the pool's sampled clock. A due
/// pass visits every cached group, dating accesses since the last pass and
/// reclaiming idle groups except each track's latest. Delayed calls extend
/// reclaiming idle groups except each live track's latest. Delayed calls extend
/// retention. Shared pools use the latest supplied instant.
///
/// `None` means both cache policies are disabled. After enabling a capacity
Expand Down Expand Up @@ -564,7 +565,14 @@ impl Track {
}
// Counts as a producer while it lives, which is why `track::Producer` gates
// its teardown on its own clone count rather than the state's.
let Some(state) = self.state.upgrade() else { return };
let Some(state) = self.state.upgrade() else {
// An ended track's channel is closed, but a stale consumer can still hold its
// groups: the sweep expires them in place, or they would never go.
if full && scan_expiry {
self.state.read(|state| state.expire_closed(state.expiry_scan_drain()));
}
return;
};
let expiry = if scan_expiry {
let state = state.read();
let scan = if full {
Expand Down
Loading
Loading