Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ pub fn random_token() -> String {
pub fn random_prefixed_string(prefix: &str) -> String {
format!("{}-{}", prefix, random_string())
}

/// Returns a duration of approximately 1 hour, with random jitter of +/- 10 minutes.
pub fn random_reconnect_interval() -> std::time::Duration {
const BASE_SECONDS: u64 = 60 * 60; // 1 hour
const JITTER_SECONDS: u64 = 10 * 60; // 10 minutes

let jitter: i64 =
rand::thread_rng().gen_range(-(JITTER_SECONDS as i64)..=(JITTER_SECONDS as i64));
let total_seconds = (BASE_SECONDS as i64 + jitter) as u64;
std::time::Duration::from_secs(total_seconds)
}
7 changes: 7 additions & 0 deletions plane/src/drone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use plane_common::{
},
typed_socket::{client::TypedSocketConnector, TypedSocketSender},
types::{BackendState, ClusterName, DronePoolName},
util::random_reconnect_interval,
PlaneClient,
};
use runtime::docker::DockerRuntime;
Expand Down Expand Up @@ -98,8 +99,14 @@ pub async fn drone_loop(
log_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut message_counts: HashMap<&'static str, u64> = HashMap::new();

let reconnect_at = tokio::time::Instant::now() + random_reconnect_interval();

loop {
tokio::select! {
_ = tokio::time::sleep_until(reconnect_at) => {
tracing::info!("Periodic reconnect triggered.");
break;
}
_ = log_interval.tick() => {
let (outgoing, incoming) = socket.channel_depths();
tracing::info!(
Expand Down
7 changes: 7 additions & 0 deletions plane/src/proxy/proxy_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use plane_common::{
names::ProxyName,
protocol::{MessageFromProxy, MessageToProxy, RouteInfoRequest},
types::ClusterName,
util::random_reconnect_interval,
PlaneClient,
};
use std::{collections::HashMap, sync::Arc, time::Duration};
Expand Down Expand Up @@ -62,8 +63,14 @@ impl ProxyConnection {
log_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut message_counts: HashMap<&'static str, u64> = HashMap::new();

let reconnect_at = tokio::time::Instant::now() + random_reconnect_interval();

loop {
tokio::select! {
_ = tokio::time::sleep_until(reconnect_at) => {
tracing::info!("Periodic reconnect triggered.");
break;
}
_ = log_interval.tick() => {
let (outgoing, incoming) = conn.channel_depths();
tracing::info!(
Expand Down
Loading