代码拉取完成,页面将自动刷新
package loadbalance
import (
"gitee.com/mosache/YFrame/micro/filter"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/resolver"
)
type Builder struct {
Filter filter.Filter
picker PickerFunc
}
type PickerFunc func(nodes []*NodeInfo) balancer.Picker
type BalancerBuildOption func(b *Builder)
func BuilderWithPickerFunc(pickerFunc PickerFunc) BalancerBuildOption {
return func(b *Builder) {
b.picker = pickerFunc
}
}
func BuilderWithFilter(f filter.Filter) BalancerBuildOption {
return func(b *Builder) {
b.Filter = f
}
}
func NewBalancerBuilder(opts ...BalancerBuildOption) *Builder {
builder := &Builder{
Filter: func(info resolver.Address) bool {
return true
}, picker: func(nodes []*NodeInfo) balancer.Picker {
return Picker{nodes}
}}
for _, opt := range opts {
opt(builder)
}
return builder
}
func (b *Builder) Build(info base.PickerBuildInfo) balancer.Picker {
nodes := make([]*NodeInfo, 0, len(info.ReadySCs))
for k, v := range info.ReadySCs {
if b.Filter(v.Address) {
nodes = append(nodes, &NodeInfo{
SubConn: k,
})
}
}
return b.picker(nodes)
}
type Picker struct {
Nodes []*NodeInfo
}
func (p Picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
}
type NodeInfo struct {
SubConn balancer.SubConn
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。