@@ -36,13 +36,16 @@ var timeoutDialerOption grpc.DialOption
36
36
// Dial returns a GRPC connection for use communicating with a Google cloud
37
37
// service, configured with the given ClientOptions.
38
38
func 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
42
42
}
43
- if o .GRPCConnPool != 0 {
43
+ if o .GRPCConnPool != nil {
44
+ return o .GRPCConnPool .Conn (), nil
45
+ }
46
+ if o .GRPCConnPoolSize != 0 {
44
47
// 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 ))
46
49
o .GRPCDialOpts = append (o .GRPCDialOpts , grpc .WithBalancer (balancer ))
47
50
}
48
51
return dial (ctx , false , o )
@@ -52,9 +55,9 @@ func Dial(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, e
52
55
// with fake or mock Google cloud service implementations, such as emulators.
53
56
// The connection is configured with the given ClientOptions.
54
57
func 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
58
61
}
59
62
return dial (ctx , true , o )
60
63
}
@@ -67,12 +70,15 @@ func DialInsecure(ctx context.Context, opts ...option.ClientOption) (*grpc.Clien
67
70
//
68
71
// 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.
69
72
func 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
73
76
}
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.
76
82
77
83
if poolSize == 0 || poolSize == 1 {
78
84
// 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
95
101
return pool , nil
96
102
}
97
103
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 ) {
102
105
if o .HTTPClient != nil {
103
106
return nil , errors .New ("unsupported HTTP client specified" )
104
107
}
@@ -112,7 +115,7 @@ func dial(ctx context.Context, insecure bool, o internal.DialSettings) (*grpc.Cl
112
115
if o .APIKey != "" {
113
116
log .Print ("API keys are not supported for gRPC APIs. Remove the WithAPIKey option from your client-creating call." )
114
117
}
115
- creds , err := internal .Creds (ctx , & o )
118
+ creds , err := internal .Creds (ctx , o )
116
119
if err != nil {
117
120
return nil , err
118
121
}
@@ -180,7 +183,7 @@ func dial(ctx context.Context, insecure bool, o internal.DialSettings) (*grpc.Cl
180
183
return grpc .DialContext (ctx , o .Endpoint , grpcOpts ... )
181
184
}
182
185
183
- func addOCStatsHandler (opts []grpc.DialOption , settings internal.DialSettings ) []grpc.DialOption {
186
+ func addOCStatsHandler (opts []grpc.DialOption , settings * internal.DialSettings ) []grpc.DialOption {
184
187
if settings .TelemetryDisabled {
185
188
return opts
186
189
}
@@ -255,3 +258,30 @@ func isDirectPathEnabled(endpoint string) bool {
255
258
}
256
259
return false
257
260
}
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