feat(identify): do not implicitly dial on push

Previously, we would implicitly establish a connection when the user wanted to push identify information to a peer. I believe that this is the wrong behaviour. Instead, I am suggesting to log a message that we are skipping the push to this peer.

Additionally, the way this is currently implemented does not make much sense. Dialing a peer takes time. In case we don't have a connection at all, it could be that we drop the push requests because there isn't an active handler and thus we would have unnecessarily established the connection.

Instead of fixing this - which would require buffering the push messages - I think we should just remove the implicit dial.

Pull-Request: #3843.
This commit is contained in:
Thomas Eizinger
2023-04-28 16:06:33 +02:00
committed by GitHub
parent 2f5270ba76
commit e3a1650933
4 changed files with 18 additions and 9 deletions

View File

@ -25,9 +25,8 @@ use libp2p_identity::PeerId;
use libp2p_identity::PublicKey;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionDenied, ConnectionHandlerUpgrErr, DialError,
ExternalAddresses, ListenAddresses, NetworkBehaviour, NotifyHandler, PollParameters,
THandlerInEvent, ToSwarm,
AddressScore, ConnectionDenied, ConnectionHandlerUpgrErr, DialError, ExternalAddresses,
ListenAddresses, NetworkBehaviour, NotifyHandler, PollParameters, THandlerInEvent, ToSwarm,
};
use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
use lru::LruCache;
@ -193,16 +192,17 @@ impl Behaviour {
I: IntoIterator<Item = PeerId>,
{
for p in peers {
if !self.connected.contains_key(&p) {
log::debug!("Not pushing to {p} because we are not connected");
continue;
}
let request = Request {
peer_id: p,
protocol: Protocol::Push,
};
if !self.requests.contains(&request) {
self.requests.push(request);
self.events.push_back(ToSwarm::Dial {
opts: DialOpts::peer_id(p).build(),
});
}
}
}