diff --git a/rust/Cargo.lock b/rust/Cargo.lock index a1b41b7c7..98b3444a8 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -369,9 +369,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b62ddb9cb1ec0a098ad4bbf9344d0713fa193ae1a80af55febcff2627b6a00c1" dependencies = [ + "futures-core", "getrandom", "instant", + "pin-project-lite", "rand", + "tokio", ] [[package]] @@ -1115,6 +1118,7 @@ name = "firezone-client-connlib" version = "0.1.0" dependencies = [ "async-trait", + "backoff", "boringtun", "chrono", "firezone-tunnel", @@ -1130,6 +1134,7 @@ name = "firezone-gateway-connlib" version = "0.1.0" dependencies = [ "async-trait", + "backoff", "boringtun", "chrono", "firezone-tunnel", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 28287bf88..69b390d95 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -16,6 +16,7 @@ members = [ boringtun = { git = "https://github.com/firezone/boringtun", branch = "master", default-features = false } chrono = { version = "0.4", default-features = false, features = ["std", "clock", "oldtime", "serde"] } swift-bridge = "0.1.52" +backoff = { version = "0.4", features = ["tokio"] } # Patched to use https://github.com/rust-lang/cc-rs/pull/708 # (the `patch` section can't be used for build deps...) diff --git a/rust/connlib/libs/client/Cargo.toml b/rust/connlib/libs/client/Cargo.toml index 41c7f1c20..d51af65f5 100644 --- a/rust/connlib/libs/client/Cargo.toml +++ b/rust/connlib/libs/client/Cargo.toml @@ -14,6 +14,7 @@ libs-common = { path = "../common" } firezone-tunnel = { path = "../tunnel" } serde = { version = "1.0", default-features = false, features = ["std", "derive"] } boringtun = { workspace = true } +backoff = { workspace = true } [dev-dependencies] serde_json = { version = "1.0", default-features = false, features = ["std"] } diff --git a/rust/connlib/libs/client/src/control.rs b/rust/connlib/libs/client/src/control.rs index a8c4e4b9a..1ecdefd9f 100644 --- a/rust/connlib/libs/client/src/control.rs +++ b/rust/connlib/libs/client/src/control.rs @@ -1,6 +1,7 @@ use std::{sync::Arc, time::Duration}; use crate::messages::{Connect, ConnectionDetails, EgressMessages, InitClient, Messages}; +use backoff::{ExponentialBackoff, ExponentialBackoffBuilder}; use boringtun::x25519::StaticSecret; use libs_common::{ control::{ErrorInfo, ErrorReply, MessageResult, PhoenixSenderWithTopic}, @@ -254,4 +255,8 @@ impl ControlSession for ControlPlane fn socket_path() -> &'static str { "device" } + + fn retry_strategy() -> ExponentialBackoff { + ExponentialBackoffBuilder::default().build() + } } diff --git a/rust/connlib/libs/common/Cargo.toml b/rust/connlib/libs/common/Cargo.toml index f4db8daec..de2e48b6b 100644 --- a/rust/connlib/libs/common/Cargo.toml +++ b/rust/connlib/libs/common/Cargo.toml @@ -23,7 +23,7 @@ tokio = { version = "1.28", default-features = false, features = ["rt", "rt-mult url = { version = "2.3.1", default-features = false } rand_core = { version = "0.6.4", default-features = false, features = ["std"] } async-trait = { version = "0.1", default-features = false } -backoff = { version = "0.4", default-features = false } +backoff = { workspace = true } ip_network = { version = "0.4", default-features = false, features = ["serde"] } boringtun = { workspace = true } os_info = { version = "3", default-features = false } diff --git a/rust/connlib/libs/common/src/session.rs b/rust/connlib/libs/common/src/session.rs index 7182554dd..ec41a5e6f 100644 --- a/rust/connlib/libs/common/src/session.rs +++ b/rust/connlib/libs/common/src/session.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use backoff::{backoff::Backoff, ExponentialBackoffBuilder}; +use backoff::{backoff::Backoff, ExponentialBackoff}; use boringtun::x25519::{PublicKey, StaticSecret}; use ip_network::IpNetwork; use parking_lot::Mutex; @@ -40,6 +40,9 @@ pub trait ControlSession { /// Either "gateway" or "client" used to get the control-plane URL. fn socket_path() -> &'static str; + + /// Retry strategy in case of disconnection for the session. + fn retry_strategy() -> ExponentialBackoff; } // TODO: Currently I'm using Session for both gateway and clients @@ -306,7 +309,7 @@ where ); tokio::spawn(async move { - let mut exponential_backoff = ExponentialBackoffBuilder::default().build(); + let mut exponential_backoff = T::retry_strategy(); loop { // `connection.start` calls the callback only after connecting let result = connection.start(vec![topic.clone()], || exponential_backoff.reset()).await; diff --git a/rust/connlib/libs/gateway/Cargo.toml b/rust/connlib/libs/gateway/Cargo.toml index 70c5d9747..5e5f3ecc4 100644 --- a/rust/connlib/libs/gateway/Cargo.toml +++ b/rust/connlib/libs/gateway/Cargo.toml @@ -12,6 +12,7 @@ tracing = { version = "0.1", default-features = false, features = ["std", "attri serde = { version = "1.0", default-features = false, features = ["std", "derive"] } boringtun = { workspace = true } chrono = { workspace = true } +backoff = { workspace = true } [dev-dependencies] serde_json = { version = "1.0", default-features = false, features = ["std"] } diff --git a/rust/connlib/libs/gateway/src/control.rs b/rust/connlib/libs/gateway/src/control.rs index eae70eb00..343c72185 100644 --- a/rust/connlib/libs/gateway/src/control.rs +++ b/rust/connlib/libs/gateway/src/control.rs @@ -1,5 +1,6 @@ use std::{sync::Arc, time::Duration}; +use backoff::{ExponentialBackoff, ExponentialBackoffBuilder}; use boringtun::x25519::StaticSecret; use firezone_tunnel::{ControlSignal, Tunnel}; use libs_common::{ @@ -164,4 +165,10 @@ impl ControlSession for ControlPla fn socket_path() -> &'static str { "gateway" } + + fn retry_strategy() -> ExponentialBackoff { + ExponentialBackoffBuilder::default() + .with_max_elapsed_time(None) + .build() + } }