mirror of
https://github.com/aljazceru/pkdns.git
synced 2025-12-17 05:54:21 +01:00
use ttl_cache instead of retainer
This commit is contained in:
39
Cargo.lock
generated
39
Cargo.lock
generated
@@ -132,26 +132,6 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3c07dab4369547dbe5114677b33fbbf724971019f3818172d59a97a61c774ffd"
|
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]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.77"
|
version = "0.1.77"
|
||||||
@@ -720,12 +700,6 @@ dependencies = [
|
|||||||
"windows-sys 0.52.0",
|
"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]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.0.1"
|
version = "2.0.1"
|
||||||
@@ -1197,7 +1171,6 @@ dependencies = [
|
|||||||
"ctrlc",
|
"ctrlc",
|
||||||
"pkarr",
|
"pkarr",
|
||||||
"pknames_core",
|
"pknames_core",
|
||||||
"retainer",
|
|
||||||
"simple-dns",
|
"simple-dns",
|
||||||
"tokio",
|
"tokio",
|
||||||
"ttl_cache",
|
"ttl_cache",
|
||||||
@@ -1371,18 +1344,6 @@ version = "0.8.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
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]]
|
[[package]]
|
||||||
name = "rmp"
|
name = "rmp"
|
||||||
version = "0.8.12"
|
version = "0.8.12"
|
||||||
|
|||||||
@@ -18,4 +18,3 @@ chrono = "0.4.33"
|
|||||||
tokio = { version = "1.36.0", features = ["full"] }
|
tokio = { version = "1.36.0", features = ["full"] }
|
||||||
async-trait = "0.1.77"
|
async-trait = "0.1.77"
|
||||||
anyhow = "1.0.79"
|
anyhow = "1.0.79"
|
||||||
retainer = "0.3.0"
|
|
||||||
|
|||||||
@@ -1,28 +1,24 @@
|
|||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
use pkarr::{dns::Packet, PublicKey};
|
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
|
* Pkarr record ttl cache
|
||||||
*/
|
*/
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PkarrPacketTtlCache {
|
pub struct PkarrPacketTtlCache {
|
||||||
cache: Arc<Cache<String, Vec<u8>>>,
|
|
||||||
max_cache_ttl: u64,
|
max_cache_ttl: u64,
|
||||||
monitor: Arc<Mutex<JoinHandle<()>>>,
|
cache: Arc<Mutex<TtlCache<String, Vec<u8>>>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PkarrPacketTtlCache {
|
impl PkarrPacketTtlCache {
|
||||||
pub async fn new(max_cache_ttl: u64) -> Self {
|
pub async fn new(max_cache_ttl: u64) -> Self {
|
||||||
let cache: Arc<Cache<String, Vec<u8>>> = 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 {
|
PkarrPacketTtlCache {
|
||||||
cache: Arc::new(Cache::new()),
|
|
||||||
max_cache_ttl,
|
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 = ttl.min(self.max_cache_ttl);
|
||||||
let ttl = Duration::from_secs(ttl as u64);
|
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<Vec<u8>> {
|
pub async fn get(&self, pubkey: &PublicKey) -> Option<Vec<u8>> {
|
||||||
let z32 = pubkey.to_z32();
|
let z32 = pubkey.to_z32();
|
||||||
self.cache.get(&z32).await.map(|value| value.clone())
|
let cache = self.cache.lock().await;
|
||||||
}
|
cache.get(&z32).map(|value| value.clone())
|
||||||
|
|
||||||
pub async fn stop(self) {
|
|
||||||
self.monitor.lock().await.abort();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user