better handling when rate limited

This commit is contained in:
Bryan Stitt 2023-01-25 14:24:38 -08:00
parent f80390c88a
commit 43b5652ba8
2 changed files with 34 additions and 13 deletions

@ -201,22 +201,37 @@ async fn check_rpc(
"params": [block_hash, false],
});
// TODO: don't unwrap! don't use the try operator
let response: JsonRpcResponse<Block<TxHash>> = client
let response = client
.post(rpc.clone())
.json(&block_by_hash_request)
.send()
.await
.context(format!("awaiting response from {}", rpc))?
.json()
.await
.context(format!("reading json on {}", rpc))?;
.context(format!("awaiting response from {}", rpc))?;
if let Some(result) = response.result {
if !response.status().is_success() {
return Err(anyhow::anyhow!(
"bad response from {}: {}",
rpc,
response.status(),
));
}
let body = response
.text()
.await
.context(format!("failed parsing body from {}", rpc))?;
let response_json: JsonRpcResponse<Block<TxHash>> = serde_json::from_str(&body)
.context(format!("body: {}", body))
.context(format!("failed parsing json from {}", rpc))?;
if let Some(result) = response_json.result {
let abbreviated = AbbreviatedBlock::from(result);
debug!("{} has {:?}@{}", rpc, abbreviated.hash, abbreviated.num);
Ok(abbreviated)
} else if let Some(result) = response.error {
} else if let Some(result) = response_json.error {
Err(anyhow!(
"jsonrpc error during check_rpc from {}: {:#}",
rpc,
@ -226,7 +241,7 @@ async fn check_rpc(
Err(anyhow!(
"empty result during check_rpc from {}: {:#}",
rpc,
json!(response)
json!(response_json)
))
}
}

@ -97,7 +97,7 @@ impl SentrydSubCommand {
.or_else(|| top_config.map(|x| x.app.chain_id))
.context("--config or --chain-id required")?;
let web3_proxy = self.web3_proxy.trim_end_matches("/").to_string();
let primary_proxy = self.web3_proxy.trim_end_matches("/").to_string();
let other_proxy: Vec<_> = self
.other_proxy
@ -166,7 +166,7 @@ impl SentrydSubCommand {
// check the main rpc's /health endpoint
{
let url = format!("{}/health", web3_proxy);
let url = format!("{}/health", primary_proxy);
let error_sender = error_sender.clone();
// TODO: what timeout?
@ -212,7 +212,7 @@ impl SentrydSubCommand {
{
let max_age = self.max_age;
let max_lag = self.max_lag;
let rpc = self.web3_proxy.clone();
let primary_proxy = primary_proxy.clone();
let error_sender = error_sender.clone();
let mut others = other_proxy.clone();
@ -225,7 +225,13 @@ impl SentrydSubCommand {
log::Level::Error,
error_sender,
move |error_builder| {
compare::main(error_builder, rpc.clone(), others.clone(), max_age, max_lag)
compare::main(
error_builder,
primary_proxy.clone(),
others.clone(),
max_age,
max_lag,
)
},
);