Skip to content

Commit

Permalink
Remove least active peer
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsa committed Oct 18, 2024
1 parent e77d0ac commit 4678b9b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 26 additions & 3 deletions core/network/impl/peer_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,21 @@ namespace kagome::network {
}
}

PeerId PeerManagerImpl::findLeastActivePeer() const {
if (active_peers_.empty()) {
return PeerId{};
}

auto it =
std::min_element(active_peers_.begin(),
active_peers_.end(),
[](const auto &l, const auto &r) {
return l.second.time_point < r.second.time_point;
});

return it->first;
}

void PeerManagerImpl::tryOpenValidationProtocol(
const PeerInfo &peer_info,
PeerState &peer_state,
Expand Down Expand Up @@ -820,9 +835,17 @@ namespace kagome::network {

if (not out) {
if (countPeers(PeerType::PEER_TYPE_IN) >= app_config_.inPeers()) {
connecting_peers_.erase(peer_id);
disconnectFromPeer(peer_id);
return;
PeerId peer_to_remove = findLeastActivePeer();
if (peer_to_remove.empty()) {
SL_ERROR(log_,
"New connection from peer {} was dropped: "
"no peers to disconnect",
peer_id);
connecting_peers_.erase(peer_id);
disconnectFromPeer(peer_id);
return;
}
connecting_peers_.erase(peer_to_remove);
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/network/impl/peer_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ namespace kagome::network {

void clearClosedPingingConnections();

PeerId findLeastActivePeer() const;

using IsLight = Tagged<bool, struct IsLightTag>;
size_t countPeers(PeerType in_out, IsLight in_light = false) const;

Expand Down

0 comments on commit 4678b9b

Please sign in to comment.