Skip to content

Commit

Permalink
Refine the Ready peers event
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Sep 11, 2023
1 parent c7f0568 commit 37210e4
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions dht-cache/src/dht.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! DHT Abstraction
//!
use std::collections::HashSet;
use std::time::Duration;

use crate::domolibp2p::{DomoBehaviour, OutEvent};
Expand Down Expand Up @@ -28,7 +29,7 @@ pub enum Event {
VolatileData(String),
Config(String),
Discovered(Vec<PeerId>),
Ready(Vec<PeerId>),
Ready(HashSet<PeerId>),
}

fn handle_command(swarm: &mut Swarm<DomoBehaviour>, cmd: Command) -> bool {
Expand Down Expand Up @@ -169,15 +170,15 @@ pub fn dht_channel(
let volatile = Topic::new("domo-volatile-data").hash();
let persistent = Topic::new("domo-persistent-data").hash();
let config = Topic::new("domo-config").hash();

let mut ready_peers = HashSet::new();
// Only peers that subscribed to all the topics are usable
let check_peers = |swarm: &mut Swarm<DomoBehaviour>| {
swarm
.behaviour_mut()
.gossipsub
.all_peers()
.filter_map(|(p, topics)| {
log::info!("{p}, {topics:?}");
log::debug!("{p}, {topics:?}");
(topics.contains(&&volatile)
&& topics.contains(&&persistent)
&& topics.contains(&&config))
Expand All @@ -192,10 +193,12 @@ pub fn dht_channel(
// the mdns event is not enough to ensure we can send messages
_ = interval.tick() => {
log::debug!("{} Checking for peers", swarm.local_peer_id());
let peers: Vec<_> = check_peers(&mut swarm);
if !peers.is_empty() &&
ev_send.send(Event::Ready(peers)).is_err() {
let peers: HashSet<_> = check_peers(&mut swarm);
if !peers.is_empty() && ready_peers != peers {
ready_peers = peers.clone();
if ev_send.send(Event::Ready(peers)).is_err() {
return swarm;
}
}
}
cmd = cmd_recv.recv() => {
Expand Down

0 comments on commit 37210e4

Please sign in to comment.