Replies: 1 comment
-
A simple workaround I implemented for now is to delay calling pub struct Behaviour {
kademlia: kad::Behaviour<kad::store::MemoryStore>,
pending_peers: HashMap<(PeerId, Multiaddr), Instant>,
}
impl NetworkBehaviour for Behaviour {
// ...
fn on_swarm_event(&mut self, event: FromSwarm<'_>) {
match event {
FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id,
failed_addresses,
..
}) => {
self.pending_peers.retain(|(pending_peer_id, addr), _| {
// Keep all entries for different peers
pending_peer_id != &peer_id
// OR keep same-peer entries that failed
|| failed_addresses.iter().any(|failed_addr| failed_addr == addr)
});
}
FromSwarm::NewExternalAddrOfPeer(NewExternalAddrOfPeer { peer_id, addr }) => {
self.kademlia.add_address(&peer_id, addr.clone());
}
FromSwarm::DialFailure(DialFailure {
peer_id: Some(peer_id),
error: DialError::Transport(errors),
..
}) => {
let now = Instant::now();
for (addr, _) in errors {
self.pending_peers
.entry((peer_id, addr.clone()))
.or_insert(now);
}
}
_ => {}
}
self.kademlia.on_swarm_event(event)
}
fn poll(
&mut self,
cx: &mut task::Context<'_>,
) -> task::Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
let failed_peers = self
.pending_peers
.extract_if(|_, failed_at| failed_at.elapsed() > Duration::from_secs(5));
for ((peer_id, addr), _) in failed_peers {
self.kademlia.remove_address(&peer_id, &addr);
}
// ...
}
} Any advice on if this approach is good or has issues, please let me know. I believe the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm experiencing connection issues when peers start simultaneously, and I'm unclear about the proper way to manage peer addresses across multiple behaviors.
Setup
I have a behavior combining
mdns
,kad
, andrequest_response
. When mDNS discovers peers, I need to make them available to both kademlia and request_response.API Usage Question
request_response::Behaviour::add_address
is deprecated in favor ofSwarm::add_peer_address
, butkad::Behaviour::add_address
is not deprecated. This suggests different address management patterns:My Current Implementation
To avoid manual kademlia address management, I implemented this in my wrapper's
NetworkBehaviour::on_swarm_event
:This is replicating the code I found here:
rust-libp2p/swarm/src/behaviour/peer_addresses.rs
Lines 24 to 41 in 1aa4337
The Problem
This works when peers start at different times, but when two peers start simultaneously, I consistently get:
This
InvalidData
error causes discovered addresses to be immediately removed via theDialFailure
handler, preventing successful connections.Questions
NewExternalAddrOfPeer
like request_response does?swarm.add_peer_address()
andkad.add_address()
explicitly?InvalidData
error specifically during simultaneous startup, and how can I handle it properly?Beta Was this translation helpful? Give feedback.
All reactions