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

35 lines
699 B
Go
Raw Normal View History

package internal
2020-08-18 18:36:44 +08:00
import (
"strings"
"google.golang.org/grpc/resolver"
)
type directBuilder struct{}
func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
resolver.Resolver, error) {
var addrs []resolver.Address
2020-08-20 15:35:13 +08:00
endpoints := strings.FieldsFunc(target.Endpoint, 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
for _, val := range subset(endpoints, subsetSize) {
addrs = append(addrs, resolver.Address{
Addr: val,
})
}
if err := cc.UpdateState(resolver.State{
2020-08-18 18:36:44 +08:00
Addresses: addrs,
}); err != nil {
return nil, err
}
2020-08-18 18:36:44 +08:00
return &nopResolver{cc: cc}, nil
}
func (d *directBuilder) Scheme() string {
return DirectScheme
}