@@ -36,13 +36,16 @@ var timeoutDialerOption grpc.DialOption
3636// Dial returns a GRPC connection for use communicating with a Google cloud
3737// service, configured with the given ClientOptions.
3838func Dial (ctx context.Context , opts ... option.ClientOption ) (* grpc.ClientConn , error ) {
39- var o internal. DialSettings
40- for _ , opt := range opts {
41- opt . Apply ( & o )
39+ o , err := processAndValidateOpts ( opts )
40+ if err != nil {
41+ return nil , err
4242 }
43- if o .GRPCConnPool != 0 {
43+ if o .GRPCConnPool != nil {
44+ return o .GRPCConnPool .Conn (), nil
45+ }
46+ if o .GRPCConnPoolSize != 0 {
4447 // NOTE(cbro): RoundRobin and WithBalancer are deprecated and we need to remove usages of it.
45- balancer := grpc .RoundRobin (internal .NewPoolResolver (o .GRPCConnPool , & o ))
48+ balancer := grpc .RoundRobin (internal .NewPoolResolver (o .GRPCConnPoolSize , o ))
4649 o .GRPCDialOpts = append (o .GRPCDialOpts , grpc .WithBalancer (balancer ))
4750 }
4851 return dial (ctx , false , o )
@@ -52,9 +55,9 @@ func Dial(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, e
5255// with fake or mock Google cloud service implementations, such as emulators.
5356// The connection is configured with the given ClientOptions.
5457func DialInsecure (ctx context.Context , opts ... option.ClientOption ) (* grpc.ClientConn , error ) {
55- var o internal. DialSettings
56- for _ , opt := range opts {
57- opt . Apply ( & o )
58+ o , err := processAndValidateOpts ( opts )
59+ if err != nil {
60+ return nil , err
5861 }
5962 return dial (ctx , true , o )
6063}
@@ -67,12 +70,15 @@ func DialInsecure(ctx context.Context, opts ...option.ClientOption) (*grpc.Clien
6770//
6871// This API is subject to change as we further refine requirements. It will go away if gRPC stubs accept an interface instead of the concrete ClientConn type. See https://github.com/grpc/grpc-go/issues/1287.
6972func DialPool (ctx context.Context , opts ... option.ClientOption ) (ConnPool , error ) {
70- var o internal. DialSettings
71- for _ , opt := range opts {
72- opt . Apply ( & o )
73+ o , err := processAndValidateOpts ( opts )
74+ if err != nil {
75+ return nil , err
7376 }
74- poolSize := o .GRPCConnPool
75- o .GRPCConnPool = 0 // we don't *need* to set this to zero, but it's safe to.
77+ if o .GRPCConnPool != nil {
78+ return o .GRPCConnPool , nil
79+ }
80+ poolSize := o .GRPCConnPoolSize
81+ o .GRPCConnPoolSize = 0 // we don't *need* to set this to zero, but it's safe to.
7682
7783 if poolSize == 0 || poolSize == 1 {
7884 // Fast path for common case for a connection pool with a single connection.
@@ -95,10 +101,7 @@ func DialPool(ctx context.Context, opts ...option.ClientOption) (ConnPool, error
95101 return pool , nil
96102}
97103
98- func dial (ctx context.Context , insecure bool , o internal.DialSettings ) (* grpc.ClientConn , error ) {
99- if err := o .Validate (); err != nil {
100- return nil , err
101- }
104+ func dial (ctx context.Context , insecure bool , o * internal.DialSettings ) (* grpc.ClientConn , error ) {
102105 if o .HTTPClient != nil {
103106 return nil , errors .New ("unsupported HTTP client specified" )
104107 }
@@ -112,7 +115,7 @@ func dial(ctx context.Context, insecure bool, o internal.DialSettings) (*grpc.Cl
112115 if o .APIKey != "" {
113116 log .Print ("API keys are not supported for gRPC APIs. Remove the WithAPIKey option from your client-creating call." )
114117 }
115- creds , err := internal .Creds (ctx , & o )
118+ creds , err := internal .Creds (ctx , o )
116119 if err != nil {
117120 return nil , err
118121 }
@@ -180,7 +183,7 @@ func dial(ctx context.Context, insecure bool, o internal.DialSettings) (*grpc.Cl
180183 return grpc .DialContext (ctx , o .Endpoint , grpcOpts ... )
181184}
182185
183- func addOCStatsHandler (opts []grpc.DialOption , settings internal.DialSettings ) []grpc.DialOption {
186+ func addOCStatsHandler (opts []grpc.DialOption , settings * internal.DialSettings ) []grpc.DialOption {
184187 if settings .TelemetryDisabled {
185188 return opts
186189 }
@@ -255,3 +258,30 @@ func isDirectPathEnabled(endpoint string) bool {
255258 }
256259 return false
257260}
261+
262+ func processAndValidateOpts (opts []option.ClientOption ) (* internal.DialSettings , error ) {
263+ var o internal.DialSettings
264+ for _ , opt := range opts {
265+ opt .Apply (& o )
266+ }
267+ if err := o .Validate (); err != nil {
268+ return nil , err
269+ }
270+ return & o , nil
271+ }
272+
273+ type connPoolOption struct { ConnPool }
274+
275+ // WithConnPool returns a ClientOption that specifies the ConnPool
276+ // connection to use as the basis of communications.
277+ //
278+ // This is only to be used by Google client libraries internally, for example
279+ // when creating a longrunning API client that shares the same connection pool
280+ // as a service client.
281+ func WithConnPool (p ConnPool ) option.ClientOption {
282+ return connPoolOption {p }
283+ }
284+
285+ func (o connPoolOption ) Apply (s * internal.DialSettings ) {
286+ s .GRPCConnPool = o .ConnPool
287+ }
0 commit comments