1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
connector.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-10-22 01:06 . rpc ,排序权重
package grpc
import (
"gitee.com/h79/goutils/common/attributes"
"gitee.com/h79/goutils/discovery/resolver/builder"
grpcAttributes "google.golang.org/grpc/attributes"
grpcResolver "google.golang.org/grpc/resolver"
"sort"
)
// connector
// 一个与grpc桥接的实现
type connector struct {
cc grpcResolver.ClientConn
}
// UpdateState builder.Connector interface
func (cr *connector) UpdateState(state builder.State) {
if cr.cc == nil {
return
}
s := grpcResolver.State{
ServiceConfig: nil,
Addresses: toGrpcAddress(state.Addresses),
Attributes: toGrpcAttribute(state.Attributes),
}
_ = cr.cc.UpdateState(s)
}
// ReportError resolver.Connector interface
func (cr *connector) ReportError(er error) {
if cr.cc == nil {
return
}
cr.cc.ReportError(er)
}
func toGrpcAttribute(attrs *attributes.Attributes) *grpcAttributes.Attributes {
if attrs == nil {
return nil
}
var attr *grpcAttributes.Attributes
attrs.For(func(key interface{}, val interface{}) {
attr = attr.WithValue(key, val)
})
return attr
}
func toGrpcAddress(addr builder.Addresses) []grpcResolver.Address {
sort.Sort(addr)
grpcAddress := make([]grpcResolver.Address, 0)
for i := range addr {
aa := grpcResolver.Address{
Addr: addr[i].Addr,
ServerName: addr[i].ServerName,
Attributes: toGrpcAttribute(addr[i].Attributes),
}
grpcAddress = append(grpcAddress, aa)
}
return grpcAddress
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.8.99

搜索帮助