63 Star 183 Fork 3

Gitee 极速下载/hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
filter.go 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package filter
import (
"math/rand"
"github.com/hyperledger/fabric/gossip/comm"
"github.com/hyperledger/fabric/gossip/discovery"
"github.com/hyperledger/fabric/gossip/util"
)
// RoutingFilter defines a predicate on a NetworkMember
// It is used to assert whether a given NetworkMember should be
// selected for be given a message
type RoutingFilter func(discovery.NetworkMember) bool
// SelectNonePolicy selects an empty set of members
var SelectNonePolicy = func(discovery.NetworkMember) bool {
return false
}
// SelectAllPolicy selects all members given
var SelectAllPolicy = func(discovery.NetworkMember) bool {
return true
}
// CombineRoutingFilters returns the logical AND of given routing filters
func CombineRoutingFilters(filters ...RoutingFilter) RoutingFilter {
return func(member discovery.NetworkMember) bool {
for _, filter := range filters {
if !filter(member) {
return false
}
}
return true
}
}
// SelectPeers returns a slice of peers that match the routing filter
func SelectPeers(k int, peerPool []discovery.NetworkMember, filter RoutingFilter) []*comm.RemotePeer {
var res []*comm.RemotePeer
rand.Seed(int64(util.RandomUInt64()))
// Iterate over the possible candidates in random order
for _, index := range rand.Perm(len(peerPool)) {
// If we collected K peers, we can stop the iteration.
if len(res) == k {
break
}
peer := peerPool[index]
// For each one, check if it is a worthy candidate to be selected
if !filter(peer) {
continue
}
p := &comm.RemotePeer{PKIID: peer.PKIid, Endpoint: peer.PreferredEndpoint()}
res = append(res, p)
}
return res
}
// First returns the first peer that matches the given filter
func First(peerPool []discovery.NetworkMember, filter RoutingFilter) *comm.RemotePeer {
for _, p := range peerPool {
if filter(p) {
return &comm.RemotePeer{PKIID: p.PKIid, Endpoint: p.PreferredEndpoint()}
}
}
return nil
}
// AnyMatch filters out peers that don't match any of the given filters
func AnyMatch(peerPool []discovery.NetworkMember, filters ...RoutingFilter) []discovery.NetworkMember {
var res []discovery.NetworkMember
for _, peer := range peerPool {
for _, matches := range filters {
if matches(peer) {
res = append(res, peer)
break
}
}
}
return res
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v1.4.0

搜索帮助