diff --git a/mullvad-ios/src/ios/api_client/mod.rs b/mullvad-ios/src/ios/api_client/mod.rs index 432c80bafe..d64468b9c7 100644 --- a/mullvad-ios/src/ios/api_client/mod.rs +++ b/mullvad-ios/src/ios/api_client/mod.rs @@ -16,7 +16,7 @@ use mullvad_types::access_method::Id; use response::ApiResponse; use retry_strategy::RetryStrategy; use std::{future::Future, sync::Arc}; -use talpid_future::retry::retry_future; +use talpid_future::retry::retry_future2; mod access_method_resolver; mod access_method_settings; @@ -270,5 +270,5 @@ where Ok(_) => false, }; - retry_future(future_factory, should_retry, retry_strategy.delays()).await + retry_future2(future_factory, should_retry, retry_strategy.delays()).await } diff --git a/talpid-future/src/retry.rs b/talpid-future/src/retry.rs index a7ae243829..2a35cc9d4a 100644 --- a/talpid-future/src/retry.rs +++ b/talpid-future/src/retry.rs @@ -28,6 +28,32 @@ pub async fn retry_future< } } +/// Retries a future until it should stop as determined by the retry function, or when +/// the iterator returns `None`. Unlike [`retry_future`], the future may not reference the function. +pub async fn retry_future2< + F: FnMut() -> O, + R: FnMut(&T) -> bool, + D: Iterator, + O: Future, + T, +>( + mut factory: F, + mut should_retry: R, + mut delays: D, +) -> T { + loop { + let current_result = factory().await; + if should_retry(¤t_result) + && 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,