go-zero/rpcx/internal/resolver/resolver.go

59 lines
1.0 KiB
Go
Raw Normal View History

2020-07-30 20:09:45 +08:00
package resolver
2020-07-31 17:33:08 +08:00
import (
"zero/core/discov"
"google.golang.org/grpc/resolver"
)
2020-07-30 20:09:45 +08:00
2020-08-04 11:59:39 +08:00
const discovScheme = "discov"
type discovBuilder struct {
etcd discov.EtcdConf
2020-07-31 17:33:08 +08:00
}
2020-08-04 11:59:39 +08:00
func (b *discovBuilder) Scheme() string {
return discovScheme
}
func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
2020-07-31 17:33:08 +08:00
resolver.Resolver, error) {
2020-08-04 11:59:39 +08:00
sub, err := discov.NewSubscriber(b.etcd.Hosts, b.etcd.Key)
2020-07-31 17:33:08 +08:00
if err != nil {
return nil, err
}
sub.AddListener(func() {
vals := sub.Values()
var addrs []resolver.Address
for _, val := range vals {
addrs = append(addrs, resolver.Address{
Addr: val,
})
}
2020-08-04 11:59:39 +08:00
cc.UpdateState(resolver.State{
2020-07-31 17:33:08 +08:00
Addresses: addrs,
})
})
2020-08-04 11:59:39 +08:00
return &discovResolver{
cc: cc,
}, nil
2020-07-31 17:33:08 +08:00
}
2020-08-04 11:59:39 +08:00
type discovResolver struct {
cc resolver.ClientConn
2020-07-31 17:33:08 +08:00
}
2020-08-04 11:59:39 +08:00
func (r *discovResolver) Close() {
2020-07-30 20:09:45 +08:00
}
2020-08-04 11:59:39 +08:00
func (r *discovResolver) ResolveNow(options resolver.ResolveNowOptions) {
2020-07-30 20:09:45 +08:00
}
2020-08-04 11:59:39 +08:00
func RegisterResolver(etcd discov.EtcdConf) {
resolver.Register(&discovBuilder{
etcd: etcd,
2020-07-31 17:33:08 +08:00
})
2020-07-30 20:09:45 +08:00
}