diff --git a/crates/notebook/src/live/relay.rs b/crates/notebook/src/live/relay.rs index 92badc5607b271b115ac7c5522da9e5a6f0c942f..b476b27b5ad11fd20dc133b5ab92e635d4767d4e 100644 --- a/crates/notebook/src/live/relay.rs +++ b/crates/notebook/src/live/relay.rs @@ -7,8 +7,8 @@ //! peers then meet afresh. use super::{ - Event, Hello, OPENING, PATIENCE, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, - Side, code_parts, group, + Event, Hello, OPENING, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, Side, + code_parts, group, transport::{self, Address, Failure, parse}, wire::kind, }; @@ -25,6 +25,8 @@ use std::{ const KEEPALIVE: Duration = Duration::from_secs(30); /// A connection that lasted this long was no failure, so the next waits only a second. const STEADY: Duration = Duration::from_secs(60); +/// The longest wait before joining again, so the room is back soon after a relay restarts. +const REJOIN: Duration = Duration::from_secs(5); /// The most sent in one message, well under any relay's cap. const CHUNK: usize = 64 << 10; /// The largest message read from the relay. @@ -64,6 +66,8 @@ fn keep(shared: &Arc, address: &Address, port: u16) { .flatten(); while !shared.stopped.load(Ordering::Acquire) { let began = Instant::now(); + // The least the relay asked this end to wait. + let mut asked = Duration::ZERO; let path = match (owner, shared.tag()) { (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path), (false, None) => return, @@ -88,7 +92,7 @@ fn keep(shared: &Arc, address: &Address, port: u16) { if status == 410 { return; } - wait = wait.max(retry.unwrap_or_default()); + asked = retry.unwrap_or_default(); } Err(Failure::Trouble(trouble, error)) => { eprintln!("Live: no relay at {}: {error}", address.authority); @@ -98,11 +102,20 @@ fn keep(shared: &Arc, address: &Address, port: u16) { if began.elapsed() >= STEADY { wait = Duration::from_secs(1); } - thread::sleep(wait); - wait = (wait * 2).min(PATIENCE); + thread::sleep(pause(wait, asked)); + wait = (wait * 2).min(REJOIN); } } +/// How long to wait before joining again: between half `wait` and all of it, so peers a +/// restart hung up on come back apart, and never less than the relay `asked`. +fn pause(wait: Duration, asked: Duration) -> Duration { + let mut random = [0; 4]; + let _ = getrandom::fill(&mut random); + let share = f64::from(u32::from_le_bytes(random)) / f64::from(u32::MAX); + wait.mul_f64(0.5 + share / 2.0).max(asked) +} + /// Opens the relay's `path` at `address`: the connection, and what reads its messages. fn connect( shared: &Shared, @@ -565,6 +578,24 @@ impl Pipe for Relayed { mod tests { use super::*; + /// However long a relay stays away, the next try comes within seconds of its return, + /// tries spread apart, and none comes before the relay's Retry-After. + #[test] + fn rejoining_waits_a_little_spread_and_as_asked() { + let mut wait = Duration::from_secs(1); + let mut pauses = Vec::new(); + for _ in 0..12 { + pauses.push(pause(wait, Duration::ZERO)); + wait = (wait * 2).min(REJOIN); + } + assert!( + pauses.iter().all(|pause| *pause <= Duration::from_secs(5)), + "{pauses:?}" + ); + assert!(pauses.windows(2).any(|two| two[0] != two[1])); + assert!(pause(wait, Duration::from_secs(30)) >= Duration::from_secs(30)); + } + #[test] fn relay_addresses_parse() { let address = parse("wss://live.example.net").unwrap(); diff --git a/crates/notebook/tests/support/live.rs b/crates/notebook/tests/support/live.rs index 37b7cbb050cd208138665b6e5ac12f6df45847a6..12f9917c05309e6829134600de4ebc1eae182887 100644 --- a/crates/notebook/tests/support/live.rs +++ b/crates/notebook/tests/support/live.rs @@ -285,7 +285,10 @@ impl PresenceRelay { impl Drop for PresenceRelay { fn drop(&mut self) { { - let mut state = self.state.lock().unwrap(); + let mut state = self + .state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); state.stopped = true; for (_, socket) in &state.sockets { let _ = socket.shutdown(std::net::Shutdown::Both);