diff --git a/daemon.go b/daemon.go index 97602075..6a4cf3c0 100644 --- a/daemon.go +++ b/daemon.go @@ -41,7 +41,6 @@ import ( "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/keepalive" - "google.golang.org/grpc/resolver" "google.golang.org/protobuf/encoding/protojson" ) @@ -433,7 +432,7 @@ func (s *Daemon) Client() (V1Client, error) { conn, err := grpc.DialContext(context.Background(), fmt.Sprintf("static:///%s", s.PeerInfo.GRPCAddress), - grpc.WithResolvers(newStaticBuilder()), + grpc.WithResolvers(NewStaticBuilder()), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err @@ -482,41 +481,3 @@ func WaitForConnect(ctx context.Context, addresses []string) error { } return nil } - -type staticBuilder struct{} - -var _ resolver.Builder = (*staticBuilder)(nil) - -func (sb *staticBuilder) Scheme() string { - return "static" -} - -func (sb *staticBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) { - var resolverAddrs []resolver.Address - for _, address := range strings.Split(target.Endpoint(), ",") { - resolverAddrs = append(resolverAddrs, resolver.Address{ - Addr: address, - ServerName: address, - }) - } - if err := cc.UpdateState(resolver.State{Addresses: resolverAddrs}); err != nil { - return nil, err - } - return &staticResolver{cc: cc}, nil -} - -// newStaticBuilder returns a builder which returns a staticResolver that tells GRPC -// to connect a specific peer in the cluster. -func newStaticBuilder() resolver.Builder { - return &staticBuilder{} -} - -type staticResolver struct { - cc resolver.ClientConn -} - -func (sr *staticResolver) ResolveNow(_ resolver.ResolveNowOptions) {} - -func (sr *staticResolver) Close() {} - -var _ resolver.Resolver = (*staticResolver)(nil) diff --git a/functional_test.go b/functional_test.go index 53b69f06..246e837e 100644 --- a/functional_test.go +++ b/functional_test.go @@ -39,7 +39,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "google.golang.org/grpc/resolver" json "google.golang.org/protobuf/encoding/protojson" ) @@ -1041,7 +1040,7 @@ func TestGlobalRateLimitsWithLoadBalancing(t *testing.T) { // Connect to owner and non-owner peers in round robin. dialOpts := []grpc.DialOption{ - grpc.WithResolvers(newStaticBuilder()), + grpc.WithResolvers(guber.NewStaticBuilder()), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`), } @@ -1837,46 +1836,3 @@ func waitForBroadcast(timeout clock.Duration, d *guber.Daemon, expect int) error } } } - -// staticBuilder implements the `resolver.Builder` interface. -type staticBuilder struct{} - -func newStaticBuilder() resolver.Builder { - return &staticBuilder{} -} - -func (sb *staticBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) { - var resolverAddrs []resolver.Address - for _, address := range strings.Split(target.Endpoint(), ",") { - resolverAddrs = append(resolverAddrs, resolver.Address{ - Addr: address, - ServerName: address, - }) - - } - r, err := newStaticResolver(cc, resolverAddrs) - if err != nil { - return nil, err - } - return r, nil -} - -func (sb *staticBuilder) Scheme() string { - return "static" -} - -type staticResolver struct { - cc resolver.ClientConn -} - -func newStaticResolver(cc resolver.ClientConn, addresses []resolver.Address) (resolver.Resolver, error) { - err := cc.UpdateState(resolver.State{Addresses: addresses}) - if err != nil { - return nil, err - } - return &staticResolver{cc: cc}, nil -} - -func (sr *staticResolver) ResolveNow(_ resolver.ResolveNowOptions) {} - -func (sr *staticResolver) Close() {} diff --git a/staticbuilder.go b/staticbuilder.go new file mode 100644 index 00000000..9bbd8325 --- /dev/null +++ b/staticbuilder.go @@ -0,0 +1,45 @@ +package gubernator + +import ( + "strings" + + "google.golang.org/grpc/resolver" +) + +type staticBuilder struct{} + +var _ resolver.Builder = (*staticBuilder)(nil) + +func (sb *staticBuilder) Scheme() string { + return "static" +} + +func (sb *staticBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) { + var resolverAddrs []resolver.Address + for _, address := range strings.Split(target.Endpoint(), ",") { + resolverAddrs = append(resolverAddrs, resolver.Address{ + Addr: address, + ServerName: address, + }) + } + if err := cc.UpdateState(resolver.State{Addresses: resolverAddrs}); err != nil { + return nil, err + } + return &staticResolver{cc: cc}, nil +} + +// NewStaticBuilder returns a builder which returns a staticResolver that tells GRPC +// to connect a specific peer in the cluster. +func NewStaticBuilder() resolver.Builder { + return &staticBuilder{} +} + +type staticResolver struct { + cc resolver.ClientConn +} + +func (sr *staticResolver) ResolveNow(_ resolver.ResolveNowOptions) {} + +func (sr *staticResolver) Close() {} + +var _ resolver.Resolver = (*staticResolver)(nil)