put timeout around the moka stuff

we went a long time without seeing errors on this, but i just saw some again
This commit is contained in:
Bryan Stitt 2023-09-18 23:08:16 -07:00
parent e6c376ae94
commit 5f85a17b19

@ -53,7 +53,7 @@ use std::time::Duration;
use tokio::select; use tokio::select;
use tokio::sync::{broadcast, mpsc, oneshot, watch, Semaphore}; use tokio::sync::{broadcast, mpsc, oneshot, watch, Semaphore};
use tokio::task::{yield_now, JoinHandle}; use tokio::task::{yield_now, JoinHandle};
use tokio::time::{sleep, Instant}; use tokio::time::{sleep, timeout, Instant};
use tracing::{error, info, trace, warn, Level}; use tracing::{error, info, trace, warn, Level};
// TODO: make this customizable? // TODO: make this customizable?
@ -1743,41 +1743,52 @@ impl Web3ProxyApp {
.jsonrpc_response_cache .jsonrpc_response_cache
.try_get_with::<_, Web3ProxyError>(cache_key.hash(), async { .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 // 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 proxy_with_timeout= timeout(
.try_proxy_connection::<_, Arc<RawValue>>( Duration::from_secs(300),
method, self.balanced_rpcs
params, .try_proxy_connection::<_, Arc<RawValue>>(
Some(request_metadata), method,
max_wait, params,
from_block_num.as_ref(), Some(request_metadata),
to_block_num.as_ref(), max_wait,
) from_block_num.as_ref(),
.await; to_block_num.as_ref(),
)
);
if !cache_jsonrpc_errors && let Err(err) = response_data { match proxy_with_timeout.await {
// if we are not supposed to cache jsonrpc errors, Ok(response_data) => {
// then we must not convert Provider errors into a JsonRpcResponseEnum if !cache_jsonrpc_errors && let Err(err) = response_data {
// return all the errors now. moka will not cache Err results // if we are not supposed to cache jsonrpc errors,
Err(err) // then we must not convert Provider errors into a JsonRpcResponseEnum
} else { // return all the errors now. moka will not cache Err results
// convert jsonrpc errors into JsonRpcResponseEnum, but leave the rest as errors Err(err)
let response_data: JsonRpcResponseEnum<Arc<RawValue>> = response_data.try_into()?; } else {
// convert jsonrpc errors into JsonRpcResponseEnum, but leave the rest as errors
let response_data: JsonRpcResponseEnum<Arc<RawValue>> = response_data.try_into()?;
if response_data.is_null() { if response_data.is_null() {
// don't ever cache "null" as a success. its too likely to be a problem // don't ever cache "null" as a success. its too likely to be a problem
Err(Web3ProxyError::NullJsonRpcResult) Err(Web3ProxyError::NullJsonRpcResult)
} else if response_data.num_bytes() > max_response_cache_bytes { } else if response_data.num_bytes() > max_response_cache_bytes {
// don't cache really large requests // don't cache really large requests
// TODO: emit a stat // TODO: emit a stat
Err(Web3ProxyError::JsonRpcResponse(response_data)) Err(Web3ProxyError::JsonRpcResponse(response_data))
} else { } else {
// TODO: response data should maybe be Arc<JsonRpcResponseEnum<Box<RawValue>>>, but that's more work // TODO: response data should maybe be Arc<JsonRpcResponseEnum<Box<RawValue>>>, but that's more work
Ok(response_data) Ok(response_data)
}
}
}
Err(err) => {
Err(Web3ProxyError::from(err))
} }
} }
}).await? }).await?
} else { } else {
let x = self.balanced_rpcs let x = timeout(
Duration::from_secs(300),
self.balanced_rpcs
.try_proxy_connection::<_, Arc<RawValue>>( .try_proxy_connection::<_, Arc<RawValue>>(
method, method,
params, params,
@ -1786,7 +1797,7 @@ impl Web3ProxyApp {
None, None,
None, None,
) )
.await?; ).await??;
x.into() x.into()
} }