use predefined endpoint separator

master v1.0.6
kevin 4 years ago
parent f3c367a323
commit 456b395860

@ -12,7 +12,7 @@ func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, op
resolver.Resolver, error) { resolver.Resolver, error) {
var addrs []resolver.Address var addrs []resolver.Address
endpoints := strings.FieldsFunc(target.Endpoint, func(r rune) bool { endpoints := strings.FieldsFunc(target.Endpoint, func(r rune) bool {
return r == EndpointSep return r == EndpointSepChar
}) })
for _, val := range subset(endpoints, subsetSize) { for _, val := range subset(endpoints, subsetSize) {

@ -12,7 +12,7 @@ type discovBuilder struct{}
func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
resolver.Resolver, error) { resolver.Resolver, error) {
hosts := strings.FieldsFunc(target.Authority, func(r rune) bool { hosts := strings.FieldsFunc(target.Authority, func(r rune) bool {
return r == EndpointSep return r == EndpointSepChar
}) })
sub, err := discov.NewSubscriber(hosts, target.Endpoint) sub, err := discov.NewSubscriber(hosts, target.Endpoint)
if err != nil { if err != nil {

@ -1,17 +1,22 @@
package resolver package resolver
import "google.golang.org/grpc/resolver" import (
"fmt"
"google.golang.org/grpc/resolver"
)
const ( const (
DirectScheme = "direct" DirectScheme = "direct"
DiscovScheme = "discov" DiscovScheme = "discov"
EndpointSep = ',' EndpointSepChar = ','
subsetSize = 32 subsetSize = 32
) )
var ( var (
dirBuilder directBuilder EndpointSep = fmt.Sprintf("%c", EndpointSepChar)
disBuilder discovBuilder dirBuilder directBuilder
disBuilder discovBuilder
) )
func RegisterResolver() { func RegisterResolver() {

@ -8,11 +8,11 @@ import (
) )
func BuildDirectTarget(endpoints []string) string { func BuildDirectTarget(endpoints []string) string {
return fmt.Sprintf("%s:///%s", resolver.DirectScheme, strings.Join( return fmt.Sprintf("%s:///%s", resolver.DirectScheme,
endpoints, fmt.Sprintf("%c", resolver.EndpointSep))) strings.Join(endpoints, resolver.EndpointSep))
} }
func BuildDiscovTarget(endpoints []string, key string) string { func BuildDiscovTarget(endpoints []string, key string) string {
return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme, strings.Join( return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme,
endpoints, fmt.Sprintf("%c", resolver.EndpointSep)), key) strings.Join(endpoints, resolver.EndpointSep), key)
} }

Loading…
Cancel
Save