1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
builder.go 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-08-28 17:55 . log
package grpc
import (
"fmt"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/discovery/resolver/builder"
grpcresolver "google.golang.org/grpc/resolver"
)
// priBuilder 为grpc桥接
type priBuilder struct {
myBuilder builder.Builder
}
func Register(builder builder.Builder) {
b := &priBuilder{
myBuilder: builder,
}
// 注册到GRPC中
grpcresolver.Register(b)
}
// Build
// grpc interface
func (cb *priBuilder) Build(target grpcresolver.Target,
cc grpcresolver.ClientConn, opts grpcresolver.BuildOptions) (grpcresolver.Resolver, error) {
if cc == nil {
return nil, fmt.Errorf("GRPC: client connect is nil")
}
logger.Info("GRPC: Build target: %+v", target)
// - "dns://some_authority/foo.bar"
// Target{Scheme: "dns", Authority: "some_authority", Endpoint: "foo.bar"}
// - "foo.bar"
// Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "foo.bar"}
// - "unknown_scheme://authority/endpoint"
// Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "unknown_scheme://authority/endpoint"}
endpoints := target.URL.Path
if len(endpoints) <= 0 {
endpoints = target.URL.Opaque
}
tar := builder.Target{
Scheme: target.URL.Scheme,
Authority: target.URL.Host,
Endpoint: endpoints,
}
cr := &connector{
cc: cc,
}
rr, err := cb.myBuilder.Build(tar, cr)
if err != nil {
return nil, err
}
return &priResolver{myResolver: rr}, nil
}
// Scheme
// grpc interface
func (cb *priBuilder) Scheme() string {
return cb.myBuilder.Scheme()
}
type priResolver struct {
myResolver builder.Resolver
}
func (cr *priResolver) ResolveNow(grpcresolver.ResolveNowOptions) {
cr.myResolver.ResolveNow()
}
func (cr *priResolver) Close() {
cr.myResolver.Close()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.52

搜索帮助