From 8a0c2dfdc1f542985a72bdf3bca896c13a64a372 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 19 Sep 2023 18:37:34 -0700 Subject: [PATCH] try timeout inside the cache get again but with shorter timeouts --- web3_proxy/src/app/mod.rs | 53 ++++++++++++++++++-------------- web3_proxy/src/response_cache.rs | 2 +- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/web3_proxy/src/app/mod.rs b/web3_proxy/src/app/mod.rs index a6405d18..e7fe3ff6 100644 --- a/web3_proxy/src/app/mod.rs +++ b/web3_proxy/src/app/mod.rs @@ -1734,7 +1734,9 @@ impl Web3ProxyApp { }; // TODO: think more about timeouts - let max_wait = Some(Duration::from_secs(299)); + // TODO: different user tiers should have different timeouts + // erigon's timeout is 300, so keep this a few seconds shorter + let max_wait = Some(Duration::from_secs(295)); if let Some(cache_key) = cache_key { let from_block_num = cache_key.from_block_num().copied(); @@ -1746,11 +1748,11 @@ impl Web3ProxyApp { // TODO: try to fetch out of s3 - timeout(Duration::from_secs(300), self + self .jsonrpc_response_cache .try_get_with::<_, Web3ProxyError>(cache_key.hash(), async { // TODO: think more about this timeout. we should probably have a `request_expires_at` Duration on the request_metadata - let response_data = self.balanced_rpcs + let response_data = timeout(Duration::from_secs(290), self.balanced_rpcs .try_proxy_connection::<_, Arc>( method, params, @@ -1758,30 +1760,35 @@ impl Web3ProxyApp { max_wait, from_block_num.as_ref(), to_block_num.as_ref(), - ).await; + )).await; - if !cache_jsonrpc_errors && let Err(err) = response_data { - // if we are not supposed to cache jsonrpc errors, - // then we must not convert Provider errors into a JsonRpcResponseEnum - // return all the errors now. moka will not cache Err results - Err(err) - } else { - // convert jsonrpc errors into JsonRpcResponseEnum, but leave the rest as errors - let response_data: JsonRpcResponseEnum> = response_data.try_into()?; + match response_data { + Ok(response_data) => { + if !cache_jsonrpc_errors && let Err(err) = response_data { + // if we are not supposed to cache jsonrpc errors, + // then we must not convert Provider errors into a JsonRpcResponseEnum + // return all the errors now. moka will not cache Err results + Err(err) + } else { + // convert jsonrpc errors into JsonRpcResponseEnum, but leave the rest as errors + let response_data: JsonRpcResponseEnum> = response_data.try_into()?; - if response_data.is_null() { - // don't ever cache "null" as a success. its too likely to be a problem - Err(Web3ProxyError::NullJsonRpcResult) - } else if response_data.num_bytes() > max_response_cache_bytes { - // don't cache really large requests - // TODO: emit a stat - Err(Web3ProxyError::JsonRpcResponse(response_data)) - } else { - // TODO: response data should maybe be Arc>>, but that's more work - Ok(response_data) + if response_data.is_null() { + // don't ever cache "null" as a success. its too likely to be a problem + Err(Web3ProxyError::NullJsonRpcResult) + } else if response_data.num_bytes() > max_response_cache_bytes { + // don't cache really large requests + // TODO: emit a stat + Err(Web3ProxyError::JsonRpcResponse(response_data)) + } else { + // TODO: response data should maybe be Arc>>, but that's more work + Ok(response_data) + } + } } + Err(err) => Err(Web3ProxyError::from(err)), } - })).await?? + }).await? } else { let x = timeout( Duration::from_secs(300), diff --git a/web3_proxy/src/response_cache.rs b/web3_proxy/src/response_cache.rs index 050aa6fa..2b445b4c 100644 --- a/web3_proxy/src/response_cache.rs +++ b/web3_proxy/src/response_cache.rs @@ -14,7 +14,7 @@ use std::{ #[derive(Clone, Debug, Eq, From)] pub struct JsonRpcQueryCacheKey { - /// hashed inputs + /// hashed params hash: u64, from_block: Option, to_block: Option,