Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry iot_config client rpcs #607

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions iot_config/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ pub struct Client {
batch_size: u32,
}

macro_rules! call_with_retry {
($rpc:expr) => {{
use tonic::Code;

let mut attempt = 1;
loop {
let response = $rpc.await;
match response {
Ok(resp) => break Ok(resp),
Err(status) => match status.code() {
Code::Cancelled | Code::DeadlineExceeded | Code::Unavailable => {
if attempt < 3 {
attempt += 1;
tokio::time::sleep(Duration::from_millis(attempt * 100)).await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delay between attempts is very short. Doesnt give much time for any transitory issue to resolve

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andymck What do you think it should be?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 secs ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We default the rpc timeout in the client endpoint builder to 5 sec already so it should probably be less then that since we’re making three calls but I can skip the increasing back off and just retry 3 times at the default. That reminds me though we’d talked about modifying the request to set the timeout header which is going to shoot this whole thing down

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we start with 1 second, so then do 1,2,3??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works for me

continue;
} else {
break Err(status);
}
}
_ => break Err(status),
},
}
}
}};
}

pub(crate) use call_with_retry;

impl Client {
pub fn from_settings(settings: &Settings) -> Result<Self, Box<helium_crypto::Error>> {
let channel = Endpoint::from(settings.url.clone())
Expand All @@ -60,7 +88,8 @@ impl Client {
signature: vec![],
};
request.signature = self.signing_key.sign(&request.encode_to_vec())?;
let response = self.admin_client.region_params(request).await?.into_inner();
let response =
call_with_retry!(self.admin_client.region_params(request.clone()))?.into_inner();
response.verify(&self.config_pubkey)?;
Ok(RegionParamsInfo {
region: response.region(),
Expand All @@ -87,7 +116,7 @@ impl gateway_info::GatewayInfoResolver for Client {
};
request.signature = self.signing_key.sign(&request.encode_to_vec())?;
tracing::debug!(pubkey = address.to_string(), "fetching gateway info");
let response = match self.gateway_client.info(request).await {
let response = match call_with_retry!(self.gateway_client.info(request.clone())) {
Ok(info_resp) => {
let response = info_resp.into_inner();
response.verify(&self.config_pubkey)?;
Expand All @@ -110,10 +139,7 @@ impl gateway_info::GatewayInfoResolver for Client {
request.signature = self.signing_key.sign(&request.encode_to_vec())?;
tracing::debug!("fetching gateway info stream");
let pubkey = Arc::new(self.config_pubkey.clone());
let response_stream = self
.gateway_client
.info_stream(request)
.await?
let response_stream = call_with_retry!(self.gateway_client.info_stream(request.clone()))?
.into_inner()
.filter_map(|resp| async move { resp.ok() })
.map(move |resp| (resp, pubkey.clone()))
Expand Down
12 changes: 6 additions & 6 deletions iot_config/src/client/org_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
iot_config, Arc, Channel, ClientError, Duration, Endpoint, Keypair, Message, MsgVerify,
PublicKey, Settings, Sign,
call_with_retry, iot_config, Arc, Channel, ClientError, Duration, Endpoint, Keypair, Message,
MsgVerify, PublicKey, Settings, Sign,
};
use chrono::Utc;
use file_store::traits::TimestampEncode;
Expand Down Expand Up @@ -32,15 +32,15 @@ impl OrgClient {
tracing::debug!(%oui, "retrieving org");

let req = OrgGetReqV1 { oui };
let res = self.client.get(req).await?.into_inner();
let res = call_with_retry!(self.client.get(req.clone()))?.into_inner();
res.verify(&self.config_pubkey)?;
Ok(res)
}

pub async fn list(&mut self) -> Result<Vec<OrgV1>, ClientError> {
tracing::debug!("retrieving org list");

let res = self.client.list(OrgListReqV1 {}).await?.into_inner();
let res = call_with_retry!(self.client.list(OrgListReqV1 {}))?.into_inner();
res.verify(&self.config_pubkey)?;
Ok(res.orgs)
}
Expand All @@ -55,7 +55,7 @@ impl OrgClient {
signature: vec![],
};
req.signature = self.signing_key.sign(&req.encode_to_vec())?;
let res = self.client.enable(req).await?.into_inner();
let res = call_with_retry!(self.client.enable(req.clone()))?.into_inner();
res.verify(&self.config_pubkey)?;
Ok(())
}
Expand All @@ -70,7 +70,7 @@ impl OrgClient {
signature: vec![],
};
req.signature = self.signing_key.sign(&req.encode_to_vec())?;
let res = self.client.disable(req).await?.into_inner();
let res = call_with_retry!(self.client.disable(req.clone()))?.into_inner();
res.verify(&self.config_pubkey)?;
Ok(())
}
Expand Down