Skip to content

Commit abe253d

Browse files
Add talpid-future crate
Refactor some parts of `talpid-core` to `talpid-future`.
1 parent 23a007e commit abe253d

File tree

7 files changed

+297
-115
lines changed

7 files changed

+297
-115
lines changed

Cargo.lock

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,32 @@ members = [
1111
"android/translations-converter",
1212
"ios/MullvadREST/Transport/Shadowsocks/shadowsocks-proxy",
1313
"ios/TunnelObfuscation/tunnel-obfuscator-proxy",
14-
"mullvad-daemon",
14+
"mullvad-api",
1515
"mullvad-cli",
16+
"mullvad-daemon",
17+
"mullvad-exclude",
1618
"mullvad-fs",
17-
"mullvad-setup",
18-
"mullvad-problem-report",
1919
"mullvad-jni",
20+
"mullvad-management-interface",
21+
"mullvad-nsis",
2022
"mullvad-paths",
23+
"mullvad-problem-report",
2124
"mullvad-relay-selector",
25+
"mullvad-setup",
2226
"mullvad-types",
23-
"mullvad-api",
24-
"mullvad-exclude",
2527
"mullvad-version",
26-
"mullvad-nsis",
27-
"talpid-openvpn-plugin",
2828
"talpid-core",
2929
"talpid-dbus",
30+
"talpid-future",
3031
"talpid-openvpn",
32+
"talpid-openvpn-plugin",
3133
"talpid-platform-metadata",
3234
"talpid-routing",
3335
"talpid-time",
3436
"talpid-tunnel",
3537
"talpid-tunnel-config-client",
3638
"talpid-windows",
3739
"talpid-wireguard",
38-
"mullvad-management-interface",
3940
"tunnel-obfuscation",
4041
]
4142

talpid-core/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,3 @@ features = [
9292

9393
[build-dependencies]
9494
tonic-build = { workspace = true, default-features = false, features = ["transport", "prost"] }
95-
96-
[dev-dependencies]
97-
proptest = "1.4"
98-
tokio = { workspace = true, features = [ "test-util" ] }

talpid-core/src/future_retry.rs

-102
Original file line numberDiff line numberDiff line change
@@ -149,105 +149,3 @@ fn apply_jitter(duration: Duration, jitter: f64) -> Duration {
149149
let millis = (secs * 1000f64) + (nanos / 1000000f64);
150150
Duration::from_millis(millis as u64)
151151
}
152-
153-
#[cfg(test)]
154-
mod test {
155-
use super::*;
156-
use proptest::prelude::*;
157-
158-
#[test]
159-
fn test_constant_interval() {
160-
let mut ivl = ConstantInterval::new(Duration::from_secs(2), Some(3));
161-
162-
assert_eq!(ivl.next(), Some(Duration::from_secs(2)));
163-
assert_eq!(ivl.next(), Some(Duration::from_secs(2)));
164-
assert_eq!(ivl.next(), Some(Duration::from_secs(2)));
165-
assert_eq!(ivl.next(), None);
166-
}
167-
168-
#[test]
169-
fn test_constant_interval_no_max() {
170-
let mut ivl = ConstantInterval::new(Duration::from_secs(2), None);
171-
assert_eq!(ivl.next(), Some(Duration::from_secs(2)));
172-
}
173-
174-
#[test]
175-
fn test_exponential_backoff() {
176-
let mut backoff = ExponentialBackoff::new(Duration::from_secs(2), 3);
177-
178-
assert_eq!(backoff.next(), Some(Duration::from_secs(2)));
179-
assert_eq!(backoff.next(), Some(Duration::from_secs(6)));
180-
assert_eq!(backoff.next(), Some(Duration::from_secs(18)));
181-
}
182-
183-
#[test]
184-
fn test_at_maximum_value() {
185-
let max = Duration::MAX;
186-
let mu = Duration::from_micros(1);
187-
let mut backoff = ExponentialBackoff::new(max - mu, 2);
188-
189-
assert_eq!(backoff.next(), Some(max - mu));
190-
assert_eq!(backoff.next(), Some(max));
191-
assert_eq!(backoff.next(), Some(max));
192-
}
193-
194-
#[test]
195-
fn test_maximum_bound() {
196-
let mut backoff = ExponentialBackoff::new(Duration::from_millis(2), 3)
197-
.max_delay(Some(Duration::from_millis(7)));
198-
199-
assert_eq!(backoff.next(), Some(Duration::from_millis(2)));
200-
assert_eq!(backoff.next(), Some(Duration::from_millis(6)));
201-
assert_eq!(backoff.next(), Some(Duration::from_millis(7)));
202-
}
203-
204-
#[test]
205-
fn test_minimum_value() {
206-
let zero = Duration::from_millis(0);
207-
let mut backoff = ExponentialBackoff::new(zero, 10);
208-
209-
assert_eq!(backoff.next(), Some(zero));
210-
assert_eq!(backoff.next(), Some(zero));
211-
212-
let mut backoff = ExponentialBackoff::new(Duration::from_millis(1), 0);
213-
214-
assert_eq!(backoff.next(), Some(Duration::from_millis(1)));
215-
assert_eq!(backoff.next(), Some(zero));
216-
}
217-
218-
#[test]
219-
fn test_rounding() {
220-
let second = Duration::from_secs(1);
221-
assert_eq!(apply_jitter(second, 1.0), second);
222-
}
223-
224-
proptest! {
225-
#[test]
226-
fn test_jitter(millis: u64, jitter: u64) {
227-
let max_num = 2u64.checked_pow(f64::MANTISSA_DIGITS).unwrap();
228-
let jitter = (jitter % max_num) as f64 / (max_num as f64);
229-
let unjittered_duration = Duration::from_millis(millis);
230-
let jittered_duration = apply_jitter(unjittered_duration, jitter);
231-
prop_assert!(jittered_duration <= unjittered_duration);
232-
}
233-
}
234-
235-
// NOTE: The test is disabled because the clock does not advance.
236-
#[ignore]
237-
#[tokio::test]
238-
async fn test_exponential_backoff_delay() {
239-
let retry_interval_initial = Duration::from_secs(4);
240-
let retry_interval_factor = 5;
241-
let retry_interval_max = Duration::from_secs(24 * 60 * 60);
242-
tokio::time::pause();
243-
244-
let _ = retry_future(
245-
|| async { 0 },
246-
|_| true,
247-
ExponentialBackoff::new(retry_interval_initial, retry_interval_factor)
248-
.max_delay(Some(retry_interval_max))
249-
.take(5),
250-
)
251-
.await;
252-
}
253-
}

talpid-future/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "talpid-future"
3+
description = "Utilities for working with futures"
4+
authors.workspace = true
5+
repository.workspace = true
6+
license.workspace = true
7+
edition.workspace = true
8+
rust-version.workspace = true
9+
10+
[lints]
11+
workspace = true
12+
13+
[dependencies]
14+
rand = "0.8.5"
15+
talpid-time = { path = "../talpid-time" }
16+
17+
[dev-dependencies]
18+
proptest = "1.4"
19+
tokio = { workspace = true, features = [ "test-util", "macros" ] }

talpid-future/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! This library provides utility functions and types for working with futures.
2+
#![deny(missing_docs)]
3+
4+
/// Retry futures
5+
pub mod retry;

0 commit comments

Comments
 (0)