2023-09-27 04:18:06 +03:00
|
|
|
use crate::{
|
2023-11-01 05:04:20 +03:00
|
|
|
block_number::{BlockNumOrHash, CacheMode},
|
2023-09-27 04:18:06 +03:00
|
|
|
errors::{Web3ProxyError, Web3ProxyResult},
|
2023-10-03 23:46:27 +03:00
|
|
|
frontend::authorization::RequestOrMethod,
|
2023-10-17 09:56:38 +03:00
|
|
|
jsonrpc::{self, JsonRpcErrorData, ResponsePayload},
|
2023-09-27 04:18:06 +03:00
|
|
|
};
|
2023-05-13 21:13:02 +03:00
|
|
|
use derive_more::From;
|
2023-06-27 08:40:00 +03:00
|
|
|
use ethers::{
|
|
|
|
providers::{HttpClientError, JsonRpcError, ProviderError, WsClientError},
|
2023-07-12 10:35:07 +03:00
|
|
|
types::U64,
|
2023-06-27 08:40:00 +03:00
|
|
|
};
|
2023-05-31 02:32:34 +03:00
|
|
|
use hashbrown::hash_map::DefaultHashBuilder;
|
2023-06-08 03:26:38 +03:00
|
|
|
use moka::future::Cache;
|
2023-10-17 20:03:52 +03:00
|
|
|
use serde_json::value::{to_raw_value, RawValue};
|
2023-05-13 21:13:02 +03:00
|
|
|
use std::{
|
2023-05-31 02:32:34 +03:00
|
|
|
hash::{BuildHasher, Hash, Hasher},
|
2023-06-07 23:57:38 +03:00
|
|
|
sync::Arc,
|
2023-05-13 21:13:02 +03:00
|
|
|
};
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
#[derive(Clone, Debug, Eq, From)]
|
2023-10-03 23:46:27 +03:00
|
|
|
pub struct JsonRpcQueryCacheKey<'a> {
|
2023-10-28 01:41:24 +03:00
|
|
|
/// hashed params and block info so that we don't have to clone a potentially big thing
|
|
|
|
/// this is probably a premature optimization
|
2023-05-31 02:32:34 +03:00
|
|
|
hash: u64,
|
2023-10-28 01:41:24 +03:00
|
|
|
from_block: Option<&'a BlockNumOrHash>,
|
2023-11-01 05:04:20 +03:00
|
|
|
to_block: Option<&'a BlockNumOrHash>,
|
2023-10-03 23:46:27 +03:00
|
|
|
cache_jsonrpc_errors: bool,
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
impl JsonRpcQueryCacheKey<'_> {
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
2023-06-07 23:57:38 +03:00
|
|
|
pub fn hash(&self) -> u64 {
|
|
|
|
self.hash
|
|
|
|
}
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn from_block_num(&self) -> Option<U64> {
|
|
|
|
self.from_block.map(|x| x.num())
|
2023-05-31 02:32:34 +03:00
|
|
|
}
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn to_block_num(&self) -> Option<U64> {
|
|
|
|
self.to_block.map(|x| x.num())
|
2023-05-31 02:32:34 +03:00
|
|
|
}
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
2023-05-31 02:32:34 +03:00
|
|
|
pub fn cache_errors(&self) -> bool {
|
2023-10-03 23:46:27 +03:00
|
|
|
self.cache_jsonrpc_errors
|
2023-05-31 02:32:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
impl PartialEq for JsonRpcQueryCacheKey<'_> {
|
2023-05-31 02:32:34 +03:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.hash.eq(&other.hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
impl Hash for JsonRpcQueryCacheKey<'_> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
2023-05-31 02:32:34 +03:00
|
|
|
// TODO: i feel like this hashes twice. oh well
|
|
|
|
self.hash.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
impl<'a> JsonRpcQueryCacheKey<'a> {
|
|
|
|
pub fn new(cache_mode: &'a CacheMode, request: &'a RequestOrMethod) -> Self {
|
|
|
|
// TODO: do this without clone
|
|
|
|
let from_block = cache_mode.from_block();
|
|
|
|
let to_block = cache_mode.to_block();
|
2023-11-01 05:04:20 +03:00
|
|
|
let cache_block = cache_mode.cache_block();
|
2023-10-03 23:46:27 +03:00
|
|
|
let cache_jsonrpc_errors = cache_mode.cache_jsonrpc_errors();
|
2023-05-31 02:32:34 +03:00
|
|
|
|
|
|
|
let mut hasher = DefaultHashBuilder::default().build_hasher();
|
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
from_block.hash(&mut hasher);
|
|
|
|
to_block.hash(&mut hasher);
|
2023-11-01 05:04:20 +03:00
|
|
|
cache_block.hash(&mut hasher);
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
request.method().hash(&mut hasher);
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
// TODO: make sure preserve_order feature is OFF
|
2023-11-01 05:04:20 +03:00
|
|
|
// TODO: is there a faster way to do this? can we serialize directly into the hasher?
|
2023-10-03 23:46:27 +03:00
|
|
|
request.params().to_string().hash(&mut hasher);
|
2023-05-31 02:32:34 +03:00
|
|
|
|
2023-10-03 23:46:27 +03:00
|
|
|
cache_jsonrpc_errors.hash(&mut hasher);
|
2023-05-31 02:32:34 +03:00
|
|
|
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
hash,
|
2023-06-29 03:42:43 +03:00
|
|
|
from_block,
|
|
|
|
to_block,
|
2023-10-03 23:46:27 +03:00
|
|
|
cache_jsonrpc_errors,
|
2023-05-31 02:32:34 +03:00
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
pub type JsonRpcResponseCache = Cache<u64, ForwardedResponse<Arc<RawValue>>>;
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-10-17 09:56:38 +03:00
|
|
|
/// TODO: think about this more. there is a lot of overlap with ParsedResponse
|
2023-05-30 01:48:22 +03:00
|
|
|
#[derive(Clone, Debug)]
|
2023-10-17 09:56:38 +03:00
|
|
|
pub enum ForwardedResponse<T> {
|
2023-05-13 21:13:02 +03:00
|
|
|
Result {
|
2023-10-17 09:56:38 +03:00
|
|
|
value: T,
|
|
|
|
num_bytes: u64,
|
2023-05-13 21:13:02 +03:00
|
|
|
},
|
2023-05-31 02:32:34 +03:00
|
|
|
RpcError {
|
|
|
|
error_data: JsonRpcErrorData,
|
2023-10-17 09:56:38 +03:00
|
|
|
num_bytes: u64,
|
2023-05-13 21:13:02 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
// TODO: impl for other inner result types?
|
2023-10-11 10:12:20 +03:00
|
|
|
impl<R> ForwardedResponse<R> {
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
2023-10-17 09:56:38 +03:00
|
|
|
pub fn num_bytes(&self) -> u64 {
|
2023-05-13 21:13:02 +03:00
|
|
|
match self {
|
2023-05-31 02:32:34 +03:00
|
|
|
Self::Result { num_bytes, .. } => *num_bytes,
|
|
|
|
Self::RpcError { num_bytes, .. } => *num_bytes,
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-03 01:22:26 +03:00
|
|
|
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
2023-08-03 01:22:26 +03:00
|
|
|
pub fn is_error(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Result { .. } => false,
|
|
|
|
Self::RpcError { .. } => true,
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl<R> ForwardedResponse<Option<R>> {
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
2023-08-01 00:26:07 +03:00
|
|
|
pub fn is_null(&self) -> bool {
|
2023-10-17 10:46:06 +03:00
|
|
|
matches!(self, Self::Result { value: None, .. })
|
2023-08-01 00:26:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl ForwardedResponse<Arc<RawValue>> {
|
2023-10-28 01:41:24 +03:00
|
|
|
#[inline]
|
2023-08-01 00:26:07 +03:00
|
|
|
pub fn is_null(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Result { value, .. } => value.get() == "null",
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-17 09:56:38 +03:00
|
|
|
impl From<ResponsePayload<Arc<RawValue>>> for ForwardedResponse<Arc<RawValue>> {
|
|
|
|
fn from(value: ResponsePayload<Arc<RawValue>>) -> Self {
|
|
|
|
match value {
|
|
|
|
ResponsePayload::Success { result } => {
|
|
|
|
let num_bytes = result.get().len() as u64;
|
|
|
|
|
|
|
|
ForwardedResponse::Result {
|
|
|
|
value: result,
|
|
|
|
num_bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponsePayload::Error { error } => {
|
|
|
|
let num_bytes = error.num_bytes();
|
|
|
|
|
|
|
|
ForwardedResponse::RpcError {
|
|
|
|
error_data: error,
|
|
|
|
num_bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl TryFrom<Web3ProxyResult<jsonrpc::SingleResponse>> for ForwardedResponse<Arc<RawValue>> {
|
2023-09-27 04:18:06 +03:00
|
|
|
type Error = Web3ProxyError;
|
|
|
|
fn try_from(response: Web3ProxyResult<jsonrpc::SingleResponse>) -> Result<Self, Self::Error> {
|
2023-10-17 09:56:38 +03:00
|
|
|
match response? {
|
|
|
|
jsonrpc::SingleResponse::Parsed(parsed) => match parsed.payload {
|
2023-10-11 10:12:20 +03:00
|
|
|
jsonrpc::ResponsePayload::Success { result } => {
|
2023-10-17 09:56:38 +03:00
|
|
|
let num_bytes = result.get().len() as u64;
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
Ok(ForwardedResponse::Result {
|
2023-09-27 04:18:06 +03:00
|
|
|
value: result,
|
|
|
|
num_bytes,
|
|
|
|
})
|
|
|
|
}
|
2023-10-11 10:12:20 +03:00
|
|
|
jsonrpc::ResponsePayload::Error { error } => {
|
2023-10-17 09:56:38 +03:00
|
|
|
let num_bytes = error.num_bytes();
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
Ok(ForwardedResponse::RpcError {
|
2023-09-27 04:18:06 +03:00
|
|
|
error_data: error,
|
|
|
|
num_bytes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2023-10-17 09:56:38 +03:00
|
|
|
jsonrpc::SingleResponse::Stream(stream) => Err(Web3ProxyError::StreamResponse(stream)),
|
2023-09-27 04:18:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl From<serde_json::Value> for ForwardedResponse<Arc<RawValue>> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn from(value: serde_json::Value) -> Self {
|
2023-10-17 20:03:52 +03:00
|
|
|
let value = to_raw_value(&value).unwrap();
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-05-30 01:48:22 +03:00
|
|
|
value.into()
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl From<Arc<RawValue>> for ForwardedResponse<Arc<RawValue>> {
|
2023-06-07 23:57:38 +03:00
|
|
|
fn from(value: Arc<RawValue>) -> Self {
|
2023-10-17 09:56:38 +03:00
|
|
|
let num_bytes = value.get().len() as u64;
|
2023-06-07 23:57:38 +03:00
|
|
|
|
|
|
|
Self::Result { value, num_bytes }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl From<Box<RawValue>> for ForwardedResponse<Arc<RawValue>> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn from(value: Box<RawValue>) -> Self {
|
2023-10-17 09:56:38 +03:00
|
|
|
let value: Arc<RawValue> = value.into();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-10-17 09:56:38 +03:00
|
|
|
value.into()
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl TryFrom<Web3ProxyError> for ForwardedResponse<Arc<RawValue>> {
|
2023-05-31 02:32:34 +03:00
|
|
|
type Error = Web3ProxyError;
|
|
|
|
|
|
|
|
fn try_from(value: Web3ProxyError) -> Result<Self, Self::Error> {
|
2023-08-03 01:22:26 +03:00
|
|
|
match value {
|
2023-10-07 00:51:21 +03:00
|
|
|
Web3ProxyError::EthersProvider(err) => match JsonRpcErrorData::try_from(&err) {
|
|
|
|
Ok(x) => Ok(x.into()),
|
|
|
|
Err(..) => Err(err.into()),
|
|
|
|
},
|
2023-08-03 01:22:26 +03:00
|
|
|
Web3ProxyError::JsonRpcErrorData(err) => Ok(err.into()),
|
|
|
|
err => Err(err),
|
2023-05-31 02:32:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl TryFrom<Result<Arc<RawValue>, Web3ProxyError>> for ForwardedResponse<Arc<RawValue>> {
|
2023-06-07 23:57:38 +03:00
|
|
|
type Error = Web3ProxyError;
|
|
|
|
|
|
|
|
fn try_from(value: Result<Arc<RawValue>, Web3ProxyError>) -> Result<Self, Self::Error> {
|
|
|
|
match value {
|
|
|
|
Ok(x) => Ok(x.into()),
|
|
|
|
Err(err) => {
|
|
|
|
let x: Self = err.try_into()?;
|
|
|
|
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 02:32:34 +03:00
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
impl<R> From<JsonRpcErrorData> for ForwardedResponse<R> {
|
2023-05-13 21:13:02 +03:00
|
|
|
fn from(value: JsonRpcErrorData) -> Self {
|
2023-05-30 01:48:22 +03:00
|
|
|
// TODO: wrap the error in a complete response?
|
2023-10-17 09:56:38 +03:00
|
|
|
let num_bytes = serde_json::to_string(&value).unwrap().len() as u64;
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-05-31 02:32:34 +03:00
|
|
|
Self::RpcError {
|
|
|
|
error_data: value,
|
|
|
|
num_bytes,
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 08:40:00 +03:00
|
|
|
impl<'a> From<&'a JsonRpcError> for JsonRpcErrorData {
|
|
|
|
fn from(value: &'a JsonRpcError) -> Self {
|
|
|
|
Self {
|
|
|
|
code: value.code,
|
|
|
|
message: value.message.clone().into(),
|
|
|
|
data: value.data.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-06-27 08:40:00 +03:00
|
|
|
impl<'a> TryFrom<&'a ProviderError> for JsonRpcErrorData {
|
|
|
|
type Error = &'a ProviderError;
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-06-27 08:40:00 +03:00
|
|
|
fn try_from(e: &'a ProviderError) -> Result<Self, Self::Error> {
|
2023-05-13 21:13:02 +03:00
|
|
|
match e {
|
|
|
|
ProviderError::JsonRpcClientError(err) => {
|
2023-10-07 00:51:21 +03:00
|
|
|
match err.as_error_response() {
|
|
|
|
Some(err) => {
|
|
|
|
// this isn't safe to do because we don't have the id of the request
|
|
|
|
Ok(JsonRpcErrorData {
|
|
|
|
code: err.code,
|
|
|
|
message: err.message.clone().into(),
|
|
|
|
data: err.data.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
None => Err(e),
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-27 08:40:00 +03:00
|
|
|
e => Err(e),
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
2023-06-27 08:40:00 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
|
2023-06-27 08:40:00 +03:00
|
|
|
impl<'a> TryFrom<&'a HttpClientError> for JsonRpcErrorData {
|
|
|
|
type Error = &'a HttpClientError;
|
|
|
|
|
|
|
|
fn try_from(e: &'a HttpClientError) -> Result<Self, Self::Error> {
|
|
|
|
match e {
|
|
|
|
HttpClientError::JsonRpcError(err) => Ok(err.into()),
|
|
|
|
e => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<&'a WsClientError> for JsonRpcErrorData {
|
|
|
|
type Error = &'a WsClientError;
|
|
|
|
|
|
|
|
fn try_from(e: &'a WsClientError) -> Result<Self, Self::Error> {
|
|
|
|
match e {
|
|
|
|
WsClientError::JsonRpcError(err) => Ok(err.into()),
|
|
|
|
e => Err(e),
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 01:49:10 +03:00
|
|
|
/// The inner u32 is the maximum weight per item
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct JsonRpcResponseWeigher(pub u32);
|
|
|
|
|
|
|
|
impl JsonRpcResponseWeigher {
|
2023-10-17 09:56:38 +03:00
|
|
|
pub fn weigh<K, T>(&self, _key: &K, value: &ForwardedResponse<T>) -> u32 {
|
|
|
|
if let Ok(x) = value.num_bytes().try_into() {
|
|
|
|
if x > self.0 {
|
|
|
|
// return max. the item may start to be inserted into the cache, but it will be immediatly removed
|
|
|
|
u32::MAX
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
}
|
2023-06-17 01:49:10 +03:00
|
|
|
} else {
|
2023-10-17 09:56:38 +03:00
|
|
|
u32::MAX
|
2023-06-17 01:49:10 +03:00
|
|
|
}
|
|
|
|
}
|
2023-05-13 21:13:02 +03:00
|
|
|
}
|
2023-05-30 01:48:22 +03:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-10-11 10:12:20 +03:00
|
|
|
use super::ForwardedResponse;
|
2023-06-17 01:49:10 +03:00
|
|
|
use crate::response_cache::JsonRpcResponseWeigher;
|
2023-09-18 21:04:22 +03:00
|
|
|
use moka::future::{Cache, CacheBuilder};
|
2023-05-31 02:32:34 +03:00
|
|
|
use serde_json::value::RawValue;
|
2023-06-17 01:49:10 +03:00
|
|
|
use std::{sync::Arc, time::Duration};
|
2023-05-30 01:48:22 +03:00
|
|
|
|
|
|
|
#[tokio::test(start_paused = true)]
|
|
|
|
async fn test_json_rpc_query_weigher() {
|
|
|
|
let max_item_weight = 200;
|
|
|
|
let weight_capacity = 1_000;
|
|
|
|
|
2023-06-17 01:49:10 +03:00
|
|
|
let weigher = JsonRpcResponseWeigher(max_item_weight);
|
2023-05-31 02:32:34 +03:00
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
let small_data: ForwardedResponse<Arc<RawValue>> = ForwardedResponse::Result {
|
2023-06-07 23:57:38 +03:00
|
|
|
value: Box::<RawValue>::default().into(),
|
2023-10-17 09:56:38 +03:00
|
|
|
num_bytes: (max_item_weight / 2) as u64,
|
2023-05-30 01:48:22 +03:00
|
|
|
};
|
|
|
|
|
2023-06-17 01:49:10 +03:00
|
|
|
assert_eq!(weigher.weigh(&(), &small_data), max_item_weight / 2);
|
2023-06-08 03:42:34 +03:00
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
let max_sized_data: ForwardedResponse<Arc<RawValue>> = ForwardedResponse::Result {
|
2023-06-07 23:57:38 +03:00
|
|
|
value: Box::<RawValue>::default().into(),
|
2023-10-17 09:56:38 +03:00
|
|
|
num_bytes: max_item_weight as u64,
|
2023-05-30 01:48:22 +03:00
|
|
|
};
|
|
|
|
|
2023-06-17 01:49:10 +03:00
|
|
|
assert_eq!(weigher.weigh(&(), &max_sized_data), max_item_weight);
|
2023-06-08 03:42:34 +03:00
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
let oversized_data: ForwardedResponse<Arc<RawValue>> = ForwardedResponse::Result {
|
2023-06-07 23:57:38 +03:00
|
|
|
value: Box::<RawValue>::default().into(),
|
2023-10-17 09:56:38 +03:00
|
|
|
num_bytes: (max_item_weight * 2) as u64,
|
2023-05-30 01:48:22 +03:00
|
|
|
};
|
|
|
|
|
2023-06-17 01:49:10 +03:00
|
|
|
assert_eq!(weigher.weigh(&(), &oversized_data), u32::MAX);
|
|
|
|
|
2023-10-11 10:12:20 +03:00
|
|
|
let test_cache: Cache<u32, ForwardedResponse<Arc<RawValue>>> =
|
2023-06-17 01:49:10 +03:00
|
|
|
CacheBuilder::new(weight_capacity)
|
|
|
|
.weigher(move |k, v| weigher.weigh(k, v))
|
|
|
|
.time_to_live(Duration::from_secs(2))
|
|
|
|
.build();
|
2023-06-08 03:42:34 +03:00
|
|
|
|
2023-06-08 03:26:38 +03:00
|
|
|
test_cache.insert(0, small_data).await;
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-09-18 21:04:22 +03:00
|
|
|
test_cache.get(&0).await.unwrap();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-06-08 03:26:38 +03:00
|
|
|
test_cache.insert(1, max_sized_data).await;
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-09-18 21:04:22 +03:00
|
|
|
test_cache.get(&0).await.unwrap();
|
|
|
|
test_cache.get(&1).await.unwrap();
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-06-08 03:26:38 +03:00
|
|
|
test_cache.insert(2, oversized_data).await;
|
2023-05-30 01:48:22 +03:00
|
|
|
|
2023-09-18 21:04:22 +03:00
|
|
|
test_cache.get(&0).await.unwrap();
|
|
|
|
test_cache.get(&1).await.unwrap();
|
2023-06-17 01:49:10 +03:00
|
|
|
|
|
|
|
// oversized data will be in the cache temporarily (it should just be an arc though, so that should be fine)
|
2023-09-18 21:04:22 +03:00
|
|
|
test_cache.get(&2).await.unwrap();
|
2023-06-17 01:49:10 +03:00
|
|
|
|
|
|
|
// sync should do necessary cleanup
|
2023-09-18 21:04:22 +03:00
|
|
|
test_cache.run_pending_tasks().await;
|
2023-06-17 01:49:10 +03:00
|
|
|
|
|
|
|
// now it should be empty
|
2023-09-18 21:04:22 +03:00
|
|
|
assert!(test_cache.get(&2).await.is_none());
|
2023-05-30 01:48:22 +03:00
|
|
|
}
|
|
|
|
}
|