From 456b395860dab32612fef1110685d14a8b4092ce Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 25 Aug 2020 18:36:30 +0800 Subject: [PATCH] use predefined endpoint separator --- rpcx/internal/resolver/directbuilder.go | 2 +- rpcx/internal/resolver/discovbuilder.go | 2 +- rpcx/internal/resolver/resolver.go | 19 ++++++++++++------- rpcx/internal/target.go | 8 ++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/rpcx/internal/resolver/directbuilder.go b/rpcx/internal/resolver/directbuilder.go index 2db89d20..756f9cc2 100644 --- a/rpcx/internal/resolver/directbuilder.go +++ b/rpcx/internal/resolver/directbuilder.go @@ -12,7 +12,7 @@ func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, op resolver.Resolver, error) { var addrs []resolver.Address endpoints := strings.FieldsFunc(target.Endpoint, func(r rune) bool { - return r == EndpointSep + return r == EndpointSepChar }) for _, val := range subset(endpoints, subsetSize) { diff --git a/rpcx/internal/resolver/discovbuilder.go b/rpcx/internal/resolver/discovbuilder.go index 103e9419..5f53f35e 100644 --- a/rpcx/internal/resolver/discovbuilder.go +++ b/rpcx/internal/resolver/discovbuilder.go @@ -12,7 +12,7 @@ type discovBuilder struct{} func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( resolver.Resolver, error) { hosts := strings.FieldsFunc(target.Authority, func(r rune) bool { - return r == EndpointSep + return r == EndpointSepChar }) sub, err := discov.NewSubscriber(hosts, target.Endpoint) if err != nil { diff --git a/rpcx/internal/resolver/resolver.go b/rpcx/internal/resolver/resolver.go index d1622358..f86009c6 100644 --- a/rpcx/internal/resolver/resolver.go +++ b/rpcx/internal/resolver/resolver.go @@ -1,17 +1,22 @@ package resolver -import "google.golang.org/grpc/resolver" +import ( + "fmt" + + "google.golang.org/grpc/resolver" +) const ( - DirectScheme = "direct" - DiscovScheme = "discov" - EndpointSep = ',' - subsetSize = 32 + DirectScheme = "direct" + DiscovScheme = "discov" + EndpointSepChar = ',' + subsetSize = 32 ) var ( - dirBuilder directBuilder - disBuilder discovBuilder + EndpointSep = fmt.Sprintf("%c", EndpointSepChar) + dirBuilder directBuilder + disBuilder discovBuilder ) func RegisterResolver() { diff --git a/rpcx/internal/target.go b/rpcx/internal/target.go index 3415646e..0eafcc5d 100644 --- a/rpcx/internal/target.go +++ b/rpcx/internal/target.go @@ -8,11 +8,11 @@ import ( ) func BuildDirectTarget(endpoints []string) string { - return fmt.Sprintf("%s:///%s", resolver.DirectScheme, strings.Join( - endpoints, fmt.Sprintf("%c", resolver.EndpointSep))) + return fmt.Sprintf("%s:///%s", resolver.DirectScheme, + strings.Join(endpoints, resolver.EndpointSep)) } func BuildDiscovTarget(endpoints []string, key string) string { - return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme, strings.Join( - endpoints, fmt.Sprintf("%c", resolver.EndpointSep)), key) + return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme, + strings.Join(endpoints, resolver.EndpointSep), key) }