don't cache nulls

This commit is contained in:
Bryan Stitt 2023-07-31 14:26:07 -07:00
parent 2885bc6ef2
commit 7cd91af3a8
6 changed files with 37 additions and 4 deletions

2
Cargo.lock generated

@ -7219,7 +7219,7 @@ dependencies = [
[[package]]
name = "web3_proxy"
version = "1.42.0"
version = "1.42.1"
dependencies = [
"anyhow",
"arc-swap",

@ -1,6 +1,6 @@
[package]
name = "web3_proxy"
version = "1.42.0"
version = "1.42.1"
edition = "2021"
default-run = "web3_proxy_cli"

@ -1731,10 +1731,16 @@ impl Web3ProxyApp {
// 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<Arc<RawValue>> = response_data.try_into()?;
// TODO: response data should maybe be Arc<JsonRpcResponseEnum<Box<RawValue>>>, 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 {
// TODO: response data should maybe be Arc<JsonRpcResponseEnum<Box<RawValue>>>, but that's more work
Ok(response_data)
}
}
}).await?
} else {

@ -124,6 +124,8 @@ pub enum Web3ProxyError {
#[from(ignore)]
NotImplemented(Cow<'static, str>),
NoVolatileRedisDatabase,
/// make it easy to skip caching null results
NullJsonRpcResult,
OriginRequired,
#[error(ignore)]
#[from(ignore)]
@ -759,6 +761,9 @@ impl Web3ProxyError {
},
)
}
Self::NullJsonRpcResult => {
return (StatusCode::OK, JsonRpcResponseEnum::NullResult);
}
Self::OriginRequired => {
trace!("OriginRequired");
(

@ -366,6 +366,10 @@ impl JsonRpcForwardedResponse {
pub fn from_response_data(data: JsonRpcResponseEnum<Arc<RawValue>>, id: Box<RawValue>) -> Self {
match data {
JsonRpcResponseEnum::NullResult => {
let x: Box<RawValue> = Default::default();
Self::from_raw_response(x.into(), id)
}
JsonRpcResponseEnum::Result { value, .. } => Self::from_raw_response(value, id),
JsonRpcResponseEnum::RpcError {
error_data: value, ..

@ -89,6 +89,7 @@ pub type JsonRpcResponseCache = Cache<u64, JsonRpcResponseEnum<Arc<RawValue>>>;
/// TODO: we might need one that holds RawValue and one that holds serde_json::Value
#[derive(Clone, Debug)]
pub enum JsonRpcResponseEnum<R> {
NullResult,
Result {
value: R,
num_bytes: u32,
@ -103,12 +104,29 @@ pub enum JsonRpcResponseEnum<R> {
impl<R> JsonRpcResponseEnum<R> {
pub fn num_bytes(&self) -> u32 {
match self {
Self::NullResult => 1,
Self::Result { num_bytes, .. } => *num_bytes,
Self::RpcError { num_bytes, .. } => *num_bytes,
}
}
}
impl<R> JsonRpcResponseEnum<Option<R>> {
pub fn is_null(&self) -> bool {
matches!(self, Self::NullResult | Self::Result { value: None, .. })
}
}
impl JsonRpcResponseEnum<Arc<RawValue>> {
pub fn is_null(&self) -> bool {
match self {
Self::NullResult => true,
Self::Result { value, .. } => value.get() == "null",
_ => false,
}
}
}
impl From<serde_json::Value> for JsonRpcResponseEnum<Arc<RawValue>> {
fn from(value: serde_json::Value) -> Self {
let value = RawValue::from_string(value.to_string()).unwrap();