mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 01:01:34 +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:
@ -27,8 +27,9 @@ use libp2p_core::{
|
||||
ConnectedPoint, Multiaddr, PeerId, PublicKey,
|
||||
};
|
||||
use libp2p_swarm::{
|
||||
AddressScore, DialPeerCondition, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction,
|
||||
NotifyHandler, PollParameters, ProtocolsHandler, ProtocolsHandlerUpgrErr,
|
||||
AddressScore, DialError, DialPeerCondition, IntoProtocolsHandler, NegotiatedSubstream,
|
||||
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, ProtocolsHandler,
|
||||
ProtocolsHandlerUpgrErr,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
@ -52,7 +53,7 @@ pub struct Identify {
|
||||
/// Pending replies to send.
|
||||
pending_replies: VecDeque<Reply>,
|
||||
/// Pending events to be emitted when polled.
|
||||
events: VecDeque<NetworkBehaviourAction<IdentifyPush, IdentifyEvent>>,
|
||||
events: VecDeque<NetworkBehaviourAction<IdentifyEvent, IdentifyHandler>>,
|
||||
/// Peers to which an active push with current information about
|
||||
/// the local peer should be sent.
|
||||
pending_push: HashSet<PeerId>,
|
||||
@ -173,9 +174,11 @@ impl Identify {
|
||||
for p in peers {
|
||||
if self.pending_push.insert(p) {
|
||||
if !self.connected.contains_key(&p) {
|
||||
let handler = self.new_handler();
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: p,
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
handler,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -213,13 +216,14 @@ impl NetworkBehaviour for Identify {
|
||||
peer_id: &PeerId,
|
||||
conn: &ConnectionId,
|
||||
_: &ConnectedPoint,
|
||||
_: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
|
||||
) {
|
||||
if let Some(addrs) = self.connected.get_mut(peer_id) {
|
||||
addrs.remove(conn);
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_dial_failure(&mut self, peer_id: &PeerId) {
|
||||
fn inject_dial_failure(&mut self, peer_id: &PeerId, _: Self::ProtocolsHandler, _: DialError) {
|
||||
if !self.connected.contains_key(peer_id) {
|
||||
self.pending_push.remove(peer_id);
|
||||
}
|
||||
@ -292,12 +296,7 @@ impl NetworkBehaviour for Identify {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
params: &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(event);
|
||||
}
|
||||
|
Reference in New Issue
Block a user