From cb04bae6ecefbabd4c0532efc5a9dd30a57b47c8 Mon Sep 17 00:00:00 2001 From: Severin Buhler Date: Fri, 9 Feb 2024 11:05:17 +0100 Subject: [PATCH] use ttl_cache instead of retainer --- Cargo.lock | 39 --------------------------------------- Cargo.toml | 1 - src/pkarr_cache.rs | 27 ++++++++++++--------------- 3 files changed, 12 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 179aeb2..f1749b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,26 +132,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c07dab4369547dbe5114677b33fbbf724971019f3818172d59a97a61c774ffd" -[[package]] -name = "async-lock" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" -dependencies = [ - "event-listener", -] - -[[package]] -name = "async-timer" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba5fa6ed76cb2aa820707b4eb9ec46f42da9ce70b0eafab5e5e34942b38a44d5" -dependencies = [ - "libc", - "wasm-bindgen", - "winapi", -] - [[package]] name = "async-trait" version = "0.1.77" @@ -720,12 +700,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "event-listener" -version = "2.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" - [[package]] name = "fastrand" version = "2.0.1" @@ -1197,7 +1171,6 @@ dependencies = [ "ctrlc", "pkarr", "pknames_core", - "retainer", "simple-dns", "tokio", "ttl_cache", @@ -1371,18 +1344,6 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" -[[package]] -name = "retainer" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8c01a8276c14d0f8d51ebcf8a48f0748f9f73f5f6b29e688126e6a52bcb145" -dependencies = [ - "async-lock", - "async-timer", - "log", - "rand", -] - [[package]] name = "rmp" version = "0.8.12" diff --git a/Cargo.toml b/Cargo.toml index 539685b..c032029 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,3 @@ chrono = "0.4.33" tokio = { version = "1.36.0", features = ["full"] } async-trait = "0.1.77" anyhow = "1.0.79" -retainer = "0.3.0" diff --git a/src/pkarr_cache.rs b/src/pkarr_cache.rs index 389aca3..d25a7a6 100644 --- a/src/pkarr_cache.rs +++ b/src/pkarr_cache.rs @@ -1,28 +1,24 @@ use std::{sync::Arc, time::Duration}; use pkarr::{dns::Packet, PublicKey}; -use retainer::Cache; -use tokio::{sync::Mutex, task::JoinHandle}; + +use tokio::sync::Mutex; +use ttl_cache::TtlCache; /** * Pkarr record ttl cache */ #[derive(Clone)] pub struct PkarrPacketTtlCache { - cache: Arc>>, max_cache_ttl: u64, - monitor: Arc>>, + cache: Arc>>> } impl PkarrPacketTtlCache { pub async fn new(max_cache_ttl: u64) -> Self { - let cache: Arc>> = Arc::new(Cache::new()); - let monitor = tokio::spawn(async move { cache.monitor(4, 0.25, Duration::from_secs(3)).await }); - let monitor = Arc::new(Mutex::new(monitor)); PkarrPacketTtlCache { - cache: Arc::new(Cache::new()), max_cache_ttl, - monitor, + cache: Arc::new(Mutex::new(TtlCache::new(100_000))) } } @@ -43,15 +39,16 @@ impl PkarrPacketTtlCache { let ttl = ttl.min(self.max_cache_ttl); let ttl = Duration::from_secs(ttl as u64); - self.cache.insert(pubkey.to_z32(), reply, ttl).await; + let mut cache = self.cache.lock().await; + cache.insert(pubkey.to_z32(), reply, ttl); } + /** + * Get packet + */ pub async fn get(&self, pubkey: &PublicKey) -> Option> { let z32 = pubkey.to_z32(); - self.cache.get(&z32).await.map(|value| value.clone()) - } - - pub async fn stop(self) { - self.monitor.lock().await.abort(); + let cache = self.cache.lock().await; + cache.get(&z32).map(|value| value.clone()) } }