diff --git a/transport/grpc/dial.go b/transport/grpc/dial.go index a6630a0e440..19fee4e00b3 100644 --- a/transport/grpc/dial.go +++ b/transport/grpc/dial.go @@ -552,6 +552,16 @@ func processAndValidateOpts(opts []option.ClientOption) (*internal.DialSettings, return &o, nil } +// UserConfiguredGRPCConnPoolSize processes the client options and returns the configured +// gRPC connection pool size. It returns an error if the provided options are invalid. +func UserConfiguredGRPCConnPoolSize(opts ...option.ClientOption) (int, error) { + o, err := processAndValidateOpts(opts) + if err != nil { + return 0, err + } + return o.GRPCConnPoolSize, nil +} + type connPoolOption struct{ ConnPool } // WithConnPool returns a ClientOption that specifies the ConnPool diff --git a/transport/grpc/dial_test.go b/transport/grpc/dial_test.go index 63b0a1901f4..47abb3255d2 100644 --- a/transport/grpc/dial_test.go +++ b/transport/grpc/dial_test.go @@ -16,6 +16,7 @@ import ( "github.com/google/go-cmp/cmp" "golang.org/x/oauth2/google" "google.golang.org/api/internal" + "google.golang.org/api/option" "google.golang.org/grpc" ) @@ -258,3 +259,43 @@ func TestGRPCAPIKey_GetRequestMetadata(t *testing.T) { } } } + +func TestConfiguredGRPCConnPoolSize(t *testing.T) { + tests := []struct { + name string + opts []option.ClientOption + wantSize int + wantErr bool + }{ + { + name: "No options", + opts: []option.ClientOption{}, + wantSize: 0, + wantErr: false, + }, + { + name: "WithGRPCConnectionPool(5)", + opts: []option.ClientOption{option.WithGRPCConnectionPool(5)}, + wantSize: 5, + wantErr: false, + }, + { + name: "WithGRPCConnectionPool(0)", + opts: []option.ClientOption{option.WithGRPCConnectionPool(0)}, + wantSize: 0, + wantErr: false, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + gotSize, err := UserConfiguredGRPCConnPoolSize(tc.opts...) + if (err != nil) != tc.wantErr { + t.Errorf("ConfiguredGRPCConnPoolSize() returned error: %v, wantErr: %v", err, tc.wantErr) + } + if !tc.wantErr && gotSize != tc.wantSize { + t.Errorf("ConfiguredGRPCConnPoolSize() = %d, want %d", gotSize, tc.wantSize) + } + }) + } +}