From 43b5652ba8efaf560fc4d111c3cb5658160b611c Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 25 Jan 2023 14:24:38 -0800 Subject: [PATCH] better handling when rate limited --- .../src/bin/web3_proxy_cli/sentryd/compare.rs | 33 ++++++++++++++----- .../src/bin/web3_proxy_cli/sentryd/mod.rs | 14 +++++--- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/web3_proxy/src/bin/web3_proxy_cli/sentryd/compare.rs b/web3_proxy/src/bin/web3_proxy_cli/sentryd/compare.rs index 5333c738..3e6cb89f 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/sentryd/compare.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/sentryd/compare.rs @@ -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> = 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> = 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) )) } } diff --git a/web3_proxy/src/bin/web3_proxy_cli/sentryd/mod.rs b/web3_proxy/src/bin/web3_proxy_cli/sentryd/mod.rs index 3f32e68b..b6a6844d 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/sentryd/mod.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/sentryd/mod.rs @@ -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, + ) }, );