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
81 changes: 73 additions & 8 deletions rs/moq-net/src/coding/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Reader<S: RecvStream, V> {
want: usize,
/// A contiguous copy of a message that spans transport chunks, empty between decodes.
scratch: Vec<u8>,
/// Bytes taken into a frame's unsealed segment, whose receive credit returns once it seals.
unreleased: u64,
stats: crate::stats::Session,
}

Expand All @@ -50,9 +52,10 @@ pub(crate) trait Pause {
crate::model::pace::AHEAD
}

/// Reserve the credit this stream may hold while paused, returning whether it may pause.
/// A stream outside any session always may.
fn pause(&mut self) -> bool {
/// Reserve the credit this stream may hold while paused, its stream window and the `taken`
/// bytes it read whose credit it has yet to return, returning whether it may pause. A
/// stream outside any session always may.
fn pause(&mut self, _taken: u64) -> bool {
true
}

Expand All @@ -65,8 +68,8 @@ impl Pause for crate::transport::boxed::RecvStream {
crate::transport::boxed::RecvStream::window(self)
}

fn pause(&mut self) -> bool {
crate::transport::boxed::RecvStream::pause(self)
fn pause(&mut self, taken: u64) -> bool {
crate::transport::boxed::RecvStream::pause(self, taken)
}

fn resume(&mut self) {
Expand Down Expand Up @@ -97,6 +100,7 @@ impl<S: RecvStream, V: StreamCodes> Reader<S, V> {
version,
want: 0,
scratch: Vec::new(),
unreleased: 0,
stats: Default::default(),
}
}
Expand Down Expand Up @@ -392,7 +396,13 @@ impl<S: RecvStream, V: StreamCodes> Reader<S, V> {
// A writer too far ahead of the group's fastest reader leaves the rest in the
// stream, so flow control pushes back upstream.
let lease = match self.poll_pace(waiter, frame.poll_ready(waiter, self.stream.window())) {
Poll::Ready(Ok(())) => self.poll_lease(waiter, frame.remaining().min(1)),
// The stream alone, so `unreleased` can count while the lease is out.
Poll::Ready(Ok(())) => {
let version = &self.version;
self.stream
.poll_lease(waiter, frame.remaining().min(1))
.map_err(|err| version.transport_error(err))
}
Poll::Ready(Err(err)) => break Err(err),
Poll::Pending => Poll::Pending,
};
Expand Down Expand Up @@ -430,8 +440,10 @@ impl<S: RecvStream, V: StreamCodes> Reader<S, V> {
if complete || chunk.len() == frame.unsealed() {
// Every byte of the segment is in hand, so its credit returns.
lease.release();
self.unreleased = 0;
} else {
drop(lease);
self.unreleased += chunk.len() as u64;
}

if !chunk.is_empty() {
Expand Down Expand Up @@ -488,7 +500,7 @@ impl<S: RecvStream, V: StreamCodes> Reader<S, V> {
self.stream.resume();
Poll::Ready(Ok(()))
}
Poll::Pending if self.stream.pause() => {
Poll::Pending if self.stream.pause(self.unreleased) => {
let err = ready!(self.stream.poll_reset(waiter));
Poll::Ready(Err(self.version.transport_error(err)))
}
Expand Down Expand Up @@ -557,6 +569,7 @@ impl<S: RecvStream, V: StreamCodes> Reader<S, V> {
version,
want: self.want,
scratch: self.scratch,
unreleased: self.unreleased,
stats: self.stats,
}
}
Expand Down Expand Up @@ -1145,11 +1158,27 @@ mod tests {
chunks: VecDeque<Bytes>,
held: usize,
released: usize,
/// The pacing window, or the cap when zero.
window: u64,
/// The taken bytes each pause reserved for.
paused: Vec<u64>,
}

struct CreditedLease<'a>(&'a mut Credited);

impl Pause for Credited {}
impl Pause for Credited {
fn window(&self) -> u64 {
match self.window {
0 => crate::model::pace::AHEAD,
window => window,
}
}

fn pause(&mut self, taken: u64) -> bool {
self.paused.push(taken);
true
}
}

impl RecvStream for Credited {
type Error = crate::lite::test_transport::SinkError;
Expand Down Expand Up @@ -1237,4 +1266,40 @@ mod tests {
}
assert_eq!(read, data);
}

/// A stream paused partway through a segment still holds the credit of what it took of
/// it, so the pause reserves that along with the stream window.
#[test]
fn a_pause_mid_segment_reserves_what_it_took() {
use crate::frame::SEGMENT;
const CHUNK: usize = 100_000;

// An idle reader behind one committed frame, so the writer is paced past the window.
let mut group = crate::group::Info { start: 0 }.produce();
let _idle = group.consume();
group
.write_frame(crate::Timestamp::ZERO, Bytes::from_static(b"x"))
.unwrap();
let mut frame = group
.create_frame(crate::frame::Info {
size: 2 * SEGMENT as u64,
timestamp: crate::Timestamp::ZERO,
})
.unwrap();

let stream = Credited {
window: SEGMENT as u64 / 2,
..Default::default()
};
let mut reader = Reader::new(stream, crate::lite::Version::Lite05);
for _ in 0..SEGMENT / CHUNK {
reader.stream.chunks.push_back(Bytes::from(vec![0u8; CHUNK]));
}
let noop = &kio::Waiter::noop();
assert!(reader.poll_read_frame(noop, &mut frame).is_pending(), "paced mid-frame");

let stream = &reader.stream;
assert!(stream.held > 0 && stream.held < SEGMENT, "{} bytes held", stream.held);
assert_eq!(stream.paused, [stream.held as u64], "the pause reserves what it holds");
}
}
5 changes: 3 additions & 2 deletions rs/moq-net/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ impl Metrics for Untracked {}
///
/// A session's ingest stops reading a group's stream while the group's fastest reader is far
/// behind, so flow control pushes back to the publisher. Such a paused stream keeps up to
/// `stream` bytes counted against the shared `connection` window, so the session pauses only
/// as many streams as fit in half of it; the rest keep reading. See
/// `stream` bytes it has not read, plus what it read of a frame's unsealed segment, counted
/// against the shared `connection` window, so the session pauses only as many streams as fit
/// in half of it; the rest keep reading. See
/// [`poll::Session::receive_window`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
Expand Down
60 changes: 36 additions & 24 deletions rs/moq-net/src/transport/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,16 @@ pub(super) struct Shared<S: ?Sized> {
}

impl Shared<dyn ErasedSession> {
/// Reserve the credit of one more paused stream, unless it would push the paused total
/// over half the connection window, returning how much it reserved. Without a connection
/// window a paused stream holds only its own credit, so it reserves nothing.
fn reserve(&self) -> Option<u64> {
/// Reserve the credit of one more paused stream, its stream window and the `taken` bytes it
/// read without returning their credit, unless it would push the paused total over half the
/// connection window, returning how much it reserved. Without a connection window a paused
/// stream holds only its own credit, so it reserves nothing.
fn reserve(&self, taken: u64) -> Option<u64> {
let window = self.session.receive_window()?;
let Some(connection) = window.connection else {
return Some(0);
};
let charge = window.stream.min(connection.get());
let charge = window.stream.saturating_add(taken).min(connection.get());
let cap = connection.get() / 2;
self.paused
.fetch_update(Ordering::AcqRel, Ordering::Acquire, |paused| {
Expand Down Expand Up @@ -468,11 +469,12 @@ impl RecvStream {
self.0.window()
}

/// Reserve the receive credit this stream may hold while ingest stops reading it, returning
/// whether it may stop: not once the session's paused streams hold half its connection
/// window, nor on a transport that reports no window.
pub(crate) fn pause(&mut self) -> bool {
self.0.pause()
/// Reserve the receive credit this stream may hold while ingest stops reading it, its stream
/// window and the `taken` bytes read without returning their credit, returning whether it
/// may stop: not once the session's paused streams hold half its connection window, nor on
/// a transport that reports no window.
pub(crate) fn pause(&mut self, taken: u64) -> bool {
self.0.pause(taken)
}

/// Return the credit [`Self::pause`] reserved, as ingest reads the stream again.
Expand Down Expand Up @@ -617,7 +619,7 @@ trait ErasedRecv: Send + Sync {
fn stop(&mut self, code: u32);
fn poll_reset(&mut self, waiter: &kio::Waiter) -> Poll<Error>;
fn window(&self) -> u64;
fn pause(&mut self) -> bool;
fn pause(&mut self, taken: u64) -> bool;
fn resume(&mut self);
}

Expand Down Expand Up @@ -843,11 +845,11 @@ impl<T: poll::RecvStream + Send + Sync> ErasedRecv for Budgeted<T> {
})
}

fn pause(&mut self) -> bool {
fn pause(&mut self, taken: u64) -> bool {
if self.held > 0 {
return true;
}
let Some(held) = self.session.upgrade().and_then(|shared| shared.reserve()) else {
let Some(held) = self.session.upgrade().and_then(|shared| shared.reserve(taken)) else {
return false;
};
self.held = held;
Expand Down Expand Up @@ -1112,8 +1114,9 @@ mod test {
recv
}

/// Each paused stream reserves its stream window, and the session pauses no more than
/// fit in half the connection window; a stream resuming or dropped returns its share.
/// Each paused stream reserves its stream window and what it took of an unsealed segment,
/// and the session pauses no more than fit in half the connection window; a stream
/// resuming or dropped returns its share.
#[test]
fn paused_streams_hold_at_most_half_the_connection_window() {
let gate = kio::Producer::new(true);
Expand All @@ -1122,22 +1125,31 @@ mod test {
.with_receive_window(ReceiveWindow::new(std::num::NonZeroU64::new(100), 20)),
);
let mut streams: Vec<_> = (0..3).map(|_| accept(&session)).collect();
assert!(streams[0].pause());
assert!(streams[0].pause(), "a paused stream reserves once");
assert!(streams[1].pause());
assert!(!streams[2].pause(), "a third would hold 60 of 100");
assert!(streams[0].pause(0));
assert!(streams[0].pause(0), "a paused stream reserves once");
assert!(streams[1].pause(0));
assert!(!streams[2].pause(0), "a third would hold 60 of 100");

streams[0].resume();
assert!(streams[2].pause(), "a resumed stream returned its share");
assert!(streams[2].pause(0), "a resumed stream returned its share");
drop(streams.remove(1));
assert!(accept(&session).pause(), "so did a dropped one");
assert!(accept(&session).pause(0), "so did a dropped one");

// A stream partway through a segment also holds the credit of what it took of it.
let session = Session::new(
SinkSession::accepted_bi(gate.consume())
.with_receive_window(ReceiveWindow::new(std::num::NonZeroU64::new(100), 20)),
);
let mut first = accept(&session);
assert!(first.pause(25), "holds 45 of 100");
assert!(!accept(&session).pause(0), "a second would hold 65 of 100");

// Without a connection window nothing is shared, so every stream may pause.
let session = Session::new(
SinkSession::accepted_bi(gate.consume()).with_receive_window(ReceiveWindow::new(None, u64::MAX)),
);
let mut streams: Vec<_> = (0..3).map(|_| accept(&session)).collect();
assert!(streams.iter_mut().all(|stream| stream.pause()));
assert!(streams.iter_mut().all(|stream| stream.pause(0)));
}

/// A stream paces at the credit its session grants it, so a hop holds about what its
Expand Down Expand Up @@ -1366,14 +1378,14 @@ mod test {
fn an_unbounded_stream_never_pauses() {
let gate = kio::Producer::new(true);
let session = Session::new(SinkSession::accepted_bi(gate.consume()));
assert!(!accept(&session).pause());
assert!(!accept(&session).pause(0));

let session = Session::new(
SinkSession::accepted_bi(gate.consume())
.with_receive_window(ReceiveWindow::new(std::num::NonZeroU64::new(100), 20)),
);
let mut stream = accept(&session);
drop(session);
assert!(!stream.pause());
assert!(!stream.pause(0));
}
}
2 changes: 1 addition & 1 deletion rs/moq-quic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Changes on top of the upstream commit, besides the renames:
- **Fuzzing** runs through the `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand.
- **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period.
- **Late stream limits**: peer transport parameters that raise a stream limit after an open was refused wake `Streams::poll_open`, as `MAX_STREAMS` does. A server accepted before the rest of a multi-packet ClientHello (a post-quantum key share is enough) otherwise leaves a 0.5-RTT `open_uni` parked until the idle timeout. Not offered upstream yet.
- **Receive leases** (`streams/lease.rs`, `recv::Stream::lease`), building on [#5675](https://github.com/moq-dev/moq/pull/5675)'s held credit: a lease reads a stream's ordered bytes out of the assembler but keeps their connection credit, counted in `StreamsState::held`, until the reader `advance`s or `take`s past them and `release`s, while stream credit advances as usual. A lease copies the bytes it reads out of their datagram buffers (`Leased::extend`), so a byte of credit never pins a receive batch, whether its reader waits, parks, or takes it. The lease lives in the stream's `Recv`, which a leased stream keeps, with its stream ID, until the application stops it, even past the FIN. When the peer has spent the connection window and no reader can make progress, the stream ranked lowest by `set_priority` (the newest on a tie) is evicted: its bytes and credit are freed, the stream is stopped without `STOP_SENDING`, and its next lease fails with `recv::Error::Evicted`; the application's `stop` then tells the peer and frees the stream once its final size is known. Leased streams that may hold credit are kept in `StreamsState::ranks`, in eviction order, so an eviction costs a logarithm rather than a scan of every stream. `MAX_DATA` goes out for any increase once the peer is within an eighth of the window (`max_data_due`), since what leases release may never add up to the usual threshold. Streams with data their reader has not looked at hold eviction off; they are kept in `StreamsState::unlooked`, so the stuck check stays constant time however many readers wait. `ConnectionStats::credit` reports the leased and available bytes, the streams evicted, and the peak credit spent. A `recv::Lease` owns the chunks it took, so a reader holds no connection borrow while it parses; `restore` or `release` hands it back, and an eviction pass waits while one is out. A port touching `Chunks` or `add_read_credits` has to keep `held` balanced.
- **Receive leases** (`streams/lease.rs`, `recv::Stream::lease`), building on [#5675](https://github.com/moq-dev/moq/pull/5675)'s held credit: a lease reads a stream's ordered bytes out of the assembler but keeps their connection credit, counted in `StreamsState::held`, until the reader `advance`s or `take`s past them and `release`s. Stream credit runs from the bytes the reader took rather than leased (`Leased::untaken`), widened to a waiting reader's `want`, so a reader that stops taking holds at most its stream window of bytes it has not taken, besides those it took and has not released. A lease copies the bytes it reads out of their datagram buffers (`Leased::extend`), so a byte of credit never pins a receive batch, whether its reader waits, parks, or takes it. The lease lives in the stream's `Recv`, which a leased stream keeps, with its stream ID, until the application stops it, even past the FIN. When the peer has spent the connection window and no reader can make progress, the stream ranked lowest by `set_priority` (the newest on a tie) is evicted: its bytes and credit are freed, the stream is stopped without `STOP_SENDING`, and its next lease fails with `recv::Error::Evicted`; the application's `stop` then tells the peer and frees the stream once its final size is known. Leased streams that may hold credit are kept in `StreamsState::ranks`, in eviction order, so an eviction costs a logarithm rather than a scan of every stream. `MAX_DATA` goes out for any increase once the peer is within an eighth of the window (`max_data_due`), since what leases release may never add up to the usual threshold. Streams with data their reader has not looked at hold eviction off; they are kept in `StreamsState::unlooked`, so the stuck check stays constant time however many readers wait. `ConnectionStats::credit` reports the leased and available bytes, the streams evicted, and the peak credit spent. A `recv::Lease` owns the chunks it took, so a reader holds no connection borrow while it parses; `restore` or `release` hands it back, and an eviction pass waits while one is out. A port touching `Chunks` or `add_read_credits` has to keep `held` balanced.
- **Owned datagrams**: a received datagram is copied out of its packet when queued, since `datagram_receive_buffer_size` charges only its payload and a slice keeps the whole receive batch alive. quinn has the same gap; not offered upstream yet.
- **Stream credit on stop**: `recv::Stream::stop` on a stream whose final size is already known (finished or reset) frees it and queues `MAX_STREAMS` at once, as reading the reset does, instead of leaving the credit until the next packet arrives. Not offered upstream yet; quinn `main` has the same gap.
- **Drop resets** (`quic::driver::send::Stream`): a send stream dropped before `finish` is reset with code 0, or with the peer's `STOP_SENDING` code, instead of quinn's implicit `finish`, so a cancelled writer never ends a truncated stream with a FIN. This matches moq-uring's driver and moq-net's transport contract. `quic::web` resets after the WebTransport header with the mapped code. quinn's `stream_stopped_2` finishes its stream before dropping it. Not offered upstream: it reverses a documented quinn behaviour.
Expand Down
Loading