You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/rpcx/internal/resolver/resolver.go

70 lines
1.2 KiB
Go

4 years ago
package resolver
4 years ago
import (
"fmt"
"strings"
4 years ago
"zero/core/discov"
"google.golang.org/grpc/resolver"
)
4 years ago
const (
DiscovScheme = "discov"
EndpointSep = ","
subsetSize = 36
)
var builder discovBuilder
type discovBuilder struct{}
4 years ago
func (b *discovBuilder) Scheme() string {
return DiscovScheme
}
func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
4 years ago
resolver.Resolver, error) {
if target.Scheme != DiscovScheme {
return nil, fmt.Errorf("bad scheme: %s", target.Scheme)
}
hosts := strings.Split(target.Authority, EndpointSep)
sub, err := discov.NewSubscriber(hosts, target.Endpoint)
4 years ago
if err != nil {
return nil, err
}
update := func() {
4 years ago
var addrs []resolver.Address
for _, val := range subset(sub.Values(), subsetSize) {
4 years ago
addrs = append(addrs, resolver.Address{
Addr: val,
})
}
cc.UpdateState(resolver.State{
4 years ago
Addresses: addrs,
})
}
sub.AddListener(update)
update()
4 years ago
return &discovResolver{
cc: cc,
}, nil
4 years ago
}
type discovResolver struct {
cc resolver.ClientConn
4 years ago
}
func (r *discovResolver) Close() {
4 years ago
}
func (r *discovResolver) ResolveNow(options resolver.ResolveNowOptions) {
4 years ago
}
func RegisterResolver() {
resolver.Register(&builder)
4 years ago
}