Skip to content

Commit

Permalink
Merge pull request #67 from GoogleCloudPlatform/gcpconfig
Browse files Browse the repository at this point in the history
feat: expose GCPConfig
  • Loading branch information
nimf authored Jan 23, 2024
2 parents aba71ce + 124e9e3 commit 07c3489
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion grpcgcp/gcp_multiendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/grpclog"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"

pb "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp/grpc_gcp"
)
Expand Down Expand Up @@ -158,6 +159,10 @@ func (gme *GCPMultiEndpoint) Close() error {
return errs.Combine()
}

func (gme *GCPMultiEndpoint) GCPConfig() *pb.ApiConfig {
return proto.Clone(gme.gcpConfig).(*pb.ApiConfig)
}

// GCPMultiEndpointOptions holds options to construct a MultiEndpoints-enabled gRPC client
// connection.
type GCPMultiEndpointOptions struct {
Expand Down Expand Up @@ -187,7 +192,7 @@ func NewGcpMultiEndpoint(meOpts *GCPMultiEndpointOptions, opts ...grpc.DialOptio
pools: make(map[string]*monitoredConn),
defaultName: meOpts.Default,
opts: o,
gcpConfig: meOpts.GRPCgcpConfig,
gcpConfig: proto.Clone(meOpts.GRPCgcpConfig).(*pb.ApiConfig),
dialFunc: meOpts.DialFunc,
log: NewGCPLogger(compLogger, fmt.Sprintf("[GCPMultiEndpoint #%d]", atomic.AddUint32(&gmeCounter, 1))),
}
Expand Down
55 changes: 55 additions & 0 deletions grpcgcp/test_grpc/gcp_multiendpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import (

"github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp"
"github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp/multiendpoint"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"

configpb "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp/grpc_gcp"
pb "github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp/test_grpc/helloworld/helloworld"
Expand Down Expand Up @@ -762,3 +765,55 @@ func TestGcpMultiEndpointDialFunc(t *testing.T) {
t.Fatalf("provided dial function was called for %q endpoint %v times, want %v times", fEndpoint, got, want)
}
}

func TestGCPMultiEndpointGCPConfig(t *testing.T) {

lEndpoint, fEndpoint := "localhost:50051", "127.0.0.3:50051"

defaultME, followerME := "default", "follower"

apiCfg := &configpb.ApiConfig{
ChannelPool: &configpb.ChannelPoolConfig{
MinSize: 2,
MaxSize: 3,
},
}

dialUsedFor := make(map[string]*atomic.Int32)
dialUsedFor[lEndpoint] = &atomic.Int32{}
dialUsedFor[fEndpoint] = &atomic.Int32{}

conn, err := grpcgcp.NewGcpMultiEndpoint(
&grpcgcp.GCPMultiEndpointOptions{
GRPCgcpConfig: apiCfg,
MultiEndpoints: map[string]*multiendpoint.MultiEndpointOptions{
defaultME: {
Endpoints: []string{lEndpoint, fEndpoint},
},
followerME: {
Endpoints: []string{fEndpoint, lEndpoint},
},
},
Default: defaultME,
DialFunc: func(ctx context.Context, target string, dopts ...grpc.DialOption) (*grpc.ClientConn, error) {
dialUsedFor[target].Add(1)
return grpc.DialContext(ctx, target, dopts...)
},
},
grpc.WithInsecure(),
)

if err != nil {
t.Fatalf("NewMultiEndpointConn returns unexpected error: %v", err)
}

defer conn.Close()

wantCfg := proto.Clone(apiCfg)
// This change of the initial config should not be reflected in the following comparison.
apiCfg.GetChannelPool().MaxSize = 5

if diff := cmp.Diff(wantCfg, conn.GCPConfig(), protocmp.Transform()); diff != "" {
t.Fatalf("conn.GCPConfig() returned unexpected difference in protobuf messages (-want +got):\n%s", diff)
}
}

0 comments on commit 07c3489

Please sign in to comment.