Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add http::retry, adopt it in accounts

Manav Rathib275cf518f08
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index a6ce5faa9b..77621254de 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2331,6 +2331,7 @@ dependencies = [
"bytes",
"crypto_secretstream",
"futures-core",
+ "futures-timer",
"getrandom 0.2.17",
"hex",
"md-5 0.11.0",
@@ -3134,6 +3135,10 @@ name = "futures-timer"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24"
+dependencies = [
+ "gloo-timers",
+ "send_wrapper",
+]
[[package]]
name = "futures-util"
@@ -3403,6 +3408,18 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
+[[package]]
+name = "gloo-timers"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "js-sys",
+ "wasm-bindgen",
+]
+
[[package]]
name = "gobject-sys"
version = "0.18.0"
@@ -7728,6 +7745,12 @@ dependencies = [
"serde_core",
]
+[[package]]
+name = "send_wrapper"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0"
+
[[package]]
name = "serde"
version = "1.0.228"
diff --git a/rust/crates/accounts/src/client.rs b/rust/crates/accounts/src/client.rs
index aa24fa73dc..db514f7442 100644
--- a/rust/crates/accounts/src/client.rs
+++ b/rust/crates/accounts/src/client.rs
@@ -4,7 +4,7 @@ use base64::{Engine, engine::general_purpose::STANDARD};
use ente_core::{
auth::{SrpAttributes as CoreSrpAttributes, SrpSession},
crypto::SecretVec,
- http::{self, Api, ApiConfig, Auth, Http},
+ http::{self, Api, ApiConfig, Auth, Http, RetryProfile},
};
use std::time::Duration;
@@ -20,19 +20,10 @@ use crate::{
TwoFactorStatusResponse, TwoFactorType, UpdateSrpAndKeysRequest, UpdateSrpAndKeysResponse,
VerifyEmailRequest, VerifySrpSessionRequest, VerifyTotpRequest,
},
- types::{AccountsClientConfig, SleepFn},
+ types::AccountsClientConfig,
};
const SRP_A_LEN: usize = 512;
-const MAX_RETRIES: u32 = 3;
-const INITIAL_RETRY_DELAY_MS: u64 = 250;
-const MAX_RETRY_DELAY_MS: u64 = 2_000;
-
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-enum RetryPolicy {
- Default,
- NoRetry,
-}
fn pad_left(data: &[u8], len: usize) -> Vec<u8> {
if data.len() >= len {
@@ -56,13 +47,11 @@ fn require_srp_m2(auth_response: &AuthResponse) -> Result<&str> {
pub struct AccountsClient {
api: Api,
client_package: String,
- sleep_fn: SleepFn,
}
impl AccountsClient {
/// Construct a client from a config.
pub fn new(config: AccountsClientConfig) -> Result<Self> {
- let sleep_fn = config.sleep_fn();
let api = Api::new(
Http::new()?,
ApiConfig {
@@ -76,7 +65,6 @@ impl AccountsClient {
Ok(Self {
api,
client_package: config.client_package,
- sleep_fn,
})
}
@@ -90,66 +78,21 @@ impl AccountsClient {
&self.client_package
}
- async fn sleep(&self, duration: Duration) {
- (self.sleep_fn)(duration).await;
- }
-
- fn should_retry(error: &http::Error, policy: RetryPolicy) -> bool {
- match policy {
- RetryPolicy::NoRetry => false,
- RetryPolicy::Default => error.is_retryable(),
- }
- }
-
- async fn execute<T, F, Fut>(&self, policy: RetryPolicy, mut operation: F) -> Result<T>
- where
- F: FnMut() -> Fut,
- Fut: std::future::Future<Output = std::result::Result<T, http::Error>>,
- {
- if policy == RetryPolicy::NoRetry {
- return operation().await.map_err(Error::from);
- }
-
- let mut attempt = 0;
- let mut delay = Duration::from_millis(INITIAL_RETRY_DELAY_MS);
-
- loop {
- match operation().await {
- Ok(value) => return Ok(value),
- Err(error) if Self::should_retry(&error, policy) && attempt < MAX_RETRIES => {
- attempt += 1;
- log::warn!(
- "account request failed, retrying in {:?} (attempt {}/{})",
- delay,
- attempt,
- MAX_RETRIES
- );
- self.sleep(delay).await;
- delay = delay
- .saturating_mul(2)
- .min(Duration::from_millis(MAX_RETRY_DELAY_MS));
- }
- Err(error) => return Err(Error::from(error)),
- }
- }
- }
-
/// Get SRP attributes for a user by email.
pub async fn get_srp_attributes(&self, email: &str) -> Result<SrpAttributes> {
let query = [("email", email.to_string())];
- let response: GetSrpAttributesResponse = self
- .execute(RetryPolicy::Default, || async {
- self.api
- .get("/users/srp/attributes")
- .query(&query)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await?;
+ let response: GetSrpAttributesResponse = http::retry(RetryProfile::Interactive, || async {
+ self.api
+ .get("/users/srp/attributes")
+ .query(&query)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await
+ })
+ .await?;
Ok(response.attributes)
}
@@ -179,7 +122,7 @@ impl AccountsClient {
.create_srp_session(&srp_attrs.srp_user_id, &a_pub)
.await?;
- self.sleep(Duration::from_millis(100)).await;
+ futures_timer::Delay::new(Duration::from_millis(100)).await;
let server_b = STANDARD.decode(&session.srp_b)?;
let proof = srp_session.compute_m1(&server_b)?;
@@ -206,7 +149,7 @@ impl AccountsClient {
srp_user_id: srp_user_id.to_string(),
srp_a: STANDARD.encode(client_public),
};
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.post("/users/srp/create-session")
.json(&request)
@@ -217,7 +160,7 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
/// Verify an SRP session.
@@ -232,18 +175,16 @@ impl AccountsClient {
session_id: session_id.to_string(),
srp_m1: STANDARD.encode(client_proof),
};
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/srp/verify-session")
- .json(&request)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await
+ Ok(self
+ .api
+ .post("/users/srp/verify-session")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Send an OTP/OTT.
@@ -252,7 +193,7 @@ impl AccountsClient {
email: email.to_string(),
purpose: purpose.to_string(),
};
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.post("/users/ott")
.json(&request)
@@ -262,7 +203,7 @@ impl AccountsClient {
.await?;
Ok(())
})
- .await
+ .await?)
}
/// Verify email ownership with an OTT.
@@ -277,24 +218,22 @@ impl AccountsClient {
ott: ott.to_string(),
source: source.map(str::to_string),
};
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/verify-email")
- .json(&request)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await
+ Ok(self
+ .api
+ .post("/users/verify-email")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Upload user key attributes.
pub async fn set_user_key_attributes(&self, key_attributes: KeyAttributes) -> Result<()> {
let request = SetUserAttributesRequest { key_attributes };
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.put("/users/attributes")
.json(&request)
@@ -304,12 +243,12 @@ impl AccountsClient {
.await?;
Ok(())
})
- .await
+ .await?)
}
/// Upload recovery-key attributes.
pub async fn set_recovery_key_attributes(&self, request: SetRecoveryKeyRequest) -> Result<()> {
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.put("/users/recovery-key")
.json(&request)
@@ -319,12 +258,12 @@ impl AccountsClient {
.await?;
Ok(())
})
- .await
+ .await?)
}
/// Start SRP setup for an authenticated user.
pub async fn setup_srp(&self, request: &SetupSrpRequest) -> Result<SetupSrpResponse> {
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.post("/users/srp/setup")
.json(request)
@@ -335,7 +274,7 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
/// Complete SRP setup.
@@ -348,18 +287,16 @@ impl AccountsClient {
setup_id: setup_id.to_string(),
srp_m1: srp_m1.to_string(),
};
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/srp/complete")
- .json(&request)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await
+ Ok(self
+ .api
+ .post("/users/srp/complete")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Update SRP and key attributes after a password change.
@@ -367,23 +304,21 @@ impl AccountsClient {
&self,
request: &UpdateSrpAndKeysRequest,
) -> Result<UpdateSrpAndKeysResponse> {
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/srp/update")
- .json(request)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await
+ Ok(self
+ .api
+ .post("/users/srp/update")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Get session validity and optional remote key attributes.
pub async fn get_session_validity(&self) -> Result<SessionValidityResponse> {
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.get("/users/session-validity/v2")
.send()
@@ -393,29 +328,26 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
/// Change the authenticated user's email.
pub async fn change_email(&self, email: &str, ott: &str) -> Result<()> {
let body = serde_json::json!({ "email": email, "ott": ott });
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/change-email")
- .json(&body)
- .send()
- .await?
- .error_for_code()
- .await?;
- Ok(())
- })
- .await
+ self.api
+ .post("/users/change-email")
+ .json(&body)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
}
/// Logout the current authenticated session.
pub async fn logout(&self) -> Result<()> {
let body = serde_json::json!({});
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.post("/users/logout")
.json(&body)
@@ -425,33 +357,14 @@ impl AccountsClient {
.await?;
Ok(())
})
- .await
+ .await?)
}
/// Return whether two-factor is enabled.
pub async fn get_two_factor_status(&self) -> Result<bool> {
- let response: TwoFactorStatusResponse = self
- .execute(RetryPolicy::Default, || async {
- self.api
- .get("/users/two-factor/status")
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await?;
- Ok(response.status)
- }
-
- /// Start TOTP setup.
- pub async fn setup_two_factor(&self) -> Result<TwoFactorSecret> {
- let body = serde_json::json!({});
- self.execute(RetryPolicy::NoRetry, || async {
+ let response: TwoFactorStatusResponse = http::retry(RetryProfile::Interactive, || async {
self.api
- .post("/users/two-factor/setup")
- .json(&body)
+ .get("/users/two-factor/status")
.send()
.await?
.error_for_code()
@@ -459,28 +372,41 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?;
+ Ok(response.status)
+ }
+
+ /// Start TOTP setup.
+ pub async fn setup_two_factor(&self) -> Result<TwoFactorSecret> {
+ let body = serde_json::json!({});
+ Ok(self
+ .api
+ .post("/users/two-factor/setup")
+ .json(&body)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Enable TOTP two-factor with encrypted recovery material.
pub async fn enable_two_factor(&self, request: &EnableTwoFactorRequest) -> Result<()> {
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/two-factor/enable")
- .json(request)
- .send()
- .await?
- .error_for_code()
- .await?;
- Ok(())
- })
- .await
+ self.api
+ .post("/users/two-factor/enable")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?;
+ Ok(())
}
/// Disable TOTP two-factor.
pub async fn disable_two_factor(&self) -> Result<()> {
let body = serde_json::json!({});
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.post("/users/two-factor/disable")
.json(&body)
@@ -490,7 +416,7 @@ impl AccountsClient {
.await?;
Ok(())
})
- .await
+ .await?)
}
/// Verify a TOTP code during login.
@@ -499,18 +425,16 @@ impl AccountsClient {
session_id: session_id.to_string(),
code: code.to_string(),
};
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/two-factor/verify")
- .json(&request)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await
+ Ok(self
+ .api
+ .post("/users/two-factor/verify")
+ .json(&request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Fetch 2FA recovery information.
@@ -529,7 +453,7 @@ impl AccountsClient {
},
),
];
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.get("/users/two-factor/recover")
.query(&query)
@@ -540,7 +464,7 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
/// Remove 2FA using the decrypted recovery secret.
@@ -548,23 +472,21 @@ impl AccountsClient {
&self,
request: &RemoveTwoFactorRequest,
) -> Result<TwoFactorAuthorizationResponse> {
- self.execute(RetryPolicy::NoRetry, || async {
- self.api
- .post("/users/two-factor/remove")
- .json(request)
- .send()
- .await?
- .error_for_code()
- .await?
- .json()
- .await
- })
- .await
+ Ok(self
+ .api
+ .post("/users/two-factor/remove")
+ .json(request)
+ .send()
+ .await?
+ .error_for_code()
+ .await?
+ .json()
+ .await?)
}
/// Get passkey recovery status.
pub async fn get_two_factor_recovery_status(&self) -> Result<TwoFactorRecoveryStatusResponse> {
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.get("/users/two-factor/recovery-status")
.send()
@@ -574,7 +496,7 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
/// Configure passkey recovery.
@@ -582,7 +504,7 @@ impl AccountsClient {
&self,
request: &ConfigurePasskeyRecoveryRequest,
) -> Result<()> {
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.post("/users/two-factor/passkeys/configure-recovery")
.json(request)
@@ -592,13 +514,13 @@ impl AccountsClient {
.await?;
Ok(())
})
- .await
+ .await?)
}
/// Poll passkey verification completion.
pub async fn check_passkey_status(&self, session_id: &str) -> Result<AuthResponse> {
let query = [("sessionID", session_id.to_string())];
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.get("/users/two-factor/passkeys/get-token")
.query(&query)
@@ -609,12 +531,12 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
/// Fetch accounts-app broker token and URL.
pub async fn get_accounts_token(&self) -> Result<AccountsTokenResponse> {
- self.execute(RetryPolicy::Default, || async {
+ Ok(http::retry(RetryProfile::Interactive, || async {
self.api
.get("/users/accounts-token")
.send()
@@ -624,7 +546,7 @@ impl AccountsClient {
.json()
.await
})
- .await
+ .await?)
}
}
@@ -633,10 +555,6 @@ mod tests {
use super::*;
use crate::types::AccountsClientConfig;
use mockito::Server;
- use std::sync::{
- Arc,
- atomic::{AtomicUsize, Ordering},
- };
fn make_client(base_url: String) -> AccountsClient {
AccountsClient::new(
@@ -671,47 +589,6 @@ mod tests {
second.assert_async().await;
}
- #[tokio::test]
- async fn send_otp_uses_configured_sleep_fn_for_retry() {
- let mut server = Server::new_async().await;
- let first = server
- .mock("POST", "/users/ott")
- .with_status(500)
- .with_body("temporary failure")
- .expect(1)
- .create_async()
- .await;
- let second = server
- .mock("POST", "/users/ott")
- .with_status(200)
- .expect(1)
- .create_async()
- .await;
-
- let sleep_calls = Arc::new(AtomicUsize::new(0));
- let client = AccountsClient::new(
- AccountsClientConfig::new("io.ente.photos")
- .with_base_url(server.url())
- .with_user_agent("ente-accounts-test")
- .with_sleep_fn({
- let sleep_calls = Arc::clone(&sleep_calls);
- move |_| {
- let sleep_calls = Arc::clone(&sleep_calls);
- async move {
- sleep_calls.fetch_add(1, Ordering::SeqCst);
- }
- }
- }),
- )
- .unwrap();
-
- client.send_otp("[email protected]", "login").await.unwrap();
-
- assert_eq!(sleep_calls.load(Ordering::SeqCst), 1);
- first.assert_async().await;
- second.assert_async().await;
- }
-
#[tokio::test]
async fn verify_srp_session_does_not_retry_on_server_error() {
let mut server = Server::new_async().await;
diff --git a/rust/crates/accounts/src/types.rs b/rust/crates/accounts/src/types.rs
index fced94bdc5..ef2607d927 100644
--- a/rust/crates/accounts/src/types.rs
+++ b/rust/crates/accounts/src/types.rs
@@ -1,28 +1,12 @@
//! Shared reusable types for account clients.
use ente_core::urls::PRODUCTION_API_BASE_URL;
-use futures_timer::Delay;
use serde::{Deserialize, Serialize};
-use std::{future::Future, pin::Pin, sync::Arc, time::Duration};
use zeroize::Zeroize;
/// Default base URL for Ente's public API.
pub const DEFAULT_API_BASE_URL: &str = PRODUCTION_API_BASE_URL;
-/// Boxed future returned by a configured sleep hook.
-pub type SleepFuture = Pin<Box<dyn Future<Output = ()> + 'static>>;
-
-/// Async sleep hook used by high-level account flows for retry/backoff waits.
-pub type SleepFn = Arc<dyn Fn(Duration) -> SleepFuture + 'static>;
-
-fn default_sleep_fn() -> SleepFn {
- Arc::new(|duration| {
- Box::pin(async move {
- Delay::new(duration).await;
- })
- })
-}
-
/// Configuration for constructing an [`crate::client::AccountsClient`].
#[derive(Clone)]
pub struct AccountsClientConfig {
@@ -36,7 +20,6 @@ pub struct AccountsClientConfig {
pub client_version: Option<String>,
/// Optional user agent.
pub user_agent: Option<String>,
- sleep_fn: SleepFn,
}
impl std::fmt::Debug for AccountsClientConfig {
@@ -50,7 +33,6 @@ impl std::fmt::Debug for AccountsClientConfig {
.field("client_package", &self.client_package)
.field("client_version", &self.client_version)
.field("user_agent", &self.user_agent)
- .field("sleep_fn", &"<configured>")
.finish()
}
}
@@ -64,7 +46,6 @@ impl AccountsClientConfig {
client_package: client_package.into(),
client_version: None,
user_agent: None,
- sleep_fn: default_sleep_fn(),
}
}
@@ -91,20 +72,6 @@ impl AccountsClientConfig {
self.user_agent = Some(user_agent.into());
self
}
-
- /// Override the async sleep implementation used for retry/backoff waits.
- pub fn with_sleep_fn<F, Fut>(mut self, sleep_fn: F) -> Self
- where
- F: Fn(Duration) -> Fut + 'static,
- Fut: Future<Output = ()> + 'static,
- {
- self.sleep_fn = Arc::new(move |duration| Box::pin(sleep_fn(duration)));
- self
- }
-
- pub(crate) fn sleep_fn(&self) -> SleepFn {
- Arc::clone(&self.sleep_fn)
- }
}
/// Decrypted account secrets.
diff --git a/rust/crates/core/Cargo.toml b/rust/crates/core/Cargo.toml
index 55282af872..bf3affcbfc 100644
--- a/rust/crates/core/Cargo.toml
+++ b/rust/crates/core/Cargo.toml
@@ -37,12 +37,17 @@ base64 = "0.22"
hex = "0.4"
bip39 = { version = "2", default-features = false, features = ["std"] }
zeroize = { version = "1.8.2", features = ["derive"] }
+futures-timer = "3.0.3"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = { version = "0.12", default-features = false, features = ["stream"] }
bytes = "1.11.1"
futures-core = "0.3.32"
+# futures-timer's `Delay` needs the browser's setTimeout on wasm.
+[target.'cfg(target_arch = "wasm32")'.dependencies]
+futures-timer = { version = "3.0.3", features = ["wasm-bindgen"] }
+
[dev-dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
mockito = "1.6"
diff --git a/rust/crates/core/src/http.rs b/rust/crates/core/src/http.rs
index ed6641bf67..525068fad8 100644
--- a/rust/crates/core/src/http.rs
+++ b/rust/crates/core/src/http.rs
@@ -9,8 +9,8 @@
//!
//! The interface mirrors reqwest, so if you know reqwest you will feel at home.
+use std::future::Future;
use std::sync::{PoisonError, RwLock};
-#[cfg(not(target_arch = "wasm32"))]
use std::time::Duration;
use reqwest::header::{HeaderName, HeaderValue};
@@ -508,6 +508,62 @@ pub struct PingResponse {
pub id: String,
}
+/// How persistently to retry a failed request.
+///
+/// Both profiles make one original attempt and up to three retries. The
+/// delays match the web client's retry schedules.
+#[derive(Clone, Copy, Debug)]
+pub enum RetryProfile {
+ /// For requests a user is waiting on. Retries after 2, 5, and 10 seconds.
+ Interactive,
+ /// For unattended work, where waiting longer handles remote hiccups
+ /// better. Retries after 10, 30, and 120 seconds.
+ Background,
+}
+
+impl RetryProfile {
+ fn delays(self) -> [Duration; 3] {
+ match self {
+ RetryProfile::Interactive => [2, 5, 10].map(Duration::from_secs),
+ RetryProfile::Background => [10, 30, 120].map(Duration::from_secs),
+ }
+ }
+}
+
+/// Run `operation`, retrying failures per `profile`.
+///
+/// A failure is retried when [`Error::is_retryable`] says so: the request
+/// failed in transit, or the server answered with a 429 or a 5xx status.
+/// Any other error returns immediately.
+///
+/// Each retry runs the whole closure again, so wrap only operations that
+/// are safe to repeat.
+pub async fn retry<T, F, Fut>(profile: RetryProfile, operation: F) -> Result<T, Error>
+where
+ F: FnMut() -> Fut,
+ Fut: Future<Output = Result<T, Error>>,
+{
+ retry_with_delays(&profile.delays(), operation).await
+}
+
+async fn retry_with_delays<T, F, Fut>(delays: &[Duration], mut operation: F) -> Result<T, Error>
+where
+ F: FnMut() -> Fut,
+ Fut: Future<Output = Result<T, Error>>,
+{
+ let mut delays = delays.iter();
+ loop {
+ match operation().await {
+ Ok(value) => return Ok(value),
+ Err(error) if error.is_retryable() => match delays.next() {
+ Some(delay) => futures_timer::Delay::new(*delay).await,
+ None => return Err(error),
+ },
+ Err(error) => return Err(error),
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use futures_core::Stream;
@@ -864,4 +920,74 @@ mod tests {
mock.assert_async().await;
}
+
+ #[tokio::test]
+ async fn retry_retries_retryable_failures() {
+ let mut server = Server::new_async().await;
+ let failure = server
+ .mock("GET", "/x")
+ .with_status(500)
+ .expect(1)
+ .create_async()
+ .await;
+ let success = server
+ .mock("GET", "/x")
+ .with_body("ok")
+ .expect(1)
+ .create_async()
+ .await;
+
+ let api = api(&server, None);
+ let body = retry_with_delays(&[Duration::ZERO; 3], || async {
+ api.get("/x").send().await?.error_for_status()?.text().await
+ })
+ .await
+ .unwrap();
+
+ assert_eq!(body, "ok");
+ failure.assert_async().await;
+ success.assert_async().await;
+ }
+
+ #[tokio::test]
+ async fn retry_returns_non_retryable_failures_immediately() {
+ let mut server = Server::new_async().await;
+ let mock = server
+ .mock("GET", "/x")
+ .with_status(404)
+ .expect(1)
+ .create_async()
+ .await;
+
+ let api = api(&server, None);
+ let err = retry_with_delays(&[Duration::ZERO; 3], || async {
+ api.get("/x").send().await?.error_for_status()?.text().await
+ })
+ .await
+ .unwrap_err();
+
+ assert_eq!(err.status_code(), Some(404));
+ mock.assert_async().await;
+ }
+
+ #[tokio::test]
+ async fn retry_gives_up_once_the_delays_are_exhausted() {
+ let mut server = Server::new_async().await;
+ let mock = server
+ .mock("GET", "/x")
+ .with_status(500)
+ .expect(4)
+ .create_async()
+ .await;
+
+ let api = api(&server, None);
+ let err = retry_with_delays(&[Duration::ZERO; 3], || async {
+ api.get("/x").send().await?.error_for_status()?.text().await
+ })
+ .await
+ .unwrap_err();
+
+ assert_eq!(err.status_code(), Some(500));
+ mock.assert_async().await;
+ }
}