4 个版本
0.1.3 | 2022 年 8 月 1 日 |
---|---|
0.1.2 | 2022 年 8 月 1 日 |
0.1.1 | 2022 年 8 月 1 日 |
0.1.0 | 2022 年 8 月 1 日 |
#6 在 #retrying
9KB
110 行
一个用于重试的库,专门支持 Rust 的 Azure SDK (参见 https://github.com/Azure/azure-sdk-for-rust)。
你可以提供一个可选的 Settings
参数来控制最大尝试次数、初始等待时间、退避和可选的随机生成器。
示例用法
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(())
}
依赖项
~6–13MB
~169K SLoC