diff options
| author | Emīls <emils@mullvad.net> | 2020-07-01 13:13:32 +0100 |
|---|---|---|
| committer | Emīls <emils@mullvad.net> | 2020-07-03 11:14:39 +0100 |
| commit | cb0acc43fe52a559c9c3914ef4373e4fff3d3af4 (patch) | |
| tree | 54108f66c6e76f1988ad8a90daa6fe36eaffe86c | |
| parent | 8b7a6fe8c352f06c7ec9d85a309ae747419778e7 (diff) | |
| download | mullvadvpn-cb0acc43fe52a559c9c3914ef4373e4fff3d3af4.tar.xz mullvadvpn-cb0acc43fe52a559c9c3914ef4373e4fff3d3af4.zip | |
Improve retrying
| -rw-r--r-- | talpid-core/src/future_retry.rs | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/talpid-core/src/future_retry.rs b/talpid-core/src/future_retry.rs index 74dc6f2f8c..bf992117bd 100644 --- a/talpid-core/src/future_retry.rs +++ b/talpid-core/src/future_retry.rs @@ -1,32 +1,30 @@ use rand::{distributions::OpenClosed01, Rng}; -use std::{future::Future, marker::Unpin, time::Duration}; +use std::{future::Future, time::Duration}; /// Since timers often exhibit weird behavior if they are running for too long, a workaround is /// required - run a timer for 60 seconds until a delay is shorter than 5 minutes. const MAX_SINGLE_DELAY: Duration = Duration::from_secs(5 * 60); /// Retries a future until it should stop as determined by the retry function. -pub fn retry_future_with_backoff< +pub async fn retry_future_with_backoff< F: FnMut() -> O + 'static, R: FnMut(&T) -> bool + 'static, D: Iterator<Item = Duration> + 'static, O: Future<Output = T>, - T: Unpin, + T, >( mut factory: F, mut should_retry: R, mut delays: D, -) -> impl Future<Output = T> + 'static { - async move { - loop { - let current_result = factory().await; - if should_retry(¤t_result) { - if let Some(delay) = delays.next() { - sleep(delay).await; - } - } else { - return current_result; +) -> T { + loop { + let current_result = factory().await; + if should_retry(¤t_result) { + if let Some(delay) = delays.next() { + sleep(delay).await; } + } else { + return current_result; } } } |
