Skip to content

Commit

Permalink
Add retry logic to identity client (#292)
Browse files Browse the repository at this point in the history
This adds retry logic so that edged will retry some requests. A follow-up pr for the iotedge repo will make edged use the new functionality.
  • Loading branch information
lfitchett authored Oct 5, 2021
1 parent a1087ff commit 9a12cd3
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 138 deletions.
1 change: 1 addition & 0 deletions aziotctl/src/internal/check/checks/read_certs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl ReadCerts {
let cert_client = aziot_cert_client_async::Client::new(
aziot_cert_common_http::ApiVersion::V2020_09_01,
aziot_certd.clone(),
0,
);

let mut err_aggregated = String::new();
Expand Down
1 change: 1 addition & 0 deletions aziotctl/src/internal/check/checks/read_key_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl ReadKeyPairs {
let key_client = aziot_key_client_async::Client::new(
aziot_key_common_http::ApiVersion::V2021_05_01,
aziot_keyd.clone(),
0,
);

let mut key_engine = {
Expand Down
1 change: 1 addition & 0 deletions aziotctl/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async fn reprovision(uri: &url::Url) -> Result<()> {
let client = aziot_identity_client_async::Client::new(
aziot_identity_common_http::ApiVersion::V2020_09_01,
connector,
0,
);

match client.reprovision().await {
Expand Down
45 changes: 28 additions & 17 deletions cert/aziot-cert-client-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
pub struct Client {
api_version: aziot_cert_common_http::ApiVersion,
inner: hyper::Client<http_common::Connector, hyper::Body>,
max_retries: u32,
}

impl Client {
pub fn new(
api_version: aziot_cert_common_http::ApiVersion,
connector: http_common::Connector,
max_retries: u32,
) -> Self {
let inner = connector.into_client();
Client { api_version, inner }
Client {
api_version,
inner,
max_retries,
}
}

pub async fn create_cert(
Expand All @@ -40,14 +46,15 @@ impl Client {
}),
};

let res: aziot_cert_common_http::get_cert::Response = http_common::request(
let res: aziot_cert_common_http::get_cert::Response = http_common::request_with_retry(
&self.inner,
http::Method::POST,
&format!(
"http://certd.sock/certificates?api-version={}",
self.api_version
),
Some(&body),
self.max_retries,
)
.await?;
Ok(res.pem.0)
Expand All @@ -58,7 +65,7 @@ impl Client {
pem: aziot_cert_common_http::Pem(pem.to_owned()),
};

let res: aziot_cert_common_http::import_cert::Response = http_common::request(
let res: aziot_cert_common_http::import_cert::Response = http_common::request_with_retry(
&self.inner,
http::Method::PUT,
&format!(
Expand All @@ -70,31 +77,34 @@ impl Client {
self.api_version,
),
Some(&body),
self.max_retries,
)
.await?;
Ok(res.pem.0)
}

pub async fn get_cert(&self, id: &str) -> Result<Vec<u8>, std::io::Error> {
let res: aziot_cert_common_http::get_cert::Response = http_common::request::<(), _>(
&self.inner,
http::Method::GET,
&format!(
"http://certd.sock/certificates/{}?api-version={}",
percent_encoding::percent_encode(
id.as_bytes(),
http_common::PATH_SEGMENT_ENCODE_SET
let res: aziot_cert_common_http::get_cert::Response =
http_common::request_with_retry::<(), _>(
&self.inner,
http::Method::GET,
&format!(
"http://certd.sock/certificates/{}?api-version={}",
percent_encoding::percent_encode(
id.as_bytes(),
http_common::PATH_SEGMENT_ENCODE_SET
),
self.api_version,
),
self.api_version,
),
None,
)
.await?;
None,
self.max_retries,
)
.await?;
Ok(res.pem.0)
}

pub async fn delete_cert(&self, id: &str) -> Result<(), std::io::Error> {
let () = http_common::request_no_content::<()>(
let () = http_common::request_no_content_with_retry::<()>(
&self.inner,
http::Method::DELETE,
&format!(
Expand All @@ -106,6 +116,7 @@ impl Client {
self.api_version,
),
None,
self.max_retries,
)
.await?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion http-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub use proxy::{get_proxy_uri, MaybeProxyConnector};
mod request;
#[cfg(feature = "tokio1")]
pub use request::{
request, request_no_content, request_with_headers, request_with_headers_no_content,
request, request_no_content, request_no_content_with_retry, request_with_headers,
request_with_headers_no_content, request_with_retry,
};

pub mod server;
Expand Down
Loading

0 comments on commit 9a12cd3

Please sign in to comment.