Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/orderbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ axum = { workspace = true }
bad-tokens = { workspace = true }
balance-overrides = { workspace = true }
bigdecimal = { workspace = true }
cached = { workspace = true }
chain = { workspace = true }
chrono = { workspace = true, features = ["clock"] }
clap = { workspace = true }
Expand All @@ -43,6 +42,7 @@ humantime = { workspace = true }
humantime-serde = { workspace = true }
mimalloc = { workspace = true, optional = true }
model = { workspace = true }
moka = { workspace = true }
multibase = { workspace = true }
num = { workspace = true }
number = { workspace = true }
Expand Down
50 changes: 18 additions & 32 deletions crates/orderbook/src/ipfs_app_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use {
crate::ipfs::Ipfs,
anyhow::Result,
app_data::{AppDataHash, create_ipfs_cid},
cached::{Cached, TimedSizedCache},
std::sync::Mutex,
moka::future::Cache,
};

pub struct IpfsAppData {
ipfs: Ipfs,
cache: Mutex<TimedSizedCache<AppDataHash, Option<String>>>,
cache: Cache<AppDataHash, Option<String>>,
metrics: &'static Metrics,
}

Expand All @@ -34,9 +33,10 @@ impl IpfsAppData {
}
Self {
ipfs,
cache: Mutex::new(TimedSizedCache::with_size_and_lifespan_and_refresh(
1000, 600, false,
)),
cache: Cache::builder()
.max_capacity(1000)
.time_to_live(std::time::Duration::from_secs(600))
.build(),
metrics,
}
}
Expand All @@ -63,7 +63,7 @@ impl IpfsAppData {
result
}
Ok(None) => {
tracing::debug!(?contract_app_data, %cid,"no full app data");
tracing::debug!(?contract_app_data, %cid, "no full app data");
return Ok(None);
}
Err(err) => {
Expand All @@ -86,37 +86,23 @@ impl IpfsAppData {

pub async fn fetch(&self, contract_app_data: &AppDataHash) -> Result<Option<String>> {
let outcome = |data: &Option<String>| if data.is_some() { "found" } else { "missing" };

let metric = &self.metrics.app_data;
if let Some(cached) = self
.cache
.lock()
.unwrap()
.cache_get(contract_app_data)
.cloned()
{
if let Some(cached) = self.cache.get(contract_app_data).await {
metric.with_label_values(&[outcome(&cached), "cache"]).inc();
return Ok(cached);
}

let fetched = {
let _timer = self.metrics.fetches.start_timer();
self.fetch_raw(contract_app_data).await
};
let result = match fetched {
Ok(result) => result,
Err(err) => {
let _timer = self.metrics.fetches.start_timer();
let result = self.fetch_raw(contract_app_data).await;
match &result {
Ok(result) => {
self.cache.insert(*contract_app_data, result.clone()).await;
metric.with_label_values(&[outcome(result), "node"]).inc();
}
Err(_) => {
metric.with_label_values(&["error", "node"]).inc();
return Err(err);
}
};

self.cache
.lock()
.unwrap()
.cache_set(*contract_app_data, result.clone());
metric.with_label_values(&[outcome(&result), "node"]).inc();
Ok(result)
}
result
}
}

Expand Down
Loading