mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-13 01:51:23 +00:00
Full support for multiple connections per peer in libp2p-swarm. (#1519)
* [libp2p-swarm] Make the multiple connections per peer first-class. This commit makes the notion of multiple connections per peer first-class in the API of libp2p-swarm, introducing the new callbacks `inject_connection_established` and `inject_connection_closed`. The `endpoint` parameter from `inject_connected` and `inject_disconnected` is removed, since the first connection to open may not be the last connection to close, i.e. it cannot be guaranteed, as was previously the case, that the endpoints passed to these callbacks match up. * Have identify track all addresses. So that identify requests can be answered with the correct observed address of the connection on which the request arrives. * Cleanup * Cleanup * Improve the `Peer` state API. * Remove connection ID from `SwarmEvent::Dialing`. * Mark `DialPeerCondition` non-exhaustive. * Re-encapsulate `NetworkConfig`. To retain the possibility of not re-exposing all network configuration choices, thereby providing a more convenient API on the \`SwarmBuilder\`. * Rework Swarm::dial API. * Update CHANGELOG. * Doc formatting tweaks.
This commit is contained in:
@ -22,14 +22,15 @@ use crate::protocol::{FloodsubConfig, FloodsubMessage, FloodsubRpc, FloodsubSubs
|
||||
use crate::topic::Topic;
|
||||
use cuckoofilter::CuckooFilter;
|
||||
use fnv::FnvHashSet;
|
||||
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, connection::ConnectionId};
|
||||
use libp2p_core::{Multiaddr, PeerId, connection::ConnectionId};
|
||||
use libp2p_swarm::{
|
||||
NetworkBehaviour,
|
||||
NetworkBehaviourAction,
|
||||
PollParameters,
|
||||
ProtocolsHandler,
|
||||
OneShotHandler,
|
||||
NotifyHandler
|
||||
NotifyHandler,
|
||||
DialPeerCondition,
|
||||
};
|
||||
use rand;
|
||||
use smallvec::SmallVec;
|
||||
@ -96,7 +97,9 @@ impl Floodsub {
|
||||
}
|
||||
|
||||
if self.target_peers.insert(peer_id.clone()) {
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer { peer_id });
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id, condition: DialPeerCondition::Disconnected
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -236,9 +239,9 @@ impl NetworkBehaviour for Floodsub {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
fn inject_connected(&mut self, id: PeerId, _: ConnectedPoint) {
|
||||
fn inject_connected(&mut self, id: &PeerId) {
|
||||
// We need to send our subscriptions to the newly-connected node.
|
||||
if self.target_peers.contains(&id) {
|
||||
if self.target_peers.contains(id) {
|
||||
for topic in self.subscribed_topics.iter().cloned() {
|
||||
self.events.push_back(NetworkBehaviourAction::NotifyHandler {
|
||||
peer_id: id.clone(),
|
||||
@ -257,14 +260,17 @@ impl NetworkBehaviour for Floodsub {
|
||||
self.connected_peers.insert(id.clone(), SmallVec::new());
|
||||
}
|
||||
|
||||
fn inject_disconnected(&mut self, id: &PeerId, _: ConnectedPoint) {
|
||||
fn inject_disconnected(&mut self, id: &PeerId) {
|
||||
let was_in = self.connected_peers.remove(id);
|
||||
debug_assert!(was_in.is_some());
|
||||
|
||||
// We can be disconnected by the remote in case of inactivity for example, so we always
|
||||
// try to reconnect.
|
||||
if self.target_peers.contains(id) {
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer { peer_id: id.clone() });
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: id.clone(),
|
||||
condition: DialPeerCondition::Disconnected
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user