-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add rendezvous rediscovery and registration renewal
Add cache with eviction listener to trigger discovery and renewal events on an interval and at TTL respectively.
- Loading branch information
Showing
8 changed files
with
323 additions
and
18 deletions.
There are no files selected for viewing
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,5 +1,5 @@ | ||
use_flake | ||
|
||
export RUST_LOG=homestar=debug,homestar_runtime=debug,libp2p=info,libp2p_gossipsub::behaviour=debug,tarpc=info,tower_http=debug | ||
export RUST_LOG=homestar=debug,homestar_runtime=debug,libp2p=info,libp2p_gossipsub::behaviour=debug,tarpc=info,tower_http=debug,moka=debug | ||
export RUST_BACKTRACE=full | ||
export RUSTFLAGS="--cfg tokio_unstable" |
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,91 @@ | ||
use crate::{channel, event_handler::Event}; | ||
use libp2p::PeerId; | ||
use moka::{future::Cache, Expiry as ExpiryBase}; | ||
use std::{ | ||
collections::HashMap, | ||
sync::Arc, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
struct Expiry; | ||
|
||
impl ExpiryBase<String, CacheValue> for Expiry { | ||
fn expire_after_create( | ||
&self, | ||
_key: &String, | ||
value: &CacheValue, | ||
_current_time: Instant, | ||
) -> Option<Duration> { | ||
value.expiration.as_duration() | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct CacheValue { | ||
expiration: Expiration, | ||
data: HashMap<String, CacheData>, | ||
} | ||
|
||
impl CacheValue { | ||
pub(crate) fn new(expiration: Expiration, data: HashMap<String, CacheData>) -> Self { | ||
Self { expiration, data } | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) enum CacheData { | ||
Peer(PeerId), | ||
OnEviction(DispatchEvent), | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) enum DispatchEvent { | ||
RegisterPeer, | ||
DiscoverPeers, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub(crate) enum Expiration { | ||
Registration(Duration), | ||
Discovery(Duration), | ||
} | ||
|
||
impl Expiration { | ||
pub(crate) fn as_duration(&self) -> Option<Duration> { | ||
match self { | ||
Expiration::Registration(ttl) => Some(*ttl), | ||
Expiration::Discovery(interval) => Some(*interval), | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn setup_cache( | ||
sender: Arc<channel::AsyncBoundedChannelSender<Event>>, | ||
) -> Cache<String, CacheValue> { | ||
let eviction_listener = move |_key: Arc<String>, val: CacheValue, _cause| { | ||
let tx = Arc::clone(&sender); | ||
|
||
if let Some(CacheData::OnEviction(event)) = val.data.get("on_eviction") { | ||
match event { | ||
DispatchEvent::RegisterPeer => { | ||
if let Some(CacheData::Peer(rendezvous_node)) = val.data.get("rendezvous_node") | ||
{ | ||
let _ = tx.send(Event::RegisterPeer(rendezvous_node.to_owned())); | ||
}; | ||
} | ||
DispatchEvent::DiscoverPeers => { | ||
if let Some(CacheData::Peer(rendezvous_node)) = val.data.get("rendezvous_node") | ||
{ | ||
let _ = tx.send(Event::DiscoverPeers(rendezvous_node.to_owned())); | ||
}; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
Cache::builder() | ||
.expire_after(Expiry) | ||
.time_to_live(Duration::from_secs(5)) | ||
.eviction_listener(eviction_listener) | ||
.build() | ||
} |
Oops, something went wrong.