2 Star 1 Fork 1

mosache/YFrame

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
types.go 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
ヤ沒脩袮兲︶ 提交于 2023-09-07 18:31 . temp
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mosache/YFrame.git
git@gitee.com:mosache/YFrame.git
mosache
YFrame
YFrame
v0.1.94

搜索帮助