mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-21 05:41:33 +00:00
*: Dial with handler and return handler on error and closed (#2191)
Require `NetworkBehaviourAction::{DialPeer,DialAddress}` to contain a `ProtocolsHandler`. This allows a behaviour to attach custom state to its handler. The behaviour would no longer need to track this state separately during connection establishment, thus reducing state required in a behaviour. E.g. in the case of `libp2p-kad` the behaviour can include a `GetRecord` request in its handler, or e.g. in the case of `libp2p-request-response` the behaviour can include the first request in the handler. Return `ProtocolsHandler` on connection error and close. This allows a behaviour to extract its custom state previously included in the handler on connection failure and connection closing. E.g. in the case of `libp2p-kad` the behaviour could extract the attached `GetRecord` from the handler of the failed connection and then start another connection attempt with a new handler with the same `GetRecord` or bubble up an error to the user. Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
@ -41,8 +41,8 @@ use libp2p_core::{
|
||||
multiaddr::Protocol::Ip6, ConnectedPoint, Multiaddr, PeerId,
|
||||
};
|
||||
use libp2p_swarm::{
|
||||
DialPeerCondition, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
|
||||
ProtocolsHandler,
|
||||
DialPeerCondition, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction,
|
||||
NotifyHandler, PollParameters,
|
||||
};
|
||||
|
||||
use crate::backoff::BackoffStorage;
|
||||
@ -193,7 +193,7 @@ impl From<MessageAuthenticity> for PublishConfig {
|
||||
}
|
||||
|
||||
type GossipsubNetworkBehaviourAction =
|
||||
NetworkBehaviourAction<Arc<GossipsubHandlerIn>, GossipsubEvent>;
|
||||
NetworkBehaviourAction<GossipsubEvent, GossipsubHandler, Arc<GossipsubHandlerIn>>;
|
||||
|
||||
/// Network behaviour that handles the gossipsub protocol.
|
||||
///
|
||||
@ -425,8 +425,8 @@ where
|
||||
|
||||
impl<D, F> Gossipsub<D, F>
|
||||
where
|
||||
D: DataTransform,
|
||||
F: TopicSubscriptionFilter,
|
||||
D: DataTransform + Send + 'static,
|
||||
F: TopicSubscriptionFilter + Send + 'static,
|
||||
{
|
||||
/// Lists the hashes of the topics we are currently subscribed to.
|
||||
pub fn topics(&self) -> impl Iterator<Item = &TopicHash> {
|
||||
@ -1043,9 +1043,11 @@ where
|
||||
if !self.peer_topics.contains_key(peer_id) {
|
||||
// Connect to peer
|
||||
debug!("Connecting to explicit peer {:?}", peer_id);
|
||||
let handler = self.new_handler();
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: *peer_id,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
handler,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1493,9 +1495,11 @@ where
|
||||
self.px_peers.insert(peer_id);
|
||||
|
||||
// dial peer
|
||||
let handler = self.new_handler();
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
handler,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -2969,6 +2973,7 @@ where
|
||||
peer_id: &PeerId,
|
||||
connection_id: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
_: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
|
||||
) {
|
||||
// Remove IP from peer scoring system
|
||||
if let Some((peer_score, ..)) = &mut self.peer_score {
|
||||
@ -3169,47 +3174,12 @@ where
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
_: &mut impl PollParameters,
|
||||
) -> Poll<
|
||||
NetworkBehaviourAction<
|
||||
<Self::ProtocolsHandler as ProtocolsHandler>::InEvent,
|
||||
Self::OutEvent,
|
||||
>,
|
||||
> {
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||
if let Some(event) = self.events.pop_front() {
|
||||
return Poll::Ready(match event {
|
||||
NetworkBehaviourAction::NotifyHandler {
|
||||
peer_id,
|
||||
handler,
|
||||
event: send_event,
|
||||
} => {
|
||||
// clone send event reference if others references are present
|
||||
let event = Arc::try_unwrap(send_event).unwrap_or_else(|e| (*e).clone());
|
||||
NetworkBehaviourAction::NotifyHandler {
|
||||
peer_id,
|
||||
event,
|
||||
handler,
|
||||
}
|
||||
}
|
||||
NetworkBehaviourAction::GenerateEvent(e) => {
|
||||
NetworkBehaviourAction::GenerateEvent(e)
|
||||
}
|
||||
NetworkBehaviourAction::DialAddress { address } => {
|
||||
NetworkBehaviourAction::DialAddress { address }
|
||||
}
|
||||
NetworkBehaviourAction::DialPeer { peer_id, condition } => {
|
||||
NetworkBehaviourAction::DialPeer { peer_id, condition }
|
||||
}
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score }
|
||||
}
|
||||
NetworkBehaviourAction::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
} => NetworkBehaviourAction::CloseConnection {
|
||||
peer_id,
|
||||
connection,
|
||||
},
|
||||
});
|
||||
return Poll::Ready(event.map_in(|e: Arc<GossipsubHandlerIn>| {
|
||||
// clone send event reference if others references are present
|
||||
Arc::try_unwrap(e).unwrap_or_else(|e| (*e).clone())
|
||||
}));
|
||||
}
|
||||
|
||||
// update scores
|
||||
@ -3396,7 +3366,7 @@ impl<C: DataTransform, F: TopicSubscriptionFilter> fmt::Debug for Gossipsub<C, F
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Gossipsub")
|
||||
.field("config", &self.config)
|
||||
.field("events", &self.events)
|
||||
.field("events", &self.events.len())
|
||||
.field("control_pool", &self.control_pool)
|
||||
.field("publish_config", &self.publish_config)
|
||||
.field("topic_peers", &self.topic_peers)
|
||||
|
Reference in New Issue
Block a user