diff options
| author | Markus Pettersson <markus.pettersson@mullvad.net> | 2024-02-13 13:25:01 +0100 |
|---|---|---|
| committer | Markus Pettersson <markus.pettersson@mullvad.net> | 2024-02-15 16:28:36 +0100 |
| commit | dc9929ba4af952e2d88b11dc9f36c880f1f339f5 (patch) | |
| tree | efd200426f0f061265d32ea8e9703e3cacb30b14 | |
| parent | a000aa96ca4f4a029030b398f364c56f9d32f8b9 (diff) | |
| download | mullvadvpn-dc9929ba4af952e2d88b11dc9f36c880f1f339f5.tar.xz mullvadvpn-dc9929ba4af952e2d88b11dc9f36c880f1f339f5.zip | |
Swap out `talpid-core` for `talpid-future`
| -rw-r--r-- | Cargo.lock | 4 | ||||
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 3 | ||||
| -rw-r--r-- | mullvad-daemon/src/device/service.rs | 2 | ||||
| -rw-r--r-- | mullvad-daemon/src/geoip.rs | 6 | ||||
| -rw-r--r-- | mullvad-daemon/src/version_check.rs | 7 | ||||
| -rw-r--r-- | mullvad-relay-selector/Cargo.toml | 2 | ||||
| -rw-r--r-- | mullvad-relay-selector/src/updater.rs | 2 | ||||
| -rw-r--r-- | mullvad-setup/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-setup/src/main.rs | 11 | ||||
| -rw-r--r-- | talpid-core/Cargo.toml | 20 | ||||
| -rw-r--r-- | talpid-core/src/future_retry.rs | 151 | ||||
| -rw-r--r-- | talpid-core/src/lib.rs | 3 |
12 files changed, 30 insertions, 182 deletions
diff --git a/Cargo.lock b/Cargo.lock index c224f63639..f34ef3c6bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1890,6 +1890,7 @@ dependencies = [ "simple-signal", "talpid-core", "talpid-dbus", + "talpid-future", "talpid-platform-metadata", "talpid-time", "talpid-types", @@ -2021,7 +2022,7 @@ dependencies = [ "parking_lot", "rand 0.8.5", "serde_json", - "talpid-core", + "talpid-future", "talpid-types", "tokio", ] @@ -2041,6 +2042,7 @@ dependencies = [ "mullvad-version", "once_cell", "talpid-core", + "talpid-future", "talpid-types", "tokio", ] diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index 543151941a..8677d18857 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -34,9 +34,10 @@ mullvad-api = { path = "../mullvad-api" } mullvad-fs = { path = "../mullvad-fs" } mullvad-version = { path = "../mullvad-version" } talpid-core = { path = "../talpid-core" } -talpid-types = { path = "../talpid-types" } +talpid-future = { path = "../talpid-future" } talpid-platform-metadata = { path = "../talpid-platform-metadata" } talpid-time = { path = "../talpid-time" } +talpid-types = { path = "../talpid-types" } [target.'cfg(not(target_os="android"))'.dependencies] clap = { workspace = true } diff --git a/mullvad-daemon/src/device/service.rs b/mullvad-daemon/src/device/service.rs index 4f62466b6f..c4c949ceba 100644 --- a/mullvad-daemon/src/device/service.rs +++ b/mullvad-daemon/src/device/service.rs @@ -17,7 +17,7 @@ use mullvad_api::{ rest::{self, MullvadRestHandle}, AccountsProxy, DevicesProxy, }; -use talpid_core::future_retry::{retry_future, ConstantInterval, ExponentialBackoff, Jittered}; +use talpid_future::retry::{retry_future, ConstantInterval, ExponentialBackoff, Jittered}; /// Retry strategy used for user-initiated actions that require immediate feedback const RETRY_ACTION_STRATEGY: ConstantInterval = ConstantInterval::new(Duration::ZERO, Some(3)); /// Retry strategy used for background tasks diff --git a/mullvad-daemon/src/geoip.rs b/mullvad-daemon/src/geoip.rs index 3f357abc3e..14e46a914b 100644 --- a/mullvad-daemon/src/geoip.rs +++ b/mullvad-daemon/src/geoip.rs @@ -7,10 +7,8 @@ use mullvad_api::{ }; use mullvad_types::location::{AmIMullvad, GeoIpLocation, LocationEventData}; use once_cell::sync::Lazy; -use talpid_core::{ - future_retry::{retry_future, ExponentialBackoff, Jittered}, - mpsc::Sender, -}; +use talpid_core::mpsc::Sender; +use talpid_future::retry::{retry_future, ExponentialBackoff, Jittered}; use talpid_types::ErrorExt; use crate::{DaemonEventSender, InternalDaemonEvent}; diff --git a/mullvad-daemon/src/version_check.rs b/mullvad-daemon/src/version_check.rs index d7c7eaa805..69d2d61643 100644 --- a/mullvad-daemon/src/version_check.rs +++ b/mullvad-daemon/src/version_check.rs @@ -15,7 +15,8 @@ use std::{ str::FromStr, time::Duration, }; -use talpid_core::{future_retry::ConstantInterval, mpsc::Sender}; +use talpid_core::mpsc::Sender; +use talpid_future::retry::{retry_future, ConstantInterval}; use talpid_types::ErrorExt; use tokio::fs::{self, File}; @@ -193,7 +194,7 @@ impl VersionUpdater { .map_err(Error::Download) }; - Box::pin(talpid_core::future_retry::retry_future( + Box::pin(retry_future( download_future_factory, move |result| Self::should_retry_immediate(result, &api_handle), IMMEDIATE_RETRY_STRATEGY, @@ -233,7 +234,7 @@ impl VersionUpdater { } }; - Box::pin(talpid_core::future_retry::retry_future( + Box::pin(retry_future( download_future_factory, |result| result.is_err(), std::iter::repeat(UPDATE_INTERVAL_ERROR), diff --git a/mullvad-relay-selector/Cargo.toml b/mullvad-relay-selector/Cargo.toml index 6ca82364fe..078415f31a 100644 --- a/mullvad-relay-selector/Cargo.toml +++ b/mullvad-relay-selector/Cargo.toml @@ -21,7 +21,7 @@ rand = "0.8.5" serde_json = "1.0" tokio = { workspace = true, features = ["fs", "io-util", "time"] } -talpid-core = { path = "../talpid-core" } +talpid-future = { path = "../talpid-future" } talpid-types = { path = "../talpid-types" } mullvad-api = { path = "../mullvad-api" } mullvad-types = { path = "../mullvad-types" } diff --git a/mullvad-relay-selector/src/updater.rs b/mullvad-relay-selector/src/updater.rs index e9d65d1dd8..9e86cbb413 100644 --- a/mullvad-relay-selector/src/updater.rs +++ b/mullvad-relay-selector/src/updater.rs @@ -12,7 +12,7 @@ use std::{ sync::Arc, time::{Duration, SystemTime, UNIX_EPOCH}, }; -use talpid_core::future_retry::{retry_future, ExponentialBackoff, Jittered}; +use talpid_future::retry::{retry_future, ExponentialBackoff, Jittered}; use talpid_types::ErrorExt; use tokio::fs::File; diff --git a/mullvad-setup/Cargo.toml b/mullvad-setup/Cargo.toml index c873252355..bb602303db 100644 --- a/mullvad-setup/Cargo.toml +++ b/mullvad-setup/Cargo.toml @@ -30,4 +30,5 @@ mullvad-api = { path = "../mullvad-api" } mullvad-types = { path = "../mullvad-types" } mullvad-version = { path = "../mullvad-version" } talpid-core = { path = "../talpid-core" } +talpid-future = { path = "../talpid-future" } talpid-types = { path = "../talpid-types" } diff --git a/mullvad-setup/src/main.rs b/mullvad-setup/src/main.rs index cf93b2d039..4b14319414 100644 --- a/mullvad-setup/src/main.rs +++ b/mullvad-setup/src/main.rs @@ -1,13 +1,12 @@ use clap::Parser; +use once_cell::sync::Lazy; +use std::{path::PathBuf, process, str::FromStr, time::Duration}; + use mullvad_api::{self, proxy::ApiConnectionMode, DEVICE_NOT_FOUND}; use mullvad_management_interface::MullvadProxyClient; use mullvad_types::version::ParsedAppVersion; -use once_cell::sync::Lazy; -use std::{path::PathBuf, process, str::FromStr, time::Duration}; -use talpid_core::{ - firewall::{self, Firewall}, - future_retry::{retry_future, ConstantInterval}, -}; +use talpid_core::firewall::{self, Firewall}; +use talpid_future::retry::{retry_future, ConstantInterval}; use talpid_types::ErrorExt; static APP_VERSION: Lazy<ParsedAppVersion> = diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml index e9d17cc584..2256efedd6 100644 --- a/talpid-core/Cargo.toml +++ b/talpid-core/Cargo.toml @@ -11,22 +11,21 @@ rust-version.workspace = true workspace = true [dependencies] +chrono = { workspace = true, features = ["clock"] } err-derive = { workspace = true } futures = "0.3.15" ipnetwork = "0.16" -once_cell = { workspace = true } libc = "0.2" log = { workspace = true } +once_cell = { workspace = true } parking_lot = "0.12.0" +rand = "0.8.5" talpid-routing = { path = "../talpid-routing" } -talpid-types = { path = "../talpid-types" } -talpid-time = { path = "../talpid-time" } -talpid-tunnel-config-client = { path = "../talpid-tunnel-config-client" } talpid-tunnel = { path = "../talpid-tunnel" } +talpid-tunnel-config-client = { path = "../talpid-tunnel-config-client" } +talpid-types = { path = "../talpid-types" } talpid-wireguard = { path = "../talpid-wireguard" } -chrono = { workspace = true, features = ["clock"] } tokio = { workspace = true, features = ["process", "rt-multi-thread", "fs"] } -rand = "0.8.5" [target.'cfg(not(target_os="android"))'.dependencies] talpid-openvpn = { path = "../talpid-openvpn" } @@ -47,13 +46,14 @@ duct = "0.13" [target.'cfg(target_os = "macos")'.dependencies] +async-trait = "0.1" +duct = "0.13" pfctl = "0.4.4" +subslice = "0.2" system-configuration = "0.5.1" -trust-dns-server = { version = "0.23.0", features = ["resolver"] } +talpid-time = { path = "../talpid-time" } trust-dns-proto = "0.23.0" -subslice = "0.2" -async-trait = "0.1" -duct = "0.13" +trust-dns-server = { version = "0.23.0", features = ["resolver"] } [target.'cfg(windows)'.dependencies] diff --git a/talpid-core/src/future_retry.rs b/talpid-core/src/future_retry.rs deleted file mode 100644 index 4cdc7e1282..0000000000 --- a/talpid-core/src/future_retry.rs +++ /dev/null @@ -1,151 +0,0 @@ -use rand::{distributions::OpenClosed01, Rng}; -use std::{future::Future, ops::Deref, time::Duration}; -use talpid_time::sleep; - -/// Retries a future until it should stop as determined by the retry function, or when -/// the iterator returns `None`. -pub async fn retry_future< - F: FnMut() -> O + 'static, - R: FnMut(&T) -> bool + 'static, - D: Iterator<Item = Duration> + 'static, - O: Future<Output = T>, - T, ->( - mut factory: F, - mut should_retry: R, - mut delays: D, -) -> T { - loop { - let current_result = factory().await; - if should_retry(¤t_result) { - if let Some(delay) = delays.next() { - sleep(delay).await; - continue; - } - } - return current_result; - } -} - -/// Iterator that repeats the same interval, with an optional maximum no. of attempts. -pub struct ConstantInterval { - interval: Duration, - attempt: usize, - max_attempts: Option<usize>, -} - -impl ConstantInterval { - /// Creates a `ConstantInterval` that repeats `interval`, at most `max_attempts` times. - pub const fn new(interval: Duration, max_attempts: Option<usize>) -> ConstantInterval { - ConstantInterval { - interval, - attempt: 0, - max_attempts, - } - } -} - -impl Iterator for ConstantInterval { - type Item = Duration; - - fn next(&mut self) -> Option<Duration> { - if let Some(max_attempts) = self.max_attempts { - if self.attempt >= max_attempts { - return None; - } - } - self.attempt = self.attempt.saturating_add(1); - Some(self.interval) - } -} - -/// Provides an exponential back-off timer to delay the next retry of a failed operation. -#[derive(Clone)] -pub struct ExponentialBackoff { - next: Duration, - factor: u32, - max_delay: Option<Duration>, -} - -impl ExponentialBackoff { - /// Creates a `ExponentialBackoff` starting with the provided duration. - /// - /// All else staying the same, the first delay will be `initial` long, the second - /// one will be `initial * factor`, third `initial * factor^2` and so on. - pub const fn new(initial: Duration, factor: u32) -> ExponentialBackoff { - ExponentialBackoff { - next: initial, - factor, - max_delay: None, - } - } - - /// Set the maximum delay. By default, there is no maximum value set. The limit is - /// `Duration::MAX`. - pub const fn max_delay(mut self, duration: Option<Duration>) -> ExponentialBackoff { - self.max_delay = duration; - self - } - - /// Returns the value of the delay and advances the next back-off delay. - fn next_delay(&mut self) -> Duration { - let next = self.next; - - if let Some(max_delay) = self.max_delay { - if next > max_delay { - return max_delay; - } - } - - self.next = next.saturating_mul(self.factor); - - next - } -} - -impl Iterator for ExponentialBackoff { - type Item = Duration; - fn next(&mut self) -> Option<Duration> { - Some(self.next_delay()) - } -} - -/// Adds jitter to a duration iterator -pub struct Jittered<I> { - inner: I, -} - -impl<I> Jittered<I> { - /// Create an iterator of jittered durations - pub const fn jitter(inner: I) -> Self { - Self { inner } - } -} - -impl<I: Iterator<Item = Duration>> Iterator for Jittered<I> { - type Item = Duration; - fn next(&mut self) -> Option<Self::Item> { - let next_value = self.inner.next()?; - Some(jitter(next_value)) - } -} - -impl<I> Deref for Jittered<I> { - type Target = I; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -/// Apply a jitter to a duration. -fn jitter(dur: Duration) -> Duration { - apply_jitter(dur, rand::thread_rng().sample(OpenClosed01)) -} - -fn apply_jitter(duration: Duration, jitter: f64) -> Duration { - let secs = (duration.as_secs() as f64) * jitter; - let nanos = (duration.subsec_nanos() as f64) * jitter; - let millis = (secs * 1000f64) + (nanos / 1000000f64); - Duration::from_millis(millis as u64) -} diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs index d0b6a98b38..84bb4ad90c 100644 --- a/talpid-core/src/lib.rs +++ b/talpid-core/src/lib.rs @@ -35,9 +35,6 @@ pub mod dns; /// State machine to handle tunnel configuration. pub mod tunnel_state_machine; -/// Future utilities -pub mod future_retry; - /// Misc utilities for the Linux platform. #[cfg(target_os = "linux")] mod linux; |
