-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add resolver option to make cold starts faster
- Loading branch information
Showing
7 changed files
with
206 additions
and
108 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! This example shows how to run a resolver node. | ||
use std::num::NonZeroUsize; | ||
|
||
use tracing::Level; | ||
use tracing_subscriber; | ||
|
||
use pkarr::{PkarrClient, Result}; | ||
|
||
fn main() -> Result<()> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(Level::DEBUG) | ||
.with_env_filter("pkarr") | ||
.init(); | ||
|
||
let client = PkarrClient::builder() | ||
.resolver() | ||
.cache_size(NonZeroUsize::new(1000).unwrap()) | ||
.build() | ||
.unwrap(); | ||
|
||
println!( | ||
"\nRunning a resolver at {} ...\n", | ||
client.loca_addr().unwrap() | ||
); | ||
|
||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,49 @@ | ||
use lru::LruCache; | ||
use mainline::Id; | ||
use std::num::NonZeroUsize; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use crate::{PublicKey, SignedPacket}; | ||
use crate::SignedPacket; | ||
|
||
/// A wrapper around `LruCache`. This struct is thread safe, doesn't return any references to any | ||
/// elements inside. | ||
#[derive(Debug, Clone)] | ||
pub struct PkarrCache { | ||
inner: Arc<Mutex<LruCache<PublicKey, SignedPacket>>>, | ||
inner: Arc<Mutex<LruCache<Id, SignedPacket>>>, | ||
} | ||
|
||
impl PkarrCache { | ||
/// Creats a new `LRU` cache that holds at most `cap` items. | ||
pub fn new(cap: NonZeroUsize) -> Self { | ||
pub fn new(capacity: NonZeroUsize) -> Self { | ||
Self { | ||
inner: Arc::new(Mutex::new(LruCache::new(cap))), | ||
inner: Arc::new(Mutex::new(LruCache::new(capacity))), | ||
} | ||
} | ||
|
||
/// Puts [SignedPacket], if a version of the packet already exists, | ||
/// and it has the same [SignedPacket::as_bytes], then only [SignedPacket::last_seen] will be | ||
/// updated, otherwise the input will be cloned. | ||
pub fn put(&self, signed_packet: &SignedPacket) { | ||
pub fn put(&self, target: &Id, signed_packet: &SignedPacket) { | ||
let mut lock = self.inner.lock().unwrap(); | ||
|
||
let public_key = signed_packet.public_key(); | ||
|
||
match lock.get_mut(&public_key) { | ||
match lock.get_mut(target) { | ||
Some(existing) => { | ||
if existing.as_bytes() == signed_packet.as_bytes() { | ||
// just refresh the last_seen | ||
existing.set_last_seen(signed_packet.last_seen()) | ||
} else { | ||
lock.put(public_key, signed_packet.clone()); | ||
lock.put(*target, signed_packet.clone()); | ||
} | ||
} | ||
None => { | ||
lock.put(public_key, signed_packet.clone()); | ||
lock.put(*target, signed_packet.clone()); | ||
} | ||
} | ||
} | ||
|
||
/// Returns the [SignedPacket] for the public_key in the cache or None if it is not present in the cache. | ||
/// Moves the key to the head of the LRU list if it exists. | ||
pub fn get(&self, public_key: &PublicKey) -> Option<SignedPacket> { | ||
pub fn get(&self, public_key: &Id) -> Option<SignedPacket> { | ||
self.inner.lock().unwrap().get(public_key).cloned() | ||
} | ||
} |
Oops, something went wrong.