blob: 95a7459b02f658800fbaa850b41e31f633d20938 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
2 *
3 * Copyright 2014 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package grpc
20
21import (
22 "context"
23 "errors"
24 "fmt"
25 "math"
khenaidoo5cb0d402021-12-08 14:09:16 -050026 "net/url"
khenaidoo5fc5cea2021-08-11 17:39:16 -040027 "strings"
28 "sync"
29 "sync/atomic"
30 "time"
31
32 "google.golang.org/grpc/balancer"
33 "google.golang.org/grpc/balancer/base"
34 "google.golang.org/grpc/codes"
35 "google.golang.org/grpc/connectivity"
36 "google.golang.org/grpc/credentials"
37 "google.golang.org/grpc/internal/backoff"
38 "google.golang.org/grpc/internal/channelz"
39 "google.golang.org/grpc/internal/grpcsync"
khenaidoo5fc5cea2021-08-11 17:39:16 -040040 iresolver "google.golang.org/grpc/internal/resolver"
41 "google.golang.org/grpc/internal/transport"
42 "google.golang.org/grpc/keepalive"
43 "google.golang.org/grpc/resolver"
44 "google.golang.org/grpc/serviceconfig"
45 "google.golang.org/grpc/status"
46
47 _ "google.golang.org/grpc/balancer/roundrobin" // To register roundrobin.
48 _ "google.golang.org/grpc/internal/resolver/dns" // To register dns resolver.
49 _ "google.golang.org/grpc/internal/resolver/passthrough" // To register passthrough resolver.
50 _ "google.golang.org/grpc/internal/resolver/unix" // To register unix resolver.
51)
52
53const (
54 // minimum time to give a connection to complete
55 minConnectTimeout = 20 * time.Second
56 // must match grpclbName in grpclb/grpclb.go
57 grpclbName = "grpclb"
58)
59
60var (
61 // ErrClientConnClosing indicates that the operation is illegal because
62 // the ClientConn is closing.
63 //
64 // Deprecated: this error should not be relied upon by users; use the status
65 // code of Canceled instead.
66 ErrClientConnClosing = status.Error(codes.Canceled, "grpc: the client connection is closing")
67 // errConnDrain indicates that the connection starts to be drained and does not accept any new RPCs.
68 errConnDrain = errors.New("grpc: the connection is drained")
69 // errConnClosing indicates that the connection is closing.
70 errConnClosing = errors.New("grpc: the connection is closing")
Akash Kankanala761955c2024-02-21 19:32:20 +053071 // errConnIdling indicates the the connection is being closed as the channel
72 // is moving to an idle mode due to inactivity.
73 errConnIdling = errors.New("grpc: the connection is closing due to channel idleness")
khenaidoo5fc5cea2021-08-11 17:39:16 -040074 // invalidDefaultServiceConfigErrPrefix is used to prefix the json parsing error for the default
75 // service config.
76 invalidDefaultServiceConfigErrPrefix = "grpc: the provided default service config is invalid"
77)
78
79// The following errors are returned from Dial and DialContext
80var (
81 // errNoTransportSecurity indicates that there is no transport security
82 // being set for ClientConn. Users should either set one or explicitly
83 // call WithInsecure DialOption to disable security.
Akash Kankanala761955c2024-02-21 19:32:20 +053084 errNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithTransportCredentials(insecure.NewCredentials()) explicitly or set credentials)")
khenaidoo5fc5cea2021-08-11 17:39:16 -040085 // errTransportCredsAndBundle indicates that creds bundle is used together
86 // with other individual Transport Credentials.
87 errTransportCredsAndBundle = errors.New("grpc: credentials.Bundle may not be used with individual TransportCredentials")
khenaidoo257f3192021-12-15 16:46:37 -050088 // errNoTransportCredsInBundle indicated that the configured creds bundle
89 // returned a transport credentials which was nil.
90 errNoTransportCredsInBundle = errors.New("grpc: credentials.Bundle must return non-nil transport credentials")
91 // errTransportCredentialsMissing indicates that users want to transmit
92 // security information (e.g., OAuth2 token) which requires secure
93 // connection on an insecure connection.
khenaidoo5fc5cea2021-08-11 17:39:16 -040094 errTransportCredentialsMissing = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportCredentials() to set)")
khenaidoo5fc5cea2021-08-11 17:39:16 -040095)
96
97const (
98 defaultClientMaxReceiveMessageSize = 1024 * 1024 * 4
99 defaultClientMaxSendMessageSize = math.MaxInt32
100 // http2IOBufSize specifies the buffer size for sending frames.
101 defaultWriteBufSize = 32 * 1024
102 defaultReadBufSize = 32 * 1024
103)
104
105// Dial creates a client connection to the given target.
106func Dial(target string, opts ...DialOption) (*ClientConn, error) {
107 return DialContext(context.Background(), target, opts...)
108}
109
110type defaultConfigSelector struct {
111 sc *ServiceConfig
112}
113
114func (dcs *defaultConfigSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*iresolver.RPCConfig, error) {
115 return &iresolver.RPCConfig{
116 Context: rpcInfo.Context,
117 MethodConfig: getMethodConfig(dcs.sc, rpcInfo.Method),
118 }, nil
119}
120
121// DialContext creates a client connection to the given target. By default, it's
122// a non-blocking dial (the function won't wait for connections to be
123// established, and connecting happens in the background). To make it a blocking
124// dial, use WithBlock() dial option.
125//
126// In the non-blocking case, the ctx does not act against the connection. It
127// only controls the setup steps.
128//
129// In the blocking case, ctx can be used to cancel or expire the pending
130// connection. Once this function returns, the cancellation and expiration of
131// ctx will be noop. Users should call ClientConn.Close to terminate all the
132// pending operations after this function returns.
133//
134// The target name syntax is defined in
135// https://github.com/grpc/grpc/blob/master/doc/naming.md.
136// e.g. to use dns resolver, a "dns:///" prefix should be applied to the target.
137func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) {
138 cc := &ClientConn{
Akash Kankanala761955c2024-02-21 19:32:20 +0530139 target: target,
140 csMgr: &connectivityStateManager{},
141 conns: make(map[*addrConn]struct{}),
142 dopts: defaultDialOptions(),
143 czData: new(channelzData),
khenaidoo5fc5cea2021-08-11 17:39:16 -0400144 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530145
146 // We start the channel off in idle mode, but kick it out of idle at the end
147 // of this method, instead of waiting for the first RPC. Other gRPC
148 // implementations do wait for the first RPC to kick the channel out of
149 // idle. But doing so would be a major behavior change for our users who are
150 // used to seeing the channel active after Dial.
151 //
152 // Taking this approach of kicking it out of idle at the end of this method
153 // allows us to share the code between channel creation and exiting idle
154 // mode. This will also make it easy for us to switch to starting the
155 // channel off in idle, if at all we ever get to do that.
156 cc.idlenessState = ccIdlenessStateIdle
157
khenaidoo5fc5cea2021-08-11 17:39:16 -0400158 cc.retryThrottler.Store((*retryThrottler)(nil))
159 cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil})
160 cc.ctx, cc.cancel = context.WithCancel(context.Background())
Akash Kankanala761955c2024-02-21 19:32:20 +0530161 cc.exitIdleCond = sync.NewCond(&cc.mu)
162
163 disableGlobalOpts := false
164 for _, opt := range opts {
165 if _, ok := opt.(*disableGlobalDialOptions); ok {
166 disableGlobalOpts = true
167 break
168 }
169 }
170
171 if !disableGlobalOpts {
172 for _, opt := range globalDialOptions {
173 opt.apply(&cc.dopts)
174 }
175 }
khenaidoo5fc5cea2021-08-11 17:39:16 -0400176
177 for _, opt := range opts {
178 opt.apply(&cc.dopts)
179 }
180
181 chainUnaryClientInterceptors(cc)
182 chainStreamClientInterceptors(cc)
183
184 defer func() {
185 if err != nil {
186 cc.Close()
187 }
188 }()
189
Akash Kankanala761955c2024-02-21 19:32:20 +0530190 // Register ClientConn with channelz.
191 cc.channelzRegistration(target)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400192
Akash Kankanala761955c2024-02-21 19:32:20 +0530193 if err := cc.validateTransportCredentials(); err != nil {
194 return nil, err
khenaidoo5fc5cea2021-08-11 17:39:16 -0400195 }
196
197 if cc.dopts.defaultServiceConfigRawJSON != nil {
198 scpr := parseServiceConfig(*cc.dopts.defaultServiceConfigRawJSON)
199 if scpr.Err != nil {
200 return nil, fmt.Errorf("%s: %v", invalidDefaultServiceConfigErrPrefix, scpr.Err)
201 }
202 cc.dopts.defaultServiceConfig, _ = scpr.Config.(*ServiceConfig)
203 }
204 cc.mkp = cc.dopts.copts.KeepaliveParams
205
206 if cc.dopts.copts.UserAgent != "" {
207 cc.dopts.copts.UserAgent += " " + grpcUA
208 } else {
209 cc.dopts.copts.UserAgent = grpcUA
210 }
211
212 if cc.dopts.timeout > 0 {
213 var cancel context.CancelFunc
214 ctx, cancel = context.WithTimeout(ctx, cc.dopts.timeout)
215 defer cancel()
216 }
217 defer func() {
218 select {
219 case <-ctx.Done():
220 switch {
221 case ctx.Err() == err:
222 conn = nil
223 case err == nil || !cc.dopts.returnLastError:
224 conn, err = nil, ctx.Err()
225 default:
226 conn, err = nil, fmt.Errorf("%v: %v", ctx.Err(), err)
227 }
228 default:
229 }
230 }()
231
khenaidoo5fc5cea2021-08-11 17:39:16 -0400232 if cc.dopts.bs == nil {
233 cc.dopts.bs = backoff.DefaultExponential
234 }
235
236 // Determine the resolver to use.
Akash Kankanala761955c2024-02-21 19:32:20 +0530237 if err := cc.parseTargetAndFindResolver(); err != nil {
khenaidoo5cb0d402021-12-08 14:09:16 -0500238 return nil, err
khenaidoo5fc5cea2021-08-11 17:39:16 -0400239 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530240 if err = cc.determineAuthority(); err != nil {
khenaidoo5cb0d402021-12-08 14:09:16 -0500241 return nil, err
khenaidoo5fc5cea2021-08-11 17:39:16 -0400242 }
243
Akash Kankanala761955c2024-02-21 19:32:20 +0530244 if cc.dopts.scChan != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400245 // Blocking wait for the initial service config.
246 select {
247 case sc, ok := <-cc.dopts.scChan:
248 if ok {
249 cc.sc = &sc
250 cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{&sc})
251 }
252 case <-ctx.Done():
253 return nil, ctx.Err()
254 }
255 }
256 if cc.dopts.scChan != nil {
257 go cc.scWatcher()
258 }
259
Akash Kankanala761955c2024-02-21 19:32:20 +0530260 // This creates the name resolver, load balancer, blocking picker etc.
261 if err := cc.exitIdleMode(); err != nil {
262 return nil, err
263 }
264
265 // Configure idleness support with configured idle timeout or default idle
266 // timeout duration. Idleness can be explicitly disabled by the user, by
267 // setting the dial option to 0.
268 cc.idlenessMgr = newIdlenessManager(cc, cc.dopts.idleTimeout)
269
270 // Return early for non-blocking dials.
271 if !cc.dopts.block {
272 return cc, nil
273 }
274
275 // A blocking dial blocks until the clientConn is ready.
276 for {
277 s := cc.GetState()
278 if s == connectivity.Idle {
279 cc.Connect()
280 }
281 if s == connectivity.Ready {
282 return cc, nil
283 } else if cc.dopts.copts.FailOnNonTempDialError && s == connectivity.TransientFailure {
284 if err = cc.connectionError(); err != nil {
285 terr, ok := err.(interface {
286 Temporary() bool
287 })
288 if ok && !terr.Temporary() {
289 return nil, err
290 }
291 }
292 }
293 if !cc.WaitForStateChange(ctx, s) {
294 // ctx got timeout or canceled.
295 if err = cc.connectionError(); err != nil && cc.dopts.returnLastError {
296 return nil, err
297 }
298 return nil, ctx.Err()
299 }
300 }
301}
302
303// addTraceEvent is a helper method to add a trace event on the channel. If the
304// channel is a nested one, the same event is also added on the parent channel.
305func (cc *ClientConn) addTraceEvent(msg string) {
306 ted := &channelz.TraceEventDesc{
307 Desc: fmt.Sprintf("Channel %s", msg),
308 Severity: channelz.CtInfo,
309 }
310 if cc.dopts.channelzParentID != nil {
311 ted.Parent = &channelz.TraceEventDesc{
312 Desc: fmt.Sprintf("Nested channel(id:%d) %s", cc.channelzID.Int(), msg),
313 Severity: channelz.CtInfo,
314 }
315 }
316 channelz.AddTraceEvent(logger, cc.channelzID, 0, ted)
317}
318
319// exitIdleMode moves the channel out of idle mode by recreating the name
320// resolver and load balancer.
321func (cc *ClientConn) exitIdleMode() error {
322 cc.mu.Lock()
323 if cc.conns == nil {
324 cc.mu.Unlock()
325 return errConnClosing
326 }
327 if cc.idlenessState != ccIdlenessStateIdle {
328 cc.mu.Unlock()
329 logger.Info("ClientConn asked to exit idle mode when not in idle mode")
330 return nil
331 }
332
333 defer func() {
334 // When Close() and exitIdleMode() race against each other, one of the
335 // following two can happen:
336 // - Close() wins the race and runs first. exitIdleMode() runs after, and
337 // sees that the ClientConn is already closed and hence returns early.
338 // - exitIdleMode() wins the race and runs first and recreates the balancer
339 // and releases the lock before recreating the resolver. If Close() runs
340 // in this window, it will wait for exitIdleMode to complete.
341 //
342 // We achieve this synchronization using the below condition variable.
343 cc.mu.Lock()
344 cc.idlenessState = ccIdlenessStateActive
345 cc.exitIdleCond.Signal()
346 cc.mu.Unlock()
347 }()
348
349 cc.idlenessState = ccIdlenessStateExitingIdle
350 exitedIdle := false
351 if cc.blockingpicker == nil {
352 cc.blockingpicker = newPickerWrapper()
353 } else {
354 cc.blockingpicker.exitIdleMode()
355 exitedIdle = true
356 }
357
khenaidoo5fc5cea2021-08-11 17:39:16 -0400358 var credsClone credentials.TransportCredentials
359 if creds := cc.dopts.copts.TransportCredentials; creds != nil {
360 credsClone = creds.Clone()
361 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530362 if cc.balancerWrapper == nil {
363 cc.balancerWrapper = newCCBalancerWrapper(cc, balancer.BuildOptions{
364 DialCreds: credsClone,
365 CredsBundle: cc.dopts.copts.CredsBundle,
366 Dialer: cc.dopts.copts.Dialer,
367 Authority: cc.authority,
368 CustomUserAgent: cc.dopts.copts.UserAgent,
369 ChannelzParentID: cc.channelzID,
370 Target: cc.parsedTarget,
371 })
372 } else {
373 cc.balancerWrapper.exitIdleMode()
khenaidoo5fc5cea2021-08-11 17:39:16 -0400374 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530375 cc.firstResolveEvent = grpcsync.NewEvent()
khenaidoo5fc5cea2021-08-11 17:39:16 -0400376 cc.mu.Unlock()
377
Akash Kankanala761955c2024-02-21 19:32:20 +0530378 // This needs to be called without cc.mu because this builds a new resolver
379 // which might update state or report error inline which needs to be handled
380 // by cc.updateResolverState() which also grabs cc.mu.
381 if err := cc.initResolverWrapper(credsClone); err != nil {
382 return err
383 }
384
385 if exitedIdle {
386 cc.addTraceEvent("exiting idle mode")
387 }
388 return nil
389}
390
391// enterIdleMode puts the channel in idle mode, and as part of it shuts down the
392// name resolver, load balancer and any subchannels.
393func (cc *ClientConn) enterIdleMode() error {
394 cc.mu.Lock()
395 if cc.conns == nil {
396 cc.mu.Unlock()
397 return ErrClientConnClosing
398 }
399 if cc.idlenessState != ccIdlenessStateActive {
400 logger.Error("ClientConn asked to enter idle mode when not active")
401 return nil
402 }
403
404 // cc.conns == nil is a proxy for the ClientConn being closed. So, instead
405 // of setting it to nil here, we recreate the map. This also means that we
406 // don't have to do this when exiting idle mode.
407 conns := cc.conns
408 cc.conns = make(map[*addrConn]struct{})
409
410 // TODO: Currently, we close the resolver wrapper upon entering idle mode
411 // and create a new one upon exiting idle mode. This means that the
412 // `cc.resolverWrapper` field would be overwritten everytime we exit idle
413 // mode. While this means that we need to hold `cc.mu` when accessing
414 // `cc.resolverWrapper`, it makes the code simpler in the wrapper. We should
415 // try to do the same for the balancer and picker wrappers too.
416 cc.resolverWrapper.close()
417 cc.blockingpicker.enterIdleMode()
418 cc.balancerWrapper.enterIdleMode()
419 cc.csMgr.updateState(connectivity.Idle)
420 cc.idlenessState = ccIdlenessStateIdle
421 cc.mu.Unlock()
422
423 go func() {
424 cc.addTraceEvent("entering idle mode")
425 for ac := range conns {
426 ac.tearDown(errConnIdling)
427 }
428 }()
429 return nil
430}
431
432// validateTransportCredentials performs a series of checks on the configured
433// transport credentials. It returns a non-nil error if any of these conditions
434// are met:
435// - no transport creds and no creds bundle is configured
436// - both transport creds and creds bundle are configured
437// - creds bundle is configured, but it lacks a transport credentials
438// - insecure transport creds configured alongside call creds that require
439// transport level security
440//
441// If none of the above conditions are met, the configured credentials are
442// deemed valid and a nil error is returned.
443func (cc *ClientConn) validateTransportCredentials() error {
444 if cc.dopts.copts.TransportCredentials == nil && cc.dopts.copts.CredsBundle == nil {
445 return errNoTransportSecurity
446 }
447 if cc.dopts.copts.TransportCredentials != nil && cc.dopts.copts.CredsBundle != nil {
448 return errTransportCredsAndBundle
449 }
450 if cc.dopts.copts.CredsBundle != nil && cc.dopts.copts.CredsBundle.TransportCredentials() == nil {
451 return errNoTransportCredsInBundle
452 }
453 transportCreds := cc.dopts.copts.TransportCredentials
454 if transportCreds == nil {
455 transportCreds = cc.dopts.copts.CredsBundle.TransportCredentials()
456 }
457 if transportCreds.Info().SecurityProtocol == "insecure" {
458 for _, cd := range cc.dopts.copts.PerRPCCredentials {
459 if cd.RequireTransportSecurity() {
460 return errTransportCredentialsMissing
khenaidoo5fc5cea2021-08-11 17:39:16 -0400461 }
462 }
463 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530464 return nil
465}
khenaidoo5fc5cea2021-08-11 17:39:16 -0400466
Akash Kankanala761955c2024-02-21 19:32:20 +0530467// channelzRegistration registers the newly created ClientConn with channelz and
468// stores the returned identifier in `cc.channelzID` and `cc.csMgr.channelzID`.
469// A channelz trace event is emitted for ClientConn creation. If the newly
470// created ClientConn is a nested one, i.e a valid parent ClientConn ID is
471// specified via a dial option, the trace event is also added to the parent.
472//
473// Doesn't grab cc.mu as this method is expected to be called only at Dial time.
474func (cc *ClientConn) channelzRegistration(target string) {
475 cc.channelzID = channelz.RegisterChannel(&channelzChannel{cc}, cc.dopts.channelzParentID, target)
476 cc.addTraceEvent("created")
477 cc.csMgr.channelzID = cc.channelzID
khenaidoo5fc5cea2021-08-11 17:39:16 -0400478}
479
480// chainUnaryClientInterceptors chains all unary client interceptors into one.
481func chainUnaryClientInterceptors(cc *ClientConn) {
482 interceptors := cc.dopts.chainUnaryInts
483 // Prepend dopts.unaryInt to the chaining interceptors if it exists, since unaryInt will
484 // be executed before any other chained interceptors.
485 if cc.dopts.unaryInt != nil {
486 interceptors = append([]UnaryClientInterceptor{cc.dopts.unaryInt}, interceptors...)
487 }
488 var chainedInt UnaryClientInterceptor
489 if len(interceptors) == 0 {
490 chainedInt = nil
491 } else if len(interceptors) == 1 {
492 chainedInt = interceptors[0]
493 } else {
494 chainedInt = func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
495 return interceptors[0](ctx, method, req, reply, cc, getChainUnaryInvoker(interceptors, 0, invoker), opts...)
496 }
497 }
498 cc.dopts.unaryInt = chainedInt
499}
500
501// getChainUnaryInvoker recursively generate the chained unary invoker.
502func getChainUnaryInvoker(interceptors []UnaryClientInterceptor, curr int, finalInvoker UnaryInvoker) UnaryInvoker {
503 if curr == len(interceptors)-1 {
504 return finalInvoker
505 }
506 return func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, opts ...CallOption) error {
507 return interceptors[curr+1](ctx, method, req, reply, cc, getChainUnaryInvoker(interceptors, curr+1, finalInvoker), opts...)
508 }
509}
510
511// chainStreamClientInterceptors chains all stream client interceptors into one.
512func chainStreamClientInterceptors(cc *ClientConn) {
513 interceptors := cc.dopts.chainStreamInts
514 // Prepend dopts.streamInt to the chaining interceptors if it exists, since streamInt will
515 // be executed before any other chained interceptors.
516 if cc.dopts.streamInt != nil {
517 interceptors = append([]StreamClientInterceptor{cc.dopts.streamInt}, interceptors...)
518 }
519 var chainedInt StreamClientInterceptor
520 if len(interceptors) == 0 {
521 chainedInt = nil
522 } else if len(interceptors) == 1 {
523 chainedInt = interceptors[0]
524 } else {
525 chainedInt = func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error) {
526 return interceptors[0](ctx, desc, cc, method, getChainStreamer(interceptors, 0, streamer), opts...)
527 }
528 }
529 cc.dopts.streamInt = chainedInt
530}
531
532// getChainStreamer recursively generate the chained client stream constructor.
533func getChainStreamer(interceptors []StreamClientInterceptor, curr int, finalStreamer Streamer) Streamer {
534 if curr == len(interceptors)-1 {
535 return finalStreamer
536 }
537 return func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
538 return interceptors[curr+1](ctx, desc, cc, method, getChainStreamer(interceptors, curr+1, finalStreamer), opts...)
539 }
540}
541
542// connectivityStateManager keeps the connectivity.State of ClientConn.
543// This struct will eventually be exported so the balancers can access it.
544type connectivityStateManager struct {
545 mu sync.Mutex
546 state connectivity.State
547 notifyChan chan struct{}
Akash Kankanala761955c2024-02-21 19:32:20 +0530548 channelzID *channelz.Identifier
khenaidoo5fc5cea2021-08-11 17:39:16 -0400549}
550
551// updateState updates the connectivity.State of ClientConn.
552// If there's a change it notifies goroutines waiting on state change to
553// happen.
554func (csm *connectivityStateManager) updateState(state connectivity.State) {
555 csm.mu.Lock()
556 defer csm.mu.Unlock()
557 if csm.state == connectivity.Shutdown {
558 return
559 }
560 if csm.state == state {
561 return
562 }
563 csm.state = state
564 channelz.Infof(logger, csm.channelzID, "Channel Connectivity change to %v", state)
565 if csm.notifyChan != nil {
566 // There are other goroutines waiting on this channel.
567 close(csm.notifyChan)
568 csm.notifyChan = nil
569 }
570}
571
572func (csm *connectivityStateManager) getState() connectivity.State {
573 csm.mu.Lock()
574 defer csm.mu.Unlock()
575 return csm.state
576}
577
578func (csm *connectivityStateManager) getNotifyChan() <-chan struct{} {
579 csm.mu.Lock()
580 defer csm.mu.Unlock()
581 if csm.notifyChan == nil {
582 csm.notifyChan = make(chan struct{})
583 }
584 return csm.notifyChan
585}
586
587// ClientConnInterface defines the functions clients need to perform unary and
588// streaming RPCs. It is implemented by *ClientConn, and is only intended to
589// be referenced by generated code.
590type ClientConnInterface interface {
591 // Invoke performs a unary RPC and returns after the response is received
592 // into reply.
593 Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...CallOption) error
594 // NewStream begins a streaming RPC.
595 NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error)
596}
597
598// Assert *ClientConn implements ClientConnInterface.
599var _ ClientConnInterface = (*ClientConn)(nil)
600
601// ClientConn represents a virtual connection to a conceptual endpoint, to
602// perform RPCs.
603//
604// A ClientConn is free to have zero or more actual connections to the endpoint
605// based on configuration, load, etc. It is also free to determine which actual
606// endpoints to use and may change it every RPC, permitting client-side load
607// balancing.
608//
609// A ClientConn encapsulates a range of functionality including name
610// resolution, TCP connection establishment (with retries and backoff) and TLS
611// handshakes. It also handles errors on established connections by
612// re-resolving the name and reconnecting.
613type ClientConn struct {
Akash Kankanala761955c2024-02-21 19:32:20 +0530614 ctx context.Context // Initialized using the background context at dial time.
615 cancel context.CancelFunc // Cancelled on close.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400616
Akash Kankanala761955c2024-02-21 19:32:20 +0530617 // The following are initialized at dial time, and are read-only after that.
618 target string // User's dial target.
619 parsedTarget resolver.Target // See parseTargetAndFindResolver().
620 authority string // See determineAuthority().
621 dopts dialOptions // Default and user specified dial options.
622 channelzID *channelz.Identifier // Channelz identifier for the channel.
623 resolverBuilder resolver.Builder // See parseTargetAndFindResolver().
624 balancerWrapper *ccBalancerWrapper // Uses gracefulswitch.balancer underneath.
625 idlenessMgr idlenessManager
khenaidoo5fc5cea2021-08-11 17:39:16 -0400626
Akash Kankanala761955c2024-02-21 19:32:20 +0530627 // The following provide their own synchronization, and therefore don't
628 // require cc.mu to be held to access them.
629 csMgr *connectivityStateManager
630 blockingpicker *pickerWrapper
khenaidoo5fc5cea2021-08-11 17:39:16 -0400631 safeConfigSelector iresolver.SafeConfigSelector
Akash Kankanala761955c2024-02-21 19:32:20 +0530632 czData *channelzData
633 retryThrottler atomic.Value // Updated from service config.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400634
Akash Kankanala761955c2024-02-21 19:32:20 +0530635 // firstResolveEvent is used to track whether the name resolver sent us at
636 // least one update. RPCs block on this event.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400637 firstResolveEvent *grpcsync.Event
638
Akash Kankanala761955c2024-02-21 19:32:20 +0530639 // mu protects the following fields.
640 // TODO: split mu so the same mutex isn't used for everything.
641 mu sync.RWMutex
642 resolverWrapper *ccResolverWrapper // Initialized in Dial; cleared in Close.
643 sc *ServiceConfig // Latest service config received from the resolver.
644 conns map[*addrConn]struct{} // Set to nil on close.
645 mkp keepalive.ClientParameters // May be updated upon receipt of a GoAway.
646 idlenessState ccIdlenessState // Tracks idleness state of the channel.
647 exitIdleCond *sync.Cond // Signalled when channel exits idle.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400648
649 lceMu sync.Mutex // protects lastConnectionError
650 lastConnectionError error
651}
652
Akash Kankanala761955c2024-02-21 19:32:20 +0530653// ccIdlenessState tracks the idleness state of the channel.
654//
655// Channels start off in `active` and move to `idle` after a period of
656// inactivity. When moving back to `active` upon an incoming RPC, they
657// transition through `exiting_idle`. This state is useful for synchronization
658// with Close().
659//
660// This state tracking is mostly for self-protection. The idlenessManager is
661// expected to keep track of the state as well, and is expected not to call into
662// the ClientConn unnecessarily.
663type ccIdlenessState int8
664
665const (
666 ccIdlenessStateActive ccIdlenessState = iota
667 ccIdlenessStateIdle
668 ccIdlenessStateExitingIdle
669)
670
khenaidoo5fc5cea2021-08-11 17:39:16 -0400671// WaitForStateChange waits until the connectivity.State of ClientConn changes from sourceState or
672// ctx expires. A true value is returned in former case and false in latter.
673//
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500674// # Experimental
khenaidoo5fc5cea2021-08-11 17:39:16 -0400675//
676// Notice: This API is EXPERIMENTAL and may be changed or removed in a
677// later release.
678func (cc *ClientConn) WaitForStateChange(ctx context.Context, sourceState connectivity.State) bool {
679 ch := cc.csMgr.getNotifyChan()
680 if cc.csMgr.getState() != sourceState {
681 return true
682 }
683 select {
684 case <-ctx.Done():
685 return false
686 case <-ch:
687 return true
688 }
689}
690
691// GetState returns the connectivity.State of ClientConn.
692//
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500693// # Experimental
khenaidoo5fc5cea2021-08-11 17:39:16 -0400694//
695// Notice: This API is EXPERIMENTAL and may be changed or removed in a later
696// release.
697func (cc *ClientConn) GetState() connectivity.State {
698 return cc.csMgr.getState()
699}
700
701// Connect causes all subchannels in the ClientConn to attempt to connect if
702// the channel is idle. Does not wait for the connection attempts to begin
703// before returning.
704//
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500705// # Experimental
khenaidoo5fc5cea2021-08-11 17:39:16 -0400706//
707// Notice: This API is EXPERIMENTAL and may be changed or removed in a later
708// release.
709func (cc *ClientConn) Connect() {
Akash Kankanala761955c2024-02-21 19:32:20 +0530710 cc.exitIdleMode()
711 // If the ClientConn was not in idle mode, we need to call ExitIdle on the
712 // LB policy so that connections can be created.
713 cc.balancerWrapper.exitIdleMode()
khenaidoo5fc5cea2021-08-11 17:39:16 -0400714}
715
716func (cc *ClientConn) scWatcher() {
717 for {
718 select {
719 case sc, ok := <-cc.dopts.scChan:
720 if !ok {
721 return
722 }
723 cc.mu.Lock()
724 // TODO: load balance policy runtime change is ignored.
725 // We may revisit this decision in the future.
726 cc.sc = &sc
727 cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{&sc})
728 cc.mu.Unlock()
729 case <-cc.ctx.Done():
730 return
731 }
732 }
733}
734
735// waitForResolvedAddrs blocks until the resolver has provided addresses or the
736// context expires. Returns nil unless the context expires first; otherwise
737// returns a status error based on the context.
738func (cc *ClientConn) waitForResolvedAddrs(ctx context.Context) error {
739 // This is on the RPC path, so we use a fast path to avoid the
740 // more-expensive "select" below after the resolver has returned once.
741 if cc.firstResolveEvent.HasFired() {
742 return nil
743 }
744 select {
745 case <-cc.firstResolveEvent.Done():
746 return nil
747 case <-ctx.Done():
748 return status.FromContextError(ctx.Err()).Err()
749 case <-cc.ctx.Done():
750 return ErrClientConnClosing
751 }
752}
753
754var emptyServiceConfig *ServiceConfig
755
756func init() {
757 cfg := parseServiceConfig("{}")
758 if cfg.Err != nil {
759 panic(fmt.Sprintf("impossible error parsing empty service config: %v", cfg.Err))
760 }
761 emptyServiceConfig = cfg.Config.(*ServiceConfig)
762}
763
764func (cc *ClientConn) maybeApplyDefaultServiceConfig(addrs []resolver.Address) {
765 if cc.sc != nil {
766 cc.applyServiceConfigAndBalancer(cc.sc, nil, addrs)
767 return
768 }
769 if cc.dopts.defaultServiceConfig != nil {
770 cc.applyServiceConfigAndBalancer(cc.dopts.defaultServiceConfig, &defaultConfigSelector{cc.dopts.defaultServiceConfig}, addrs)
771 } else {
772 cc.applyServiceConfigAndBalancer(emptyServiceConfig, &defaultConfigSelector{emptyServiceConfig}, addrs)
773 }
774}
775
776func (cc *ClientConn) updateResolverState(s resolver.State, err error) error {
777 defer cc.firstResolveEvent.Fire()
778 cc.mu.Lock()
779 // Check if the ClientConn is already closed. Some fields (e.g.
780 // balancerWrapper) are set to nil when closing the ClientConn, and could
781 // cause nil pointer panic if we don't have this check.
782 if cc.conns == nil {
783 cc.mu.Unlock()
784 return nil
785 }
786
787 if err != nil {
788 // May need to apply the initial service config in case the resolver
789 // doesn't support service configs, or doesn't provide a service config
790 // with the new addresses.
791 cc.maybeApplyDefaultServiceConfig(nil)
792
Akash Kankanala761955c2024-02-21 19:32:20 +0530793 cc.balancerWrapper.resolverError(err)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400794
795 // No addresses are valid with err set; return early.
796 cc.mu.Unlock()
797 return balancer.ErrBadResolverState
798 }
799
800 var ret error
khenaidoo257f3192021-12-15 16:46:37 -0500801 if cc.dopts.disableServiceConfig {
802 channelz.Infof(logger, cc.channelzID, "ignoring service config from resolver (%v) and applying the default because service config is disabled", s.ServiceConfig)
803 cc.maybeApplyDefaultServiceConfig(s.Addresses)
804 } else if s.ServiceConfig == nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400805 cc.maybeApplyDefaultServiceConfig(s.Addresses)
806 // TODO: do we need to apply a failing LB policy if there is no
807 // default, per the error handling design?
808 } else {
809 if sc, ok := s.ServiceConfig.Config.(*ServiceConfig); s.ServiceConfig.Err == nil && ok {
810 configSelector := iresolver.GetConfigSelector(s)
811 if configSelector != nil {
812 if len(s.ServiceConfig.Config.(*ServiceConfig).Methods) != 0 {
813 channelz.Infof(logger, cc.channelzID, "method configs in service config will be ignored due to presence of config selector")
814 }
815 } else {
816 configSelector = &defaultConfigSelector{sc}
817 }
818 cc.applyServiceConfigAndBalancer(sc, configSelector, s.Addresses)
819 } else {
820 ret = balancer.ErrBadResolverState
Akash Kankanala761955c2024-02-21 19:32:20 +0530821 if cc.sc == nil {
822 // Apply the failing LB only if we haven't received valid service config
823 // from the name resolver in the past.
824 cc.applyFailingLB(s.ServiceConfig)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400825 cc.mu.Unlock()
826 return ret
827 }
828 }
829 }
830
831 var balCfg serviceconfig.LoadBalancingConfig
Akash Kankanala761955c2024-02-21 19:32:20 +0530832 if cc.sc != nil && cc.sc.lbConfig != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400833 balCfg = cc.sc.lbConfig.cfg
834 }
khenaidoo5fc5cea2021-08-11 17:39:16 -0400835 bw := cc.balancerWrapper
836 cc.mu.Unlock()
Akash Kankanala761955c2024-02-21 19:32:20 +0530837
khenaidoo5fc5cea2021-08-11 17:39:16 -0400838 uccsErr := bw.updateClientConnState(&balancer.ClientConnState{ResolverState: s, BalancerConfig: balCfg})
839 if ret == nil {
840 ret = uccsErr // prefer ErrBadResolver state since any other error is
841 // currently meaningless to the caller.
842 }
843 return ret
844}
845
Akash Kankanala761955c2024-02-21 19:32:20 +0530846// applyFailingLB is akin to configuring an LB policy on the channel which
847// always fails RPCs. Here, an actual LB policy is not configured, but an always
848// erroring picker is configured, which returns errors with information about
849// what was invalid in the received service config. A config selector with no
850// service config is configured, and the connectivity state of the channel is
851// set to TransientFailure.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400852//
853// Caller must hold cc.mu.
Akash Kankanala761955c2024-02-21 19:32:20 +0530854func (cc *ClientConn) applyFailingLB(sc *serviceconfig.ParseResult) {
855 var err error
856 if sc.Err != nil {
857 err = status.Errorf(codes.Unavailable, "error parsing service config: %v", sc.Err)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400858 } else {
Akash Kankanala761955c2024-02-21 19:32:20 +0530859 err = status.Errorf(codes.Unavailable, "illegal service config type: %T", sc.Config)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400860 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530861 cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil})
862 cc.blockingpicker.updatePicker(base.NewErrPicker(err))
863 cc.csMgr.updateState(connectivity.TransientFailure)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400864}
865
866func (cc *ClientConn) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State, err error) {
Akash Kankanala761955c2024-02-21 19:32:20 +0530867 cc.balancerWrapper.updateSubConnState(sc, s, err)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400868}
869
870// newAddrConn creates an addrConn for addrs and adds it to cc.conns.
871//
872// Caller needs to make sure len(addrs) > 0.
873func (cc *ClientConn) newAddrConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (*addrConn, error) {
874 ac := &addrConn{
875 state: connectivity.Idle,
876 cc: cc,
877 addrs: addrs,
878 scopts: opts,
879 dopts: cc.dopts,
880 czData: new(channelzData),
881 resetBackoff: make(chan struct{}),
Akash Kankanala761955c2024-02-21 19:32:20 +0530882 stateChan: make(chan struct{}),
khenaidoo5fc5cea2021-08-11 17:39:16 -0400883 }
884 ac.ctx, ac.cancel = context.WithCancel(cc.ctx)
885 // Track ac in cc. This needs to be done before any getTransport(...) is called.
886 cc.mu.Lock()
Akash Kankanala761955c2024-02-21 19:32:20 +0530887 defer cc.mu.Unlock()
khenaidoo5fc5cea2021-08-11 17:39:16 -0400888 if cc.conns == nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400889 return nil, ErrClientConnClosing
890 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530891
892 var err error
893 ac.channelzID, err = channelz.RegisterSubChannel(ac, cc.channelzID, "")
894 if err != nil {
895 return nil, err
khenaidoo5fc5cea2021-08-11 17:39:16 -0400896 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530897 channelz.AddTraceEvent(logger, ac.channelzID, 0, &channelz.TraceEventDesc{
898 Desc: "Subchannel created",
899 Severity: channelz.CtInfo,
900 Parent: &channelz.TraceEventDesc{
901 Desc: fmt.Sprintf("Subchannel(id:%d) created", ac.channelzID.Int()),
902 Severity: channelz.CtInfo,
903 },
904 })
905
khenaidoo5fc5cea2021-08-11 17:39:16 -0400906 cc.conns[ac] = struct{}{}
khenaidoo5fc5cea2021-08-11 17:39:16 -0400907 return ac, nil
908}
909
910// removeAddrConn removes the addrConn in the subConn from clientConn.
911// It also tears down the ac with the given error.
912func (cc *ClientConn) removeAddrConn(ac *addrConn, err error) {
913 cc.mu.Lock()
914 if cc.conns == nil {
915 cc.mu.Unlock()
916 return
917 }
918 delete(cc.conns, ac)
919 cc.mu.Unlock()
920 ac.tearDown(err)
921}
922
923func (cc *ClientConn) channelzMetric() *channelz.ChannelInternalMetric {
924 return &channelz.ChannelInternalMetric{
925 State: cc.GetState(),
926 Target: cc.target,
927 CallsStarted: atomic.LoadInt64(&cc.czData.callsStarted),
928 CallsSucceeded: atomic.LoadInt64(&cc.czData.callsSucceeded),
929 CallsFailed: atomic.LoadInt64(&cc.czData.callsFailed),
930 LastCallStartedTimestamp: time.Unix(0, atomic.LoadInt64(&cc.czData.lastCallStartedTime)),
931 }
932}
933
934// Target returns the target string of the ClientConn.
935//
Joey Armstrongba3d9d12024-01-15 14:22:11 -0500936// # Experimental
khenaidoo5fc5cea2021-08-11 17:39:16 -0400937//
938// Notice: This API is EXPERIMENTAL and may be changed or removed in a
939// later release.
940func (cc *ClientConn) Target() string {
941 return cc.target
942}
943
944func (cc *ClientConn) incrCallsStarted() {
945 atomic.AddInt64(&cc.czData.callsStarted, 1)
946 atomic.StoreInt64(&cc.czData.lastCallStartedTime, time.Now().UnixNano())
947}
948
949func (cc *ClientConn) incrCallsSucceeded() {
950 atomic.AddInt64(&cc.czData.callsSucceeded, 1)
951}
952
953func (cc *ClientConn) incrCallsFailed() {
954 atomic.AddInt64(&cc.czData.callsFailed, 1)
955}
956
957// connect starts creating a transport.
958// It does nothing if the ac is not IDLE.
959// TODO(bar) Move this to the addrConn section.
960func (ac *addrConn) connect() error {
961 ac.mu.Lock()
962 if ac.state == connectivity.Shutdown {
Akash Kankanala761955c2024-02-21 19:32:20 +0530963 if logger.V(2) {
964 logger.Infof("connect called on shutdown addrConn; ignoring.")
965 }
khenaidoo5fc5cea2021-08-11 17:39:16 -0400966 ac.mu.Unlock()
967 return errConnClosing
968 }
969 if ac.state != connectivity.Idle {
Akash Kankanala761955c2024-02-21 19:32:20 +0530970 if logger.V(2) {
971 logger.Infof("connect called on addrConn in non-idle state (%v); ignoring.", ac.state)
972 }
khenaidoo5fc5cea2021-08-11 17:39:16 -0400973 ac.mu.Unlock()
974 return nil
975 }
khenaidoo5fc5cea2021-08-11 17:39:16 -0400976 ac.mu.Unlock()
977
978 ac.resetTransport()
979 return nil
980}
981
Akash Kankanala761955c2024-02-21 19:32:20 +0530982func equalAddresses(a, b []resolver.Address) bool {
983 if len(a) != len(b) {
984 return false
985 }
986 for i, v := range a {
987 if !v.Equal(b[i]) {
988 return false
989 }
990 }
991 return true
992}
993
994// updateAddrs updates ac.addrs with the new addresses list and handles active
995// connections or connection attempts.
996func (ac *addrConn) updateAddrs(addrs []resolver.Address) {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400997 ac.mu.Lock()
Akash Kankanala761955c2024-02-21 19:32:20 +0530998 channelz.Infof(logger, ac.channelzID, "addrConn: updateAddrs curAddr: %v, addrs: %v", ac.curAddr, addrs)
999
1000 if equalAddresses(ac.addrs, addrs) {
1001 ac.mu.Unlock()
1002 return
1003 }
1004
1005 ac.addrs = addrs
1006
khenaidoo5fc5cea2021-08-11 17:39:16 -04001007 if ac.state == connectivity.Shutdown ||
1008 ac.state == connectivity.TransientFailure ||
1009 ac.state == connectivity.Idle {
Akash Kankanala761955c2024-02-21 19:32:20 +05301010 // We were not connecting, so do nothing but update the addresses.
1011 ac.mu.Unlock()
1012 return
khenaidoo5fc5cea2021-08-11 17:39:16 -04001013 }
1014
Akash Kankanala761955c2024-02-21 19:32:20 +05301015 if ac.state == connectivity.Ready {
1016 // Try to find the connected address.
1017 for _, a := range addrs {
1018 a.ServerName = ac.cc.getServerName(a)
1019 if a.Equal(ac.curAddr) {
1020 // We are connected to a valid address, so do nothing but
1021 // update the addresses.
1022 ac.mu.Unlock()
1023 return
1024 }
khenaidoo5fc5cea2021-08-11 17:39:16 -04001025 }
1026 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301027
1028 // We are either connected to the wrong address or currently connecting.
1029 // Stop the current iteration and restart.
1030
1031 ac.cancel()
1032 ac.ctx, ac.cancel = context.WithCancel(ac.cc.ctx)
1033
1034 // We have to defer here because GracefulClose => Close => onClose, which
1035 // requires locking ac.mu.
1036 if ac.transport != nil {
1037 defer ac.transport.GracefulClose()
1038 ac.transport = nil
khenaidoo5fc5cea2021-08-11 17:39:16 -04001039 }
1040
Akash Kankanala761955c2024-02-21 19:32:20 +05301041 if len(addrs) == 0 {
1042 ac.updateConnectivityState(connectivity.Idle, nil)
1043 }
1044
1045 ac.mu.Unlock()
1046
1047 // Since we were connecting/connected, we should start a new connection
1048 // attempt.
1049 go ac.resetTransport()
khenaidoo5fc5cea2021-08-11 17:39:16 -04001050}
1051
khenaidoo5cb0d402021-12-08 14:09:16 -05001052// getServerName determines the serverName to be used in the connection
1053// handshake. The default value for the serverName is the authority on the
1054// ClientConn, which either comes from the user's dial target or through an
1055// authority override specified using the WithAuthority dial option. Name
1056// resolvers can specify a per-address override for the serverName through the
1057// resolver.Address.ServerName field which is used only if the WithAuthority
1058// dial option was not used. The rationale is that per-address authority
1059// overrides specified by the name resolver can represent a security risk, while
1060// an override specified by the user is more dependable since they probably know
1061// what they are doing.
1062func (cc *ClientConn) getServerName(addr resolver.Address) string {
1063 if cc.dopts.authority != "" {
1064 return cc.dopts.authority
1065 }
1066 if addr.ServerName != "" {
1067 return addr.ServerName
1068 }
1069 return cc.authority
1070}
1071
khenaidoo5fc5cea2021-08-11 17:39:16 -04001072func getMethodConfig(sc *ServiceConfig, method string) MethodConfig {
1073 if sc == nil {
1074 return MethodConfig{}
1075 }
1076 if m, ok := sc.Methods[method]; ok {
1077 return m
1078 }
1079 i := strings.LastIndex(method, "/")
1080 if m, ok := sc.Methods[method[:i+1]]; ok {
1081 return m
1082 }
1083 return sc.Methods[""]
1084}
1085
1086// GetMethodConfig gets the method config of the input method.
1087// If there's an exact match for input method (i.e. /service/method), we return
1088// the corresponding MethodConfig.
1089// If there isn't an exact match for the input method, we look for the service's default
1090// config under the service (i.e /service/) and then for the default for all services (empty string).
1091//
1092// If there is a default MethodConfig for the service, we return it.
1093// Otherwise, we return an empty MethodConfig.
1094func (cc *ClientConn) GetMethodConfig(method string) MethodConfig {
1095 // TODO: Avoid the locking here.
1096 cc.mu.RLock()
1097 defer cc.mu.RUnlock()
1098 return getMethodConfig(cc.sc, method)
1099}
1100
1101func (cc *ClientConn) healthCheckConfig() *healthCheckConfig {
1102 cc.mu.RLock()
1103 defer cc.mu.RUnlock()
1104 if cc.sc == nil {
1105 return nil
1106 }
1107 return cc.sc.healthCheckConfig
1108}
1109
Akash Kankanala761955c2024-02-21 19:32:20 +05301110func (cc *ClientConn) getTransport(ctx context.Context, failfast bool, method string) (transport.ClientTransport, balancer.PickResult, error) {
1111 return cc.blockingpicker.pick(ctx, failfast, balancer.PickInfo{
khenaidoo5fc5cea2021-08-11 17:39:16 -04001112 Ctx: ctx,
1113 FullMethodName: method,
1114 })
khenaidoo5fc5cea2021-08-11 17:39:16 -04001115}
1116
1117func (cc *ClientConn) applyServiceConfigAndBalancer(sc *ServiceConfig, configSelector iresolver.ConfigSelector, addrs []resolver.Address) {
1118 if sc == nil {
1119 // should never reach here.
1120 return
1121 }
1122 cc.sc = sc
1123 if configSelector != nil {
1124 cc.safeConfigSelector.UpdateConfigSelector(configSelector)
1125 }
1126
1127 if cc.sc.retryThrottling != nil {
1128 newThrottler := &retryThrottler{
1129 tokens: cc.sc.retryThrottling.MaxTokens,
1130 max: cc.sc.retryThrottling.MaxTokens,
1131 thresh: cc.sc.retryThrottling.MaxTokens / 2,
1132 ratio: cc.sc.retryThrottling.TokenRatio,
1133 }
1134 cc.retryThrottler.Store(newThrottler)
1135 } else {
1136 cc.retryThrottler.Store((*retryThrottler)(nil))
1137 }
1138
Akash Kankanala761955c2024-02-21 19:32:20 +05301139 var newBalancerName string
1140 if cc.sc != nil && cc.sc.lbConfig != nil {
1141 newBalancerName = cc.sc.lbConfig.name
1142 } else {
1143 var isGRPCLB bool
1144 for _, a := range addrs {
1145 if a.Type == resolver.GRPCLB {
1146 isGRPCLB = true
1147 break
khenaidoo5fc5cea2021-08-11 17:39:16 -04001148 }
1149 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301150 if isGRPCLB {
1151 newBalancerName = grpclbName
1152 } else if cc.sc != nil && cc.sc.LB != nil {
1153 newBalancerName = *cc.sc.LB
1154 } else {
1155 newBalancerName = PickFirstBalancerName
1156 }
khenaidoo5fc5cea2021-08-11 17:39:16 -04001157 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301158 cc.balancerWrapper.switchTo(newBalancerName)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001159}
1160
1161func (cc *ClientConn) resolveNow(o resolver.ResolveNowOptions) {
1162 cc.mu.RLock()
1163 r := cc.resolverWrapper
1164 cc.mu.RUnlock()
1165 if r == nil {
1166 return
1167 }
1168 go r.resolveNow(o)
1169}
1170
1171// ResetConnectBackoff wakes up all subchannels in transient failure and causes
1172// them to attempt another connection immediately. It also resets the backoff
1173// times used for subsequent attempts regardless of the current state.
1174//
1175// In general, this function should not be used. Typical service or network
1176// outages result in a reasonable client reconnection strategy by default.
1177// However, if a previously unavailable network becomes available, this may be
1178// used to trigger an immediate reconnect.
1179//
Joey Armstrongba3d9d12024-01-15 14:22:11 -05001180// # Experimental
khenaidoo5fc5cea2021-08-11 17:39:16 -04001181//
1182// Notice: This API is EXPERIMENTAL and may be changed or removed in a
1183// later release.
1184func (cc *ClientConn) ResetConnectBackoff() {
1185 cc.mu.Lock()
1186 conns := cc.conns
1187 cc.mu.Unlock()
1188 for ac := range conns {
1189 ac.resetConnectBackoff()
1190 }
1191}
1192
1193// Close tears down the ClientConn and all underlying connections.
1194func (cc *ClientConn) Close() error {
1195 defer cc.cancel()
1196
1197 cc.mu.Lock()
1198 if cc.conns == nil {
1199 cc.mu.Unlock()
1200 return ErrClientConnClosing
1201 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301202
1203 for cc.idlenessState == ccIdlenessStateExitingIdle {
1204 cc.exitIdleCond.Wait()
1205 }
1206
khenaidoo5fc5cea2021-08-11 17:39:16 -04001207 conns := cc.conns
1208 cc.conns = nil
1209 cc.csMgr.updateState(connectivity.Shutdown)
1210
Akash Kankanala761955c2024-02-21 19:32:20 +05301211 pWrapper := cc.blockingpicker
khenaidoo5fc5cea2021-08-11 17:39:16 -04001212 rWrapper := cc.resolverWrapper
khenaidoo5fc5cea2021-08-11 17:39:16 -04001213 bWrapper := cc.balancerWrapper
Akash Kankanala761955c2024-02-21 19:32:20 +05301214 idlenessMgr := cc.idlenessMgr
khenaidoo5fc5cea2021-08-11 17:39:16 -04001215 cc.mu.Unlock()
1216
Akash Kankanala761955c2024-02-21 19:32:20 +05301217 // The order of closing matters here since the balancer wrapper assumes the
1218 // picker is closed before it is closed.
1219 if pWrapper != nil {
1220 pWrapper.close()
1221 }
khenaidoo5fc5cea2021-08-11 17:39:16 -04001222 if bWrapper != nil {
1223 bWrapper.close()
1224 }
1225 if rWrapper != nil {
1226 rWrapper.close()
1227 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301228 if idlenessMgr != nil {
1229 idlenessMgr.close()
1230 }
khenaidoo5fc5cea2021-08-11 17:39:16 -04001231
1232 for ac := range conns {
1233 ac.tearDown(ErrClientConnClosing)
1234 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301235 cc.addTraceEvent("deleted")
1236 // TraceEvent needs to be called before RemoveEntry, as TraceEvent may add
1237 // trace reference to the entity being deleted, and thus prevent it from being
1238 // deleted right away.
1239 channelz.RemoveEntry(cc.channelzID)
1240
khenaidoo5fc5cea2021-08-11 17:39:16 -04001241 return nil
1242}
1243
1244// addrConn is a network connection to a given address.
1245type addrConn struct {
1246 ctx context.Context
1247 cancel context.CancelFunc
1248
1249 cc *ClientConn
1250 dopts dialOptions
1251 acbw balancer.SubConn
1252 scopts balancer.NewSubConnOptions
1253
1254 // transport is set when there's a viable transport (note: ac state may not be READY as LB channel
1255 // health checking may require server to report healthy to set ac to READY), and is reset
1256 // to nil when the current transport should no longer be used to create a stream (e.g. after GoAway
1257 // is received, transport is closed, ac has been torn down).
1258 transport transport.ClientTransport // The current transport.
1259
1260 mu sync.Mutex
1261 curAddr resolver.Address // The current address.
1262 addrs []resolver.Address // All addresses that the resolver resolved to.
1263
1264 // Use updateConnectivityState for updating addrConn's connectivity state.
Akash Kankanala761955c2024-02-21 19:32:20 +05301265 state connectivity.State
1266 stateChan chan struct{} // closed and recreated on every state change.
khenaidoo5fc5cea2021-08-11 17:39:16 -04001267
1268 backoffIdx int // Needs to be stateful for resetConnectBackoff.
1269 resetBackoff chan struct{}
1270
Akash Kankanala761955c2024-02-21 19:32:20 +05301271 channelzID *channelz.Identifier
khenaidoo5fc5cea2021-08-11 17:39:16 -04001272 czData *channelzData
1273}
1274
1275// Note: this requires a lock on ac.mu.
1276func (ac *addrConn) updateConnectivityState(s connectivity.State, lastErr error) {
1277 if ac.state == s {
1278 return
1279 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301280 // When changing states, reset the state change channel.
1281 close(ac.stateChan)
1282 ac.stateChan = make(chan struct{})
khenaidoo5fc5cea2021-08-11 17:39:16 -04001283 ac.state = s
Akash Kankanala761955c2024-02-21 19:32:20 +05301284 if lastErr == nil {
1285 channelz.Infof(logger, ac.channelzID, "Subchannel Connectivity change to %v", s)
1286 } else {
1287 channelz.Infof(logger, ac.channelzID, "Subchannel Connectivity change to %v, last error: %s", s, lastErr)
1288 }
khenaidoo5fc5cea2021-08-11 17:39:16 -04001289 ac.cc.handleSubConnStateChange(ac.acbw, s, lastErr)
1290}
1291
1292// adjustParams updates parameters used to create transports upon
1293// receiving a GoAway.
1294func (ac *addrConn) adjustParams(r transport.GoAwayReason) {
1295 switch r {
1296 case transport.GoAwayTooManyPings:
1297 v := 2 * ac.dopts.copts.KeepaliveParams.Time
1298 ac.cc.mu.Lock()
1299 if v > ac.cc.mkp.Time {
1300 ac.cc.mkp.Time = v
1301 }
1302 ac.cc.mu.Unlock()
1303 }
1304}
1305
1306func (ac *addrConn) resetTransport() {
1307 ac.mu.Lock()
Akash Kankanala761955c2024-02-21 19:32:20 +05301308 acCtx := ac.ctx
1309 if acCtx.Err() != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001310 ac.mu.Unlock()
1311 return
1312 }
1313
1314 addrs := ac.addrs
1315 backoffFor := ac.dopts.bs.Backoff(ac.backoffIdx)
1316 // This will be the duration that dial gets to finish.
1317 dialDuration := minConnectTimeout
1318 if ac.dopts.minConnectTimeout != nil {
1319 dialDuration = ac.dopts.minConnectTimeout()
1320 }
1321
1322 if dialDuration < backoffFor {
1323 // Give dial more time as we keep failing to connect.
1324 dialDuration = backoffFor
1325 }
1326 // We can potentially spend all the time trying the first address, and
1327 // if the server accepts the connection and then hangs, the following
1328 // addresses will never be tried.
1329 //
1330 // The spec doesn't mention what should be done for multiple addresses.
1331 // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md#proposed-backoff-algorithm
1332 connectDeadline := time.Now().Add(dialDuration)
1333
1334 ac.updateConnectivityState(connectivity.Connecting, nil)
1335 ac.mu.Unlock()
1336
Akash Kankanala761955c2024-02-21 19:32:20 +05301337 if err := ac.tryAllAddrs(acCtx, addrs, connectDeadline); err != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001338 ac.cc.resolveNow(resolver.ResolveNowOptions{})
1339 // After exhausting all addresses, the addrConn enters
1340 // TRANSIENT_FAILURE.
Akash Kankanala761955c2024-02-21 19:32:20 +05301341 if acCtx.Err() != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001342 return
1343 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301344 ac.mu.Lock()
khenaidoo5fc5cea2021-08-11 17:39:16 -04001345 ac.updateConnectivityState(connectivity.TransientFailure, err)
1346
1347 // Backoff.
1348 b := ac.resetBackoff
1349 ac.mu.Unlock()
1350
1351 timer := time.NewTimer(backoffFor)
1352 select {
1353 case <-timer.C:
1354 ac.mu.Lock()
1355 ac.backoffIdx++
1356 ac.mu.Unlock()
1357 case <-b:
1358 timer.Stop()
Akash Kankanala761955c2024-02-21 19:32:20 +05301359 case <-acCtx.Done():
khenaidoo5fc5cea2021-08-11 17:39:16 -04001360 timer.Stop()
1361 return
1362 }
1363
1364 ac.mu.Lock()
Akash Kankanala761955c2024-02-21 19:32:20 +05301365 if acCtx.Err() == nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001366 ac.updateConnectivityState(connectivity.Idle, err)
1367 }
1368 ac.mu.Unlock()
1369 return
1370 }
1371 // Success; reset backoff.
1372 ac.mu.Lock()
1373 ac.backoffIdx = 0
1374 ac.mu.Unlock()
1375}
1376
1377// tryAllAddrs tries to creates a connection to the addresses, and stop when at
1378// the first successful one. It returns an error if no address was successfully
1379// connected, or updates ac appropriately with the new transport.
Akash Kankanala761955c2024-02-21 19:32:20 +05301380func (ac *addrConn) tryAllAddrs(ctx context.Context, addrs []resolver.Address, connectDeadline time.Time) error {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001381 var firstConnErr error
1382 for _, addr := range addrs {
Akash Kankanala761955c2024-02-21 19:32:20 +05301383 if ctx.Err() != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001384 return errConnClosing
1385 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301386 ac.mu.Lock()
khenaidoo5fc5cea2021-08-11 17:39:16 -04001387
1388 ac.cc.mu.RLock()
1389 ac.dopts.copts.KeepaliveParams = ac.cc.mkp
1390 ac.cc.mu.RUnlock()
1391
1392 copts := ac.dopts.copts
1393 if ac.scopts.CredsBundle != nil {
1394 copts.CredsBundle = ac.scopts.CredsBundle
1395 }
1396 ac.mu.Unlock()
1397
1398 channelz.Infof(logger, ac.channelzID, "Subchannel picks a new address %q to connect", addr.Addr)
1399
Akash Kankanala761955c2024-02-21 19:32:20 +05301400 err := ac.createTransport(ctx, addr, copts, connectDeadline)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001401 if err == nil {
1402 return nil
1403 }
1404 if firstConnErr == nil {
1405 firstConnErr = err
1406 }
1407 ac.cc.updateConnectionError(err)
1408 }
1409
1410 // Couldn't connect to any address.
1411 return firstConnErr
1412}
1413
1414// createTransport creates a connection to addr. It returns an error if the
1415// address was not successfully connected, or updates ac appropriately with the
1416// new transport.
Akash Kankanala761955c2024-02-21 19:32:20 +05301417func (ac *addrConn) createTransport(ctx context.Context, addr resolver.Address, copts transport.ConnectOptions, connectDeadline time.Time) error {
khenaidoo5cb0d402021-12-08 14:09:16 -05001418 addr.ServerName = ac.cc.getServerName(addr)
Akash Kankanala761955c2024-02-21 19:32:20 +05301419 hctx, hcancel := context.WithCancel(ctx)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001420
Akash Kankanala761955c2024-02-21 19:32:20 +05301421 onClose := func(r transport.GoAwayReason) {
khenaidoo5fc5cea2021-08-11 17:39:16 -04001422 ac.mu.Lock()
1423 defer ac.mu.Unlock()
Akash Kankanala761955c2024-02-21 19:32:20 +05301424 // adjust params based on GoAwayReason
1425 ac.adjustParams(r)
1426 if ctx.Err() != nil {
1427 // Already shut down or connection attempt canceled. tearDown() or
1428 // updateAddrs() already cleared the transport and canceled hctx
1429 // via ac.ctx, and we expected this connection to be closed, so do
1430 // nothing here.
khenaidoo5fc5cea2021-08-11 17:39:16 -04001431 return
1432 }
1433 hcancel()
Akash Kankanala761955c2024-02-21 19:32:20 +05301434 if ac.transport == nil {
1435 // We're still connecting to this address, which could error. Do
1436 // not update the connectivity state or resolve; these will happen
1437 // at the end of the tryAllAddrs connection loop in the event of an
1438 // error.
1439 return
khenaidoo5fc5cea2021-08-11 17:39:16 -04001440 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301441 ac.transport = nil
1442 // Refresh the name resolver on any connection loss.
1443 ac.cc.resolveNow(resolver.ResolveNowOptions{})
1444 // Always go idle and wait for the LB policy to initiate a new
1445 // connection attempt.
1446 ac.updateConnectivityState(connectivity.Idle, nil)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001447 }
1448
Akash Kankanala761955c2024-02-21 19:32:20 +05301449 connectCtx, cancel := context.WithDeadline(ctx, connectDeadline)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001450 defer cancel()
Akash Kankanala761955c2024-02-21 19:32:20 +05301451 copts.ChannelzParentID = ac.channelzID
khenaidoo5fc5cea2021-08-11 17:39:16 -04001452
Akash Kankanala761955c2024-02-21 19:32:20 +05301453 newTr, err := transport.NewClientTransport(connectCtx, ac.cc.ctx, addr, copts, onClose)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001454 if err != nil {
Akash Kankanala761955c2024-02-21 19:32:20 +05301455 if logger.V(2) {
1456 logger.Infof("Creating new client transport to %q: %v", addr, err)
1457 }
khenaidoo5fc5cea2021-08-11 17:39:16 -04001458 // newTr is either nil, or closed.
Akash Kankanala761955c2024-02-21 19:32:20 +05301459 hcancel()
1460 channelz.Warningf(logger, ac.channelzID, "grpc: addrConn.createTransport failed to connect to %s. Err: %v", addr, err)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001461 return err
1462 }
1463
Akash Kankanala761955c2024-02-21 19:32:20 +05301464 ac.mu.Lock()
1465 defer ac.mu.Unlock()
1466 if ctx.Err() != nil {
1467 // This can happen if the subConn was removed while in `Connecting`
1468 // state. tearDown() would have set the state to `Shutdown`, but
1469 // would not have closed the transport since ac.transport would not
1470 // have been set at that point.
1471 //
1472 // We run this in a goroutine because newTr.Close() calls onClose()
1473 // inline, which requires locking ac.mu.
1474 //
khenaidoo5fc5cea2021-08-11 17:39:16 -04001475 // The error we pass to Close() is immaterial since there are no open
1476 // streams at this point, so no trailers with error details will be sent
1477 // out. We just need to pass a non-nil error.
Akash Kankanala761955c2024-02-21 19:32:20 +05301478 //
1479 // This can also happen when updateAddrs is called during a connection
1480 // attempt.
1481 go newTr.Close(transport.ErrConnClosing)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001482 return nil
khenaidoo5fc5cea2021-08-11 17:39:16 -04001483 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301484 if hctx.Err() != nil {
1485 // onClose was already called for this connection, but the connection
1486 // was successfully established first. Consider it a success and set
1487 // the new state to Idle.
1488 ac.updateConnectivityState(connectivity.Idle, nil)
1489 return nil
1490 }
1491 ac.curAddr = addr
1492 ac.transport = newTr
1493 ac.startHealthCheck(hctx) // Will set state to READY if appropriate.
1494 return nil
khenaidoo5fc5cea2021-08-11 17:39:16 -04001495}
1496
1497// startHealthCheck starts the health checking stream (RPC) to watch the health
1498// stats of this connection if health checking is requested and configured.
1499//
1500// LB channel health checking is enabled when all requirements below are met:
1501// 1. it is not disabled by the user with the WithDisableHealthCheck DialOption
1502// 2. internal.HealthCheckFunc is set by importing the grpc/health package
1503// 3. a service config with non-empty healthCheckConfig field is provided
1504// 4. the load balancer requests it
1505//
1506// It sets addrConn to READY if the health checking stream is not started.
1507//
1508// Caller must hold ac.mu.
1509func (ac *addrConn) startHealthCheck(ctx context.Context) {
1510 var healthcheckManagingState bool
1511 defer func() {
1512 if !healthcheckManagingState {
1513 ac.updateConnectivityState(connectivity.Ready, nil)
1514 }
1515 }()
1516
1517 if ac.cc.dopts.disableHealthCheck {
1518 return
1519 }
1520 healthCheckConfig := ac.cc.healthCheckConfig()
1521 if healthCheckConfig == nil {
1522 return
1523 }
1524 if !ac.scopts.HealthCheckEnabled {
1525 return
1526 }
1527 healthCheckFunc := ac.cc.dopts.healthCheckFunc
1528 if healthCheckFunc == nil {
1529 // The health package is not imported to set health check function.
1530 //
1531 // TODO: add a link to the health check doc in the error message.
1532 channelz.Error(logger, ac.channelzID, "Health check is requested but health check function is not set.")
1533 return
1534 }
1535
1536 healthcheckManagingState = true
1537
1538 // Set up the health check helper functions.
1539 currentTr := ac.transport
1540 newStream := func(method string) (interface{}, error) {
1541 ac.mu.Lock()
1542 if ac.transport != currentTr {
1543 ac.mu.Unlock()
1544 return nil, status.Error(codes.Canceled, "the provided transport is no longer valid to use")
1545 }
1546 ac.mu.Unlock()
1547 return newNonRetryClientStream(ctx, &StreamDesc{ServerStreams: true}, method, currentTr, ac)
1548 }
1549 setConnectivityState := func(s connectivity.State, lastErr error) {
1550 ac.mu.Lock()
1551 defer ac.mu.Unlock()
1552 if ac.transport != currentTr {
1553 return
1554 }
1555 ac.updateConnectivityState(s, lastErr)
1556 }
1557 // Start the health checking stream.
1558 go func() {
1559 err := ac.cc.dopts.healthCheckFunc(ctx, newStream, setConnectivityState, healthCheckConfig.ServiceName)
1560 if err != nil {
1561 if status.Code(err) == codes.Unimplemented {
1562 channelz.Error(logger, ac.channelzID, "Subchannel health check is unimplemented at server side, thus health check is disabled")
1563 } else {
Akash Kankanala761955c2024-02-21 19:32:20 +05301564 channelz.Errorf(logger, ac.channelzID, "Health checking failed: %v", err)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001565 }
1566 }
1567 }()
1568}
1569
1570func (ac *addrConn) resetConnectBackoff() {
1571 ac.mu.Lock()
1572 close(ac.resetBackoff)
1573 ac.backoffIdx = 0
1574 ac.resetBackoff = make(chan struct{})
1575 ac.mu.Unlock()
1576}
1577
1578// getReadyTransport returns the transport if ac's state is READY or nil if not.
1579func (ac *addrConn) getReadyTransport() transport.ClientTransport {
1580 ac.mu.Lock()
1581 defer ac.mu.Unlock()
1582 if ac.state == connectivity.Ready {
1583 return ac.transport
1584 }
1585 return nil
1586}
1587
Akash Kankanala761955c2024-02-21 19:32:20 +05301588// getTransport waits until the addrconn is ready and returns the transport.
1589// If the context expires first, returns an appropriate status. If the
1590// addrConn is stopped first, returns an Unavailable status error.
1591func (ac *addrConn) getTransport(ctx context.Context) (transport.ClientTransport, error) {
1592 for ctx.Err() == nil {
1593 ac.mu.Lock()
1594 t, state, sc := ac.transport, ac.state, ac.stateChan
1595 ac.mu.Unlock()
1596 if state == connectivity.Ready {
1597 return t, nil
1598 }
1599 if state == connectivity.Shutdown {
1600 return nil, status.Errorf(codes.Unavailable, "SubConn shutting down")
1601 }
1602
1603 select {
1604 case <-ctx.Done():
1605 case <-sc:
1606 }
1607 }
1608 return nil, status.FromContextError(ctx.Err()).Err()
1609}
1610
khenaidoo5fc5cea2021-08-11 17:39:16 -04001611// tearDown starts to tear down the addrConn.
1612//
1613// Note that tearDown doesn't remove ac from ac.cc.conns, so the addrConn struct
1614// will leak. In most cases, call cc.removeAddrConn() instead.
1615func (ac *addrConn) tearDown(err error) {
1616 ac.mu.Lock()
1617 if ac.state == connectivity.Shutdown {
1618 ac.mu.Unlock()
1619 return
1620 }
1621 curTr := ac.transport
1622 ac.transport = nil
1623 // We have to set the state to Shutdown before anything else to prevent races
1624 // between setting the state and logic that waits on context cancellation / etc.
1625 ac.updateConnectivityState(connectivity.Shutdown, nil)
1626 ac.cancel()
1627 ac.curAddr = resolver.Address{}
1628 if err == errConnDrain && curTr != nil {
1629 // GracefulClose(...) may be executed multiple times when
1630 // i) receiving multiple GoAway frames from the server; or
1631 // ii) there are concurrent name resolver/Balancer triggered
1632 // address removal and GoAway.
1633 // We have to unlock and re-lock here because GracefulClose => Close => onClose, which requires locking ac.mu.
1634 ac.mu.Unlock()
1635 curTr.GracefulClose()
1636 ac.mu.Lock()
1637 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301638 channelz.AddTraceEvent(logger, ac.channelzID, 0, &channelz.TraceEventDesc{
1639 Desc: "Subchannel deleted",
1640 Severity: channelz.CtInfo,
1641 Parent: &channelz.TraceEventDesc{
1642 Desc: fmt.Sprintf("Subchannel(id:%d) deleted", ac.channelzID.Int()),
khenaidoo5fc5cea2021-08-11 17:39:16 -04001643 Severity: channelz.CtInfo,
Akash Kankanala761955c2024-02-21 19:32:20 +05301644 },
1645 })
1646 // TraceEvent needs to be called before RemoveEntry, as TraceEvent may add
1647 // trace reference to the entity being deleted, and thus prevent it from
1648 // being deleted right away.
1649 channelz.RemoveEntry(ac.channelzID)
khenaidoo5fc5cea2021-08-11 17:39:16 -04001650 ac.mu.Unlock()
1651}
1652
1653func (ac *addrConn) getState() connectivity.State {
1654 ac.mu.Lock()
1655 defer ac.mu.Unlock()
1656 return ac.state
1657}
1658
1659func (ac *addrConn) ChannelzMetric() *channelz.ChannelInternalMetric {
1660 ac.mu.Lock()
1661 addr := ac.curAddr.Addr
1662 ac.mu.Unlock()
1663 return &channelz.ChannelInternalMetric{
1664 State: ac.getState(),
1665 Target: addr,
1666 CallsStarted: atomic.LoadInt64(&ac.czData.callsStarted),
1667 CallsSucceeded: atomic.LoadInt64(&ac.czData.callsSucceeded),
1668 CallsFailed: atomic.LoadInt64(&ac.czData.callsFailed),
1669 LastCallStartedTimestamp: time.Unix(0, atomic.LoadInt64(&ac.czData.lastCallStartedTime)),
1670 }
1671}
1672
1673func (ac *addrConn) incrCallsStarted() {
1674 atomic.AddInt64(&ac.czData.callsStarted, 1)
1675 atomic.StoreInt64(&ac.czData.lastCallStartedTime, time.Now().UnixNano())
1676}
1677
1678func (ac *addrConn) incrCallsSucceeded() {
1679 atomic.AddInt64(&ac.czData.callsSucceeded, 1)
1680}
1681
1682func (ac *addrConn) incrCallsFailed() {
1683 atomic.AddInt64(&ac.czData.callsFailed, 1)
1684}
1685
1686type retryThrottler struct {
1687 max float64
1688 thresh float64
1689 ratio float64
1690
1691 mu sync.Mutex
1692 tokens float64 // TODO(dfawley): replace with atomic and remove lock.
1693}
1694
1695// throttle subtracts a retry token from the pool and returns whether a retry
1696// should be throttled (disallowed) based upon the retry throttling policy in
1697// the service config.
1698func (rt *retryThrottler) throttle() bool {
1699 if rt == nil {
1700 return false
1701 }
1702 rt.mu.Lock()
1703 defer rt.mu.Unlock()
1704 rt.tokens--
1705 if rt.tokens < 0 {
1706 rt.tokens = 0
1707 }
1708 return rt.tokens <= rt.thresh
1709}
1710
1711func (rt *retryThrottler) successfulRPC() {
1712 if rt == nil {
1713 return
1714 }
1715 rt.mu.Lock()
1716 defer rt.mu.Unlock()
1717 rt.tokens += rt.ratio
1718 if rt.tokens > rt.max {
1719 rt.tokens = rt.max
1720 }
1721}
1722
1723type channelzChannel struct {
1724 cc *ClientConn
1725}
1726
1727func (c *channelzChannel) ChannelzMetric() *channelz.ChannelInternalMetric {
1728 return c.cc.channelzMetric()
1729}
1730
1731// ErrClientConnTimeout indicates that the ClientConn cannot establish the
1732// underlying connections within the specified timeout.
1733//
1734// Deprecated: This error is never returned by grpc and should not be
1735// referenced by users.
1736var ErrClientConnTimeout = errors.New("grpc: timed out when dialing")
1737
Akash Kankanala761955c2024-02-21 19:32:20 +05301738// getResolver finds the scheme in the cc's resolvers or the global registry.
1739// scheme should always be lowercase (typically by virtue of url.Parse()
1740// performing proper RFC3986 behavior).
khenaidoo5fc5cea2021-08-11 17:39:16 -04001741func (cc *ClientConn) getResolver(scheme string) resolver.Builder {
1742 for _, rb := range cc.dopts.resolvers {
1743 if scheme == rb.Scheme() {
1744 return rb
1745 }
1746 }
1747 return resolver.Get(scheme)
1748}
1749
1750func (cc *ClientConn) updateConnectionError(err error) {
1751 cc.lceMu.Lock()
1752 cc.lastConnectionError = err
1753 cc.lceMu.Unlock()
1754}
1755
1756func (cc *ClientConn) connectionError() error {
1757 cc.lceMu.Lock()
1758 defer cc.lceMu.Unlock()
1759 return cc.lastConnectionError
1760}
khenaidoo5cb0d402021-12-08 14:09:16 -05001761
Akash Kankanala761955c2024-02-21 19:32:20 +05301762// parseTargetAndFindResolver parses the user's dial target and stores the
1763// parsed target in `cc.parsedTarget`.
1764//
1765// The resolver to use is determined based on the scheme in the parsed target
1766// and the same is stored in `cc.resolverBuilder`.
1767//
1768// Doesn't grab cc.mu as this method is expected to be called only at Dial time.
1769func (cc *ClientConn) parseTargetAndFindResolver() error {
khenaidoo5cb0d402021-12-08 14:09:16 -05001770 channelz.Infof(logger, cc.channelzID, "original dial target is: %q", cc.target)
1771
1772 var rb resolver.Builder
1773 parsedTarget, err := parseTarget(cc.target)
1774 if err != nil {
1775 channelz.Infof(logger, cc.channelzID, "dial target %q parse failed: %v", cc.target, err)
1776 } else {
1777 channelz.Infof(logger, cc.channelzID, "parsed dial target is: %+v", parsedTarget)
Akash Kankanala761955c2024-02-21 19:32:20 +05301778 rb = cc.getResolver(parsedTarget.URL.Scheme)
khenaidoo5cb0d402021-12-08 14:09:16 -05001779 if rb != nil {
1780 cc.parsedTarget = parsedTarget
Akash Kankanala761955c2024-02-21 19:32:20 +05301781 cc.resolverBuilder = rb
1782 return nil
khenaidoo5cb0d402021-12-08 14:09:16 -05001783 }
1784 }
1785
1786 // We are here because the user's dial target did not contain a scheme or
1787 // specified an unregistered scheme. We should fallback to the default
1788 // scheme, except when a custom dialer is specified in which case, we should
1789 // always use passthrough scheme.
1790 defScheme := resolver.GetDefaultScheme()
1791 channelz.Infof(logger, cc.channelzID, "fallback to scheme %q", defScheme)
1792 canonicalTarget := defScheme + ":///" + cc.target
1793
1794 parsedTarget, err = parseTarget(canonicalTarget)
1795 if err != nil {
1796 channelz.Infof(logger, cc.channelzID, "dial target %q parse failed: %v", canonicalTarget, err)
Akash Kankanala761955c2024-02-21 19:32:20 +05301797 return err
khenaidoo5cb0d402021-12-08 14:09:16 -05001798 }
1799 channelz.Infof(logger, cc.channelzID, "parsed dial target is: %+v", parsedTarget)
Akash Kankanala761955c2024-02-21 19:32:20 +05301800 rb = cc.getResolver(parsedTarget.URL.Scheme)
khenaidoo5cb0d402021-12-08 14:09:16 -05001801 if rb == nil {
Akash Kankanala761955c2024-02-21 19:32:20 +05301802 return fmt.Errorf("could not get resolver for default scheme: %q", parsedTarget.URL.Scheme)
khenaidoo5cb0d402021-12-08 14:09:16 -05001803 }
1804 cc.parsedTarget = parsedTarget
Akash Kankanala761955c2024-02-21 19:32:20 +05301805 cc.resolverBuilder = rb
1806 return nil
khenaidoo5cb0d402021-12-08 14:09:16 -05001807}
1808
1809// parseTarget uses RFC 3986 semantics to parse the given target into a
Akash Kankanala761955c2024-02-21 19:32:20 +05301810// resolver.Target struct containing scheme, authority and url. Query
khenaidoo5cb0d402021-12-08 14:09:16 -05001811// params are stripped from the endpoint.
1812func parseTarget(target string) (resolver.Target, error) {
1813 u, err := url.Parse(target)
1814 if err != nil {
1815 return resolver.Target{}, err
1816 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301817
khenaidoo5cb0d402021-12-08 14:09:16 -05001818 return resolver.Target{
1819 Scheme: u.Scheme,
1820 Authority: u.Host,
khenaidoo5cb0d402021-12-08 14:09:16 -05001821 URL: *u,
1822 }, nil
1823}
1824
1825// Determine channel authority. The order of precedence is as follows:
1826// - user specified authority override using `WithAuthority` dial option
1827// - creds' notion of server name for the authentication handshake
1828// - endpoint from dial target of the form "scheme://[authority]/endpoint"
Akash Kankanala761955c2024-02-21 19:32:20 +05301829//
1830// Stores the determined authority in `cc.authority`.
1831//
1832// Returns a non-nil error if the authority returned by the transport
1833// credentials do not match the authority configured through the dial option.
1834//
1835// Doesn't grab cc.mu as this method is expected to be called only at Dial time.
1836func (cc *ClientConn) determineAuthority() error {
1837 dopts := cc.dopts
khenaidoo5cb0d402021-12-08 14:09:16 -05001838 // Historically, we had two options for users to specify the serverName or
1839 // authority for a channel. One was through the transport credentials
1840 // (either in its constructor, or through the OverrideServerName() method).
1841 // The other option (for cases where WithInsecure() dial option was used)
1842 // was to use the WithAuthority() dial option.
1843 //
1844 // A few things have changed since:
1845 // - `insecure` package with an implementation of the `TransportCredentials`
1846 // interface for the insecure case
1847 // - WithAuthority() dial option support for secure credentials
1848 authorityFromCreds := ""
1849 if creds := dopts.copts.TransportCredentials; creds != nil && creds.Info().ServerName != "" {
1850 authorityFromCreds = creds.Info().ServerName
1851 }
1852 authorityFromDialOption := dopts.authority
1853 if (authorityFromCreds != "" && authorityFromDialOption != "") && authorityFromCreds != authorityFromDialOption {
Akash Kankanala761955c2024-02-21 19:32:20 +05301854 return fmt.Errorf("ClientConn's authority from transport creds %q and dial option %q don't match", authorityFromCreds, authorityFromDialOption)
khenaidoo5cb0d402021-12-08 14:09:16 -05001855 }
1856
Akash Kankanala761955c2024-02-21 19:32:20 +05301857 endpoint := cc.parsedTarget.Endpoint()
1858 target := cc.target
khenaidoo5cb0d402021-12-08 14:09:16 -05001859 switch {
1860 case authorityFromDialOption != "":
Akash Kankanala761955c2024-02-21 19:32:20 +05301861 cc.authority = authorityFromDialOption
khenaidoo5cb0d402021-12-08 14:09:16 -05001862 case authorityFromCreds != "":
Akash Kankanala761955c2024-02-21 19:32:20 +05301863 cc.authority = authorityFromCreds
khenaidoo5cb0d402021-12-08 14:09:16 -05001864 case strings.HasPrefix(target, "unix:") || strings.HasPrefix(target, "unix-abstract:"):
1865 // TODO: remove when the unix resolver implements optional interface to
1866 // return channel authority.
Akash Kankanala761955c2024-02-21 19:32:20 +05301867 cc.authority = "localhost"
khenaidoo5cb0d402021-12-08 14:09:16 -05001868 case strings.HasPrefix(endpoint, ":"):
Akash Kankanala761955c2024-02-21 19:32:20 +05301869 cc.authority = "localhost" + endpoint
khenaidoo5cb0d402021-12-08 14:09:16 -05001870 default:
1871 // TODO: Define an optional interface on the resolver builder to return
1872 // the channel authority given the user's dial target. For resolvers
1873 // which don't implement this interface, we will use the endpoint from
1874 // "scheme://authority/endpoint" as the default authority.
Akash Kankanala761955c2024-02-21 19:32:20 +05301875 cc.authority = endpoint
khenaidoo5cb0d402021-12-08 14:09:16 -05001876 }
Akash Kankanala761955c2024-02-21 19:32:20 +05301877 channelz.Infof(logger, cc.channelzID, "Channel authority set to %q", cc.authority)
1878 return nil
1879}
1880
1881// initResolverWrapper creates a ccResolverWrapper, which builds the name
1882// resolver. This method grabs the lock to assign the newly built resolver
1883// wrapper to the cc.resolverWrapper field.
1884func (cc *ClientConn) initResolverWrapper(creds credentials.TransportCredentials) error {
1885 rw, err := newCCResolverWrapper(cc, ccResolverWrapperOpts{
1886 target: cc.parsedTarget,
1887 builder: cc.resolverBuilder,
1888 bOpts: resolver.BuildOptions{
1889 DisableServiceConfig: cc.dopts.disableServiceConfig,
1890 DialCreds: creds,
1891 CredsBundle: cc.dopts.copts.CredsBundle,
1892 Dialer: cc.dopts.copts.Dialer,
1893 },
1894 channelzID: cc.channelzID,
1895 })
1896 if err != nil {
1897 return fmt.Errorf("failed to build resolver: %v", err)
1898 }
1899 // Resolver implementations may report state update or error inline when
1900 // built (or right after), and this is handled in cc.updateResolverState.
1901 // Also, an error from the resolver might lead to a re-resolution request
1902 // from the balancer, which is handled in resolveNow() where
1903 // `cc.resolverWrapper` is accessed. Hence, we need to hold the lock here.
1904 cc.mu.Lock()
1905 cc.resolverWrapper = rw
1906 cc.mu.Unlock()
1907 return nil
khenaidoo5cb0d402021-12-08 14:09:16 -05001908}