A library for retrying, with specific support for the Azure SDK for Rust (cf. https://github.com/Azure/azure-sdk-for-rust).
You can supply an option Settings
parameter for controlling the maximum number of attempts, initial wait time, backoff and an optional rand generator.
``` use azuredatacosmos::prelude::; use rand::prelude::; use retry_async::{retry, Error as RetryError, Settings as RetrySettings}; use std::{error::Error, time::Duration}; mod device; mod user; use user::User;
const COSMOSACCOUNT: &str = "XXX"; const COSMOSMASTERKEY: &str = "XXX"; const DATABASENAME: &str = "XXX"; const USER_COLLECTION: &str = "users";
enum CustomError { NotFound, Other, }
async fn main() -> Result<(), Box
let mut rng = rand::thread_rng();
let mut s = RetrySettings {
attempts: 5,
initial_delay: Duration::from_millis(100),
backoff: 2.0,
rng: None, //Some(&mut rng),
};
// Get document.
match retry(
|| async {
println!("ALPHA");
// return Err(RetryError::CustomTransient(CustomError::Other));
let response: GetDocumentResponse<User> = collection
.document_client::<String, String>(
"2ea7e0af-5864-4947-b13e-a786920864cb".to_string(),
&"2ea7e0af-5864-4947-b13e-a786920864cb".to_string(),
)?
.get_document()
.into_future()
.await?;
match response {
GetDocumentResponse::Found(response) => Ok(response.document.document),
GetDocumentResponse::NotFound(_) => {
Err(RetryError::CustomPermanent(CustomError::NotFound))
}
}
},
None, // Some(&mut s),
)
.await
{
Ok(user) => {
println!("BRAVO {}", user.id);
}
Err(err) => {
println!("CHARLIE {:?}", err);
}
};
if let Some(rng) = s.rng {
let y: f64 = rng.gen();
println!("ECHO {:?}", y);
}
let y: f64 = rng.gen();
println!("DELTA {:?}", y);
Ok(())
} ```