go-zero/zrpc/internal/resolver/discovbuilder.go

42 lines
860 B
Go
Raw Normal View History

2020-08-18 18:36:44 +08:00
package resolver
import (
"strings"
"github.com/tal-tech/go-zero/core/discov"
"google.golang.org/grpc/resolver"
)
type discovBuilder struct{}
func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
resolver.Resolver, error) {
2020-08-20 15:35:13 +08:00
hosts := strings.FieldsFunc(target.Authority, func(r rune) bool {
2020-08-25 18:36:30 +08:00
return r == EndpointSepChar
2020-08-20 15:35:13 +08:00
})
2020-08-18 18:36:44 +08:00
sub, err := discov.NewSubscriber(hosts, target.Endpoint)
if err != nil {
return nil, err
}
update := func() {
var addrs []resolver.Address
for _, val := range subset(sub.Values(), subsetSize) {
addrs = append(addrs, resolver.Address{
Addr: val,
})
}
cc.UpdateState(resolver.State{
Addresses: addrs,
})
}
sub.AddListener(update)
update()
return &nopResolver{cc: cc}, nil
}
func (d *discovBuilder) Scheme() string {
return DiscovScheme
}