Skip to content

Commit

Permalink
cleanup constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jan 9, 2025
1 parent 8cf7dc5 commit 3bec867
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
11 changes: 10 additions & 1 deletion iroh-relay/src/protos/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ const MAX_FRAME_SIZE: usize = 1024 * 1024;
/// The Relay magic number, sent in the FrameType::ClientInfo frame upon initial connection.
const MAGIC: &str = "RELAY🔑";

/// Maximum time for a relay server to respond to a relay protocol ping.
#[cfg(feature = "server")]
pub(crate) const KEEP_ALIVE: Duration = Duration::from_secs(60);
pub(crate) const PING_TIMEOUT: Duration = Duration::from_secs(5);

/// Interval in which we ping the relay server to ensure the connection is alive.
///
/// The default QUIC max_idle_timeout is 30s, so setting that to half this time gives some
/// chance of recovering.
#[cfg(feature = "server")]
pub(crate) const PING_INTERVAL: Duration = Duration::from_secs(15);

/// The number of packets buffered for sending per client
#[cfg(feature = "server")]
pub(crate) const PER_CLIENT_SEND_QUEUE_DEPTH: usize = 512; //32;
Expand Down
20 changes: 12 additions & 8 deletions iroh-relay/src/server/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ use futures_util::{SinkExt, Stream, StreamExt};
use iroh_base::NodeId;
use iroh_metrics::{inc, inc_by};
use rand::Rng;
use tokio::sync::mpsc::{self, error::TrySendError};
use tokio::{
sync::mpsc::{self, error::TrySendError},
time::MissedTickBehavior,
};
use tokio_util::{sync::CancellationToken, task::AbortOnDropHandle};
use tracing::{debug, error, instrument, trace, warn, Instrument};

use crate::{
protos::{
disco,
relay::{write_frame, Frame, KEEP_ALIVE},
relay::{write_frame, Frame, PING_INTERVAL, PING_TIMEOUT},
},
server::{clients::Clients, metrics::Metrics, streams::RelayedStream, ClientRateLimit},
PingTracker,
Expand Down Expand Up @@ -104,7 +107,7 @@ impl Client {
node_id,
connection_id,
clients: clients.clone(),
ping_tracker: PingTracker::new(Duration::from_secs(5)),
ping_tracker: PingTracker::new(PING_TIMEOUT),
};

// start io loop
Expand Down Expand Up @@ -227,12 +230,13 @@ impl Actor {
// Add some jitter to ping pong interactions, to avoid all pings being sent at the same time
let next_interval = || {
let random_secs = rand::rngs::OsRng.gen_range(1..=5);
Duration::from_secs(random_secs) + KEEP_ALIVE
Duration::from_secs(random_secs) + PING_INTERVAL
};

let mut keep_alive = tokio::time::interval(next_interval());
let mut ping_interval = tokio::time::interval(next_interval());
// ticks immediately
keep_alive.tick().await;
ping_interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
ping_interval.tick().await;

loop {
tokio::select! {
Expand Down Expand Up @@ -267,10 +271,10 @@ impl Actor {
trace!("pong timed out");
break;
}
_ = keep_alive.tick() => {
_ = ping_interval.tick() => {
trace!("keep alive ping");
// new interval
keep_alive.reset_after(next_interval());
ping_interval.reset_after(next_interval());
let data = self.ping_tracker.new_ping();
self.write_frame(Frame::Ping { data }).await?;
}
Expand Down

0 comments on commit 3bec867

Please sign in to comment.