diff --git a/Cargo.lock b/Cargo.lock index b1512375918..56c7d021ba8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10990,6 +10990,7 @@ dependencies = [ "rustls 0.23.18", "slog", "socket2 0.5.7", + "static_assertions", "thiserror 2.0.3", "tokio", "tokio-metrics", diff --git a/rs/p2p/quic_transport/BUILD.bazel b/rs/p2p/quic_transport/BUILD.bazel index 080eecea976..172d0aae4e7 100644 --- a/rs/p2p/quic_transport/BUILD.bazel +++ b/rs/p2p/quic_transport/BUILD.bazel @@ -29,6 +29,7 @@ DEPENDENCIES = [ "@crate_index//:rustls", "@crate_index//:slog", "@crate_index//:socket2", + "@crate_index//:static_assertions", "@crate_index//:thiserror", "@crate_index//:tokio", "@crate_index//:tokio-metrics", diff --git a/rs/p2p/quic_transport/Cargo.toml b/rs/p2p/quic_transport/Cargo.toml index b3e9d3ff158..a73957a5bf2 100644 --- a/rs/p2p/quic_transport/Cargo.toml +++ b/rs/p2p/quic_transport/Cargo.toml @@ -29,6 +29,7 @@ prost = { workspace = true } quinn = { workspace = true } rustls = { workspace = true } slog = { workspace = true } +static_assertions = { workspace = true } socket2 = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true } diff --git a/rs/p2p/quic_transport/src/connection_manager.rs b/rs/p2p/quic_transport/src/connection_manager.rs index 39e4022e012..bf0031ca976 100644 --- a/rs/p2p/quic_transport/src/connection_manager.rs +++ b/rs/p2p/quic_transport/src/connection_manager.rs @@ -48,6 +48,7 @@ use quinn::{ }; use rustls::pki_types::CertificateDer; use socket2::{Domain, Protocol, SockAddr, Socket, Type}; +use static_assertions::const_assert; use thiserror::Error; use tokio::{runtime::Handle, select, task::JoinSet}; use tokio_util::{sync::CancellationToken, time::DelayQueue}; @@ -79,7 +80,14 @@ const KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(1); /// that were not explicitly closed. I.e replica crash const IDLE_TIMEOUT: Duration = Duration::from_secs(5); const CONNECT_TIMEOUT: Duration = Duration::from_secs(10); -const CONNECT_RETRY_BACKOFF: Duration = Duration::from_secs(3); +const CONNECT_RETRY_BACKOFF: Duration = Duration::from_secs(5); + +// There should be least two probes before timing out a connection. +const_assert!(KEEP_ALIVE_INTERVAL.as_nanos() < IDLE_TIMEOUT.as_nanos()); +// The application level timeout should no less than the QUIC idle timeout. +const_assert!(IDLE_TIMEOUT.as_nanos() <= CONNECT_TIMEOUT.as_nanos()); +// The waiting time before re-trying to connect should be no less than the IDLE_TIMEOUT. +const_assert!(IDLE_TIMEOUT.as_nanos() <= CONNECT_RETRY_BACKOFF.as_nanos()); /// Connection manager is responsible for making sure that /// there always exists a healthy connection to each peer