diff --git a/docs/research/channel-network-design.md b/docs/research/channel-network-design.md index 7e2cc9beb..4c6a5ebcb 100644 --- a/docs/research/channel-network-design.md +++ b/docs/research/channel-network-design.md @@ -744,7 +744,7 @@ One stable and one irregular observation can instead use random-port preparation the irregular side opens 256 sockets toward the stable side's retained tuple; the stable side checks 512 distinct ports on the authorized peer address, each once. Each endpoint permits at most 520 requests and 16 replies, -with at least 2 ms between sends, within the same five-second preparation lifetime. +at a mean of one send per 2 ms, within the same five-second preparation lifetime. At most two random preparations run per process across all sites and Drivers. Capacity refusal does not queue work or extend the deadline; a pair-scoped refusal ends the other endpoint's preparation and both continue ordinary ICE. Other diff --git a/prototype/channel-native/src/peer/preparation.rs b/prototype/channel-native/src/peer/preparation.rs index 5e31ef534..1a4cc1c2a 100644 --- a/prototype/channel-native/src/peer/preparation.rs +++ b/prototype/channel-native/src/peer/preparation.rs @@ -38,6 +38,8 @@ const RANDOM_PORTS: usize = 256; const RANDOM_TARGETS: usize = RANDOM_PORTS * 2; const RANDOM_REQUEST_BUDGET: usize = RANDOM_TARGETS + 8; const RANDOM_REPLY_BUDGET: usize = 16; +// The mean spacing of a schedule, not a sleep per packet: a timer that wakes on a +// coarser tick (15.6 ms on Windows) sends what is due and the rate holds. const RANDOM_PACKET_INTERVAL: Duration = Duration::from_millis(2); // Shared across every site and Driver in this process. Busy peers fall back; // they do not queue another pool behind the current pair's deadline. @@ -547,7 +549,7 @@ impl Survey { sleep_until(next_send.into()).await; socket.send_to(&request.raw, plan.targets[0]).await?; requests_left -= 1; - next_send = Instant::now() + RANDOM_PACKET_INTERVAL; + next_send += RANDOM_PACKET_INTERVAL; } send(commands, preparation.pair, UdpSignal::Ready {}).await?; } else if plan.random == Some(RandomRole::Search) { @@ -555,6 +557,8 @@ impl Survey { } let mut reads: FuturesUnordered<_> = sockets.iter().enumerate().map(|(i,s)| receive(i,s)).collect(); let start = Instant::now(); + // The wait for Ready is not time a schedule may catch up on. + next_send = start; let mut round = 0; let mut probe = 0; let probe_limit = if plan.random == Some(RandomRole::Search) { plan.targets.len() } else { RANDOM_PORTS }; @@ -585,7 +589,7 @@ impl Survey { (index, target, requests[index].raw.clone()) }; sockets[index].send_to(&bytes, target).await?; - next_send = Instant::now() + if random { RANDOM_PACKET_INTERVAL } else { Duration::ZERO }; + next_send += if random { RANDOM_PACKET_INTERVAL } else { Duration::ZERO }; }, message = incoming.recv(), if !peer_complete => { ensure!(matches!(message, Some(UdpSignal::Complete)), "expected peer preparation completion"); @@ -1389,6 +1393,7 @@ mod tests { let (hard_commands, mut hard_events) = mpsc::channel(4); let (to_hard, mut hard_input) = mpsc::channel(4); let occupied = RANDOM_PREPARATIONS.try_acquire()?; + let primed = Instant::now(); let hard = tokio::spawn(async move { Survey { preparation: Preparation::new(pair, [73; 32], Ipv4Addr::LOCALHOST, expires)?, @@ -1439,6 +1444,13 @@ mod tests { ), "pool readiness missing" ); + // A sleep per packet takes about four seconds here on a 15.6 ms timer and + // leaves the search none of the preparation's five. + ensure!( + primed.elapsed() < RANDOM_PACKET_INTERVAL * RANDOM_PORTS as u32 * 3, + "pool priming lost its packet rate: {:?}", + primed.elapsed() + ); ensure!( (2..=RANDOM_PORTS).contains(&sources.len()), "expected distinct pool sockets within the configured bound, saw {}",