mirror of
https://github.com/aphrody-labs/mullvadvpn-app.git
synced 2026-10-13 22:10:39 +00:00
Add retry_future2 and use it in iOS api client
This commit is contained in:
2 files changed
+28
-2
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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<Item = Duration>,
|
||||
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)
|
||||
&& 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,
|
||||
|
||||
Reference in new issue
Block a user