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.
40 lines
814 B
Go
40 lines
814 B
Go
4 years ago
|
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) {
|
||
|
hosts := strings.Split(target.Authority, EndpointSep)
|
||
|
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
|
||
|
}
|