@@ -884,8 +884,6 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
884884 continue ;
885885 if (node->fDisconnect )
886886 continue ;
887- if (node->addr .IsLocal ())
888- continue ;
889887 vEvictionCandidates.push_back (CNodeRef (node));
890888 }
891889 }
@@ -916,30 +914,34 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
916914
917915 if (vEvictionCandidates.empty ()) return false ;
918916
919- // Identify the network group with the most connections
917+ // Identify the network group with the most connections and youngest member.
918+ // (vEvictionCandidates is already sorted by reverse connect time)
920919 std::vector<unsigned char > naMostConnections;
921920 unsigned int nMostConnections = 0 ;
921+ int64_t nMostConnectionsTime = 0 ;
922922 std::map<std::vector<unsigned char >, std::vector<CNodeRef> > mapAddrCounts;
923923 BOOST_FOREACH (const CNodeRef &node, vEvictionCandidates) {
924924 mapAddrCounts[node->addr .GetGroup ()].push_back (node);
925+ int64_t grouptime = mapAddrCounts[node->addr .GetGroup ()][0 ]->nTimeConnected ;
926+ size_t groupsize = mapAddrCounts[node->addr .GetGroup ()].size ();
925927
926- if (mapAddrCounts[node->addr .GetGroup ()].size () > nMostConnections) {
927- nMostConnections = mapAddrCounts[node->addr .GetGroup ()].size ();
928+ if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
929+ nMostConnections = groupsize;
930+ nMostConnectionsTime = grouptime;
928931 naMostConnections = node->addr .GetGroup ();
929932 }
930933 }
931934
932935 // Reduce to the network group with the most connections
933936 vEvictionCandidates = mapAddrCounts[naMostConnections];
934937
935- // Do not disconnect peers if there is only 1 connection from their network group
938+ // Do not disconnect peers if there is only one unprotected connection from their network group.
936939 if (vEvictionCandidates.size () <= 1 )
937940 // unless we prefer the new connection (for whitelisted peers)
938941 if (!fPreferNewConnection )
939942 return false ;
940943
941- // Disconnect the most recent connection from the network group with the most connections
942- std::sort (vEvictionCandidates.begin (), vEvictionCandidates.end (), ReverseCompareNodeTimeConnected);
944+ // Disconnect from the network group with the most connections
943945 vEvictionCandidates[0 ]->fDisconnect = true ;
944946
945947 return true ;
0 commit comments