| ... | @@ -7,8 +7,8 @@ | ... | @@ -7,8 +7,8 @@ |
| 7 | //! peers then meet afresh. | 7 | //! peers then meet afresh. |
| 8 | | 8 | |
| 9 | use super::{ | 9 | use super::{ |
| 10 | Event, Hello, OPENING, PATIENCE, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, | 10 | Event, Hello, OPENING, Paced, Peer, Pipe, Presence, Relayed as Answer, Room, Shared, Side, |
| 11 | Side, code_parts, group, | 11 | code_parts, group, |
| 12 | transport::{self, Address, Failure, parse}, | 12 | transport::{self, Address, Failure, parse}, |
| 13 | wire::kind, | 13 | wire::kind, |
| 14 | }; | 14 | }; |
| ... | @@ -25,6 +25,8 @@ use std::{ | ... | @@ -25,6 +25,8 @@ use std::{ |
| 25 | const KEEPALIVE: Duration = Duration::from_secs(30); | 25 | const KEEPALIVE: Duration = Duration::from_secs(30); |
| 26 | /// A connection that lasted this long was no failure, so the next waits only a second. | 26 | /// A connection that lasted this long was no failure, so the next waits only a second. |
| 27 | const STEADY: Duration = Duration::from_secs(60); | 27 | const STEADY: Duration = Duration::from_secs(60); |
| | 28 | /// The longest wait before joining again, so the room is back soon after a relay restarts. |
| | 29 | const REJOIN: Duration = Duration::from_secs(5); |
| 28 | /// The most sent in one message, well under any relay's cap. | 30 | /// The most sent in one message, well under any relay's cap. |
| 29 | const CHUNK: usize = 64 << 10; | 31 | const CHUNK: usize = 64 << 10; |
| 30 | /// The largest message read from the relay. | 32 | /// The largest message read from the relay. |
| ... | @@ -64,6 +66,8 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) { | ... | @@ -64,6 +66,8 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) { |
| 64 | .flatten(); | 66 | .flatten(); |
| 65 | while !shared.stopped.load(Ordering::Acquire) { | 67 | while !shared.stopped.load(Ordering::Acquire) { |
| 66 | let began = Instant::now(); | 68 | let began = Instant::now(); |
| | 69 | // The least the relay asked this end to wait. |
| | 70 | let mut asked = Duration::ZERO; |
| 67 | let path = match (owner, shared.tag()) { | 71 | let path = match (owner, shared.tag()) { |
| 68 | (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path), | 72 | (false, Some(tag)) => format!("{}/v1/room/{tag}", address.path), |
| 69 | (false, None) => return, | 73 | (false, None) => return, |
| ... | @@ -88,7 +92,7 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) { | ... | @@ -88,7 +92,7 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) { |
| 88 | if status == 410 { | 92 | if status == 410 { |
| 89 | return; | 93 | return; |
| 90 | } | 94 | } |
| 91 | wait = wait.max(retry.unwrap_or_default()); | 95 | asked = retry.unwrap_or_default(); |
| 92 | } | 96 | } |
| 93 | Err(Failure::Trouble(trouble, error)) => { | 97 | Err(Failure::Trouble(trouble, error)) => { |
| 94 | eprintln!("Live: no relay at {}: {error}", address.authority); | 98 | eprintln!("Live: no relay at {}: {error}", address.authority); |
| ... | @@ -98,11 +102,20 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) { | ... | @@ -98,11 +102,20 @@ fn keep(shared: &Arc<Shared>, address: &Address, port: u16) { |
| 98 | if began.elapsed() >= STEADY { | 102 | if began.elapsed() >= STEADY { |
| 99 | wait = Duration::from_secs(1); | 103 | wait = Duration::from_secs(1); |
| 100 | } | 104 | } |
| 101 | thread::sleep(wait); | 105 | thread::sleep(pause(wait, asked)); |
| 102 | wait = (wait * 2).min(PATIENCE); | 106 | wait = (wait * 2).min(REJOIN); |
| 103 | } | 107 | } |
| 104 | } | 108 | } |
| 105 | | 109 | |
| | 110 | /// How long to wait before joining again: between half `wait` and all of it, so peers a |
| | 111 | /// restart hung up on come back apart, and never less than the relay `asked`. |
| | 112 | fn pause(wait: Duration, asked: Duration) -> Duration { |
| | 113 | let mut random = [0; 4]; |
| | 114 | let _ = getrandom::fill(&mut random); |
| | 115 | let share = f64::from(u32::from_le_bytes(random)) / f64::from(u32::MAX); |
| | 116 | wait.mul_f64(0.5 + share / 2.0).max(asked) |
| | 117 | } |
| | 118 | |
| 106 | /// Opens the relay's `path` at `address`: the connection, and what reads its messages. | 119 | /// Opens the relay's `path` at `address`: the connection, and what reads its messages. |
| 107 | fn connect( | 120 | fn connect( |
| 108 | shared: &Shared, | 121 | shared: &Shared, |
| ... | @@ -565,6 +578,24 @@ impl Pipe for Relayed { | ... | @@ -565,6 +578,24 @@ impl Pipe for Relayed { |
| 565 | mod tests { | 578 | mod tests { |
| 566 | use super::*; | 579 | use super::*; |
| 567 | | 580 | |
| | 581 | /// However long a relay stays away, the next try comes within seconds of its return, |
| | 582 | /// tries spread apart, and none comes before the relay's Retry-After. |
| | 583 | #[test] |
| | 584 | fn rejoining_waits_a_little_spread_and_as_asked() { |
| | 585 | let mut wait = Duration::from_secs(1); |
| | 586 | let mut pauses = Vec::new(); |
| | 587 | for _ in 0..12 { |
| | 588 | pauses.push(pause(wait, Duration::ZERO)); |
| | 589 | wait = (wait * 2).min(REJOIN); |
| | 590 | } |
| | 591 | assert!( |
| | 592 | pauses.iter().all(|pause| *pause <= Duration::from_secs(5)), |
| | 593 | "{pauses:?}" |
| | 594 | ); |
| | 595 | assert!(pauses.windows(2).any(|two| two[0] != two[1])); |
| | 596 | assert!(pause(wait, Duration::from_secs(30)) >= Duration::from_secs(30)); |
| | 597 | } |
| | 598 | |
| 568 | #[test] | 599 | #[test] |
| 569 | fn relay_addresses_parse() { | 600 | fn relay_addresses_parse() { |
| 570 | let address = parse("wss://live.example.net").unwrap(); | 601 | let address = parse("wss://live.example.net").unwrap(); |