From d49f0885912507e7914945bfccfc66c0d3ca0dda Mon Sep 17 00:00:00 2001 From: Frando Date: Fri, 13 Nov 2026 16:05:51 +0100 Subject: [PATCH] feat(moq-net): share an allocator across a server's sessions Server::with_allocator mirrors Client::with_allocator, so the sessions a server accepts reserve into one registry and each probes for its sum. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/CHANGELOG.md | 1 + rs/moq-net/src/server.rs | 49 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/CHANGELOG.md b/rs/moq-net/CHANGELOG.md index bc8fcc2d8f2..d545d72fa3f 100644 --- a/rs/moq-net/CHANGELOG.md +++ b/rs/moq-net/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `socket`: `Endpoint`, a server, a client, or both, which announces a hidden `/.client/` broadcast at its first dial and `/.server/` at its first accept, so grants decide its roles, dials servers, and accepts clients' dials in `track` sessions (`dial_track`, `accept_track`, `TrackStream`), a track each way with one message per group. Dials and accepts fail `Unsupported` while the origin has a moq-lite client session before 07 whose publish half doesn't opt into hidden broadcasts, since its peer never hears the endpoint; see `doc/concept/socket.md`. - `transport::SETUP_RECEIVE_WINDOW`, the connection credit a server offers before it admits a session: enough for the SETUP and one in-band AUTH. +- `Server::with_allocator`, the accepting mirror of `Client::with_allocator`: every session the server accepts reserves into one `bandwidth::Allocator` and probes for the sum, instead of each starting an empty registry. ## [0.3.10](https://github.com/moq-dev/moq/compare/moq-net-v0.3.9...moq-net-v0.3.10) - 2026-10-05 diff --git a/rs/moq-net/src/server.rs b/rs/moq-net/src/server.rs index 1856657c471..9183150fee2 100644 --- a/rs/moq-net/src/server.rs +++ b/rs/moq-net/src/server.rs @@ -100,6 +100,7 @@ pub struct Server { implementation: Option, timeout: Option, receive_window: Option, + allocator: Option, } impl Default for Server { @@ -114,6 +115,7 @@ impl Default for Server { implementation: None, timeout: Some(DEFAULT_TIMEOUT), receive_window: None, + allocator: None, } } } @@ -140,6 +142,22 @@ impl Server { self } + /// Share `allocator`'s reservations with every session this server accepts. + /// + /// The accepting mirror of [`Client::with_allocator`](crate::Client::with_allocator): + /// each session probes for what the reservations add up to, and its + /// [`Session::allocator`](crate::Session::allocator) reserves into the same registry. + /// Without one, each session starts an empty registry of its own. + pub fn with_allocator(mut self, allocator: crate::bandwidth::Allocator) -> Self { + self.allocator = Some(allocator); + self + } + + /// The registry the next session probes for. + fn allocator(&self) -> crate::bandwidth::Allocator { + self.allocator.clone().unwrap_or_default() + } + /// Attach a per-connection [`stats::Session`] context. The session's publish /// (egress) and subscribe (ingress) origin handles are tagged with it, so all /// traffic counters are attributed through the model for this session's lifetime, @@ -293,7 +311,7 @@ impl Server { credential: start.credential, estimate: Some(start.estimate), peer_target: Some(start.peer_target), - bandwidth: crate::bandwidth::Allocator::default(), + bandwidth: self.allocator(), viewers, peer_implementation: start.peer_implementation, datagrams_unrouted: start.datagrams_unrouted, @@ -937,7 +955,7 @@ impl PausedIetf { credential: None, estimate: None, peer_target: None, - bandwidth: crate::bandwidth::Allocator::default(), + bandwidth: server.allocator(), viewers: Default::default(), peer_implementation, datagrams_unrouted: None, @@ -1068,7 +1086,7 @@ impl NegotiatedStart { credential: start.credential, estimate: Some(start.estimate), peer_target: Some(start.peer_target), - bandwidth: crate::bandwidth::Allocator::default(), + bandwidth: server.allocator(), viewers, peer_implementation: start.peer_implementation, datagrams_unrouted: start.datagrams_unrouted, @@ -1822,6 +1840,31 @@ mod tests { } } + /// An accepted session reserves into the registry the server was given, so one + /// allocator spans every session a server accepts, as a client's spans its dials. + #[kio::test] + async fn an_accepted_session_shares_the_servers_allocator() { + let allocator = crate::bandwidth::Allocator::default(); + let mut target = allocator.target(); + let session = FakeSession::new(ALPN_18, [ietf_setup(ietf::Version::Draft18, None)]); + let request = Server::new() + .with_allocator(allocator.clone()) + .accept_request(kio::clock::now(), session) + .await + .expect("accept"); + let (session, _driver) = request.ok().await.expect("ok"); + + let broadcast = crate::broadcast::Info::default().produce(); + let track = broadcast.create_track("t", crate::track::Info::timed()).unwrap(); + let _reserved = session + .allocator() + .reserve(&track.demand(), crate::bandwidth::Rate::from_bps(1_000_000)); + assert_eq!( + target.poll_changed(&kio::Waiter::noop()), + std::task::Poll::Ready(crate::bandwidth::Rate::from_bps(1_000_000)) + ); + } + /// A peer that never takes the SETUP reply is refused at the handshake's deadline /// with the timeout code, and the session's stats count it. #[kio::test]