From 121cf9962cf621fb5655c29d1e77658d5623e23b Mon Sep 17 00:00:00 2001 From: Gennady Laventman Date: Wed, 18 Jan 2017 13:35:39 +0200 Subject: [PATCH] FAB-1723 Fix peers filtering for gossip push/pull File gossip/filter/filter.go Method SelectPeer - in case of small k, log(membership) for example, and many small channels in most cases it will return empty list - as first step it choose k peers from membership and only after that filter them, as result it will remain with zero peers Correct way to implement it is filter peers first and only after that choose k peers from filtered list. Change-Id: I1577df37cc8441adf7e96a0e847dc901afd60ea8 Signed-off-by: Gennady Laventman --- gossip/filter/filter.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/gossip/filter/filter.go b/gossip/filter/filter.go index ce2b9838a7e..773aa9fe0e7 100644 --- a/gossip/filter/filter.go +++ b/gossip/filter/filter.go @@ -41,23 +41,27 @@ func CombineRoutingFilters(filters ...RoutingFilter) RoutingFilter { // SelectPeers returns a slice of peers that match a list of routing filters func SelectPeers(k int, peerPool []discovery.NetworkMember, filters ...RoutingFilter) []*comm.RemotePeer { + var filteredPeers []*comm.RemotePeer + for _, peer := range peerPool { + if CombineRoutingFilters(filters ...)(peer) { + filteredPeers = append(filteredPeers, &comm.RemotePeer{PKIID: peer.PKIid, Endpoint: peer.Endpoint}) + } + } + var indices []int - if len(peerPool) <= k { - indices = make([]int, len(peerPool)) - for i := 0; i < len(peerPool); i++ { + if len(filteredPeers) <= k { + indices = make([]int, len(filteredPeers)) + for i := 0; i < len(filteredPeers); i++ { indices[i] = i } } else { - indices = util.GetRandomIndices(k, len(peerPool)-1) + indices = util.GetRandomIndices(k, len(filteredPeers)-1) } var remotePeers []*comm.RemotePeer for _, index := range indices { - peer := peerPool[index] - if CombineRoutingFilters(filters ...)(peer) { - remotePeers = append(remotePeers, &comm.RemotePeer{PKIID: peer.PKIid, Endpoint: peer.Endpoint}) - } - + remotePeers = append(remotePeers, filteredPeers[index]) } + return remotePeers }