Skip to content

Commit

Permalink
close duplicate connection
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <[email protected]>
  • Loading branch information
turuslan committed Nov 14, 2024
1 parent 192b29d commit d273fac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/network/impl/peer_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "scale/libp2p_types.hpp"
#include "storage/predefined_keys.hpp"
#include "utils/pool_handler_ready_make.hpp"
#include "utils/weak_macro.hpp"

namespace {
/// Reputation value for a node when we get disconnected from it.
Expand Down Expand Up @@ -124,6 +125,28 @@ namespace kagome::network {
}
});

peer_connected_sub_ =
host_.getBus()
.getChannel<libp2p::event::network::OnNewConnectionChannel>()
.subscribe(
[WEAK_SELF](std::weak_ptr<libp2p::connection::CapableConnection>
weak_conn) {
WEAK_LOCK(self);
WEAK_LOCK(conn);
auto peer_id = conn->remotePeer().value();
for (auto &conn2 : self->host_.getNetwork()
.getConnectionManager()
.getConnectionsToPeer(peer_id)) {
if (conn2 == conn) {
continue;
}
if (conn2->isInitiator() != conn->isInitiator()) {
continue;
}
std::ignore = conn->close();
}
});

peer_disconnected_handler_ =
host_.getBus()
.getChannel<libp2p::event::network::OnPeerDisconnectedChannel>()
Expand Down
1 change: 1 addition & 0 deletions core/network/impl/peer_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ namespace kagome::network {
std::shared_ptr<network::PeerView> peer_view_;

libp2p::event::Handle add_peer_handle_;
libp2p::event::Handle peer_connected_sub_;
libp2p::event::Handle peer_disconnected_handler_;
std::unordered_set<PeerId> peers_in_queue_;
std::deque<PeerId> queue_to_connect_;
Expand Down

0 comments on commit d273fac

Please sign in to comment.