Crates.io | retry_async |
lib.rs | retry_async |
version | 0.1.3 |
source | src |
created_at | 2022-08-01 18:39:20.268972 |
updated_at | 2022-08-01 19:08:31.776706 |
description | A library for retrying, with specific support for the Azure SDK for Rust (cf. https://github.com/Azure/azure-sdk-for-rust). |
homepage | |
repository | https://github.com/third-act/retry_async/ |
max_upload_size | |
id | 636993 |
size | 8,250 |
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 optional Settings
parameter for controlling the maximum number of attempts, initial wait time, backoff and an optional rand generator.
use azure_data_cosmos::prelude::*;
use rand::prelude::*;
use retry_async::{retry, Error as RetryError, Settings};
use std::{error::Error, time::Duration};
mod device;
mod user;
use user::User;
const COSMOS_ACCOUNT: &str = "XXX";
const COSMOS_MASTER_KEY: &str = "XXX";
const DATABASE_NAME: &str = "XXX";
const USER_COLLECTION: &str = "users";
#[derive(Debug)]
enum CustomError {
NotFound,
Other,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let authorization_token = AuthorizationToken::primary_from_base64(&COSMOS_MASTER_KEY)?;
let client = CosmosClient::new(
COSMOS_ACCOUNT.to_string(),
authorization_token,
CosmosOptions::default(),
);
let database = client.database_client(DATABASE_NAME);
let collection = database.collection_client(USER_COLLECTION);
let mut rng = rand::thread_rng();
let mut s = Settings {
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(())
}