3 * Copyright 2014 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 "golang.org/x/net/context"
32 "golang.org/x/net/trace"
33 "google.golang.org/grpc/balancer"
34 _ "google.golang.org/grpc/balancer/roundrobin" // To register roundrobin.
35 "google.golang.org/grpc/connectivity"
36 "google.golang.org/grpc/credentials"
37 "google.golang.org/grpc/grpclog"
38 "google.golang.org/grpc/keepalive"
39 "google.golang.org/grpc/resolver"
40 _ "google.golang.org/grpc/resolver/dns" // To register dns resolver.
41 _ "google.golang.org/grpc/resolver/passthrough" // To register passthrough resolver.
42 "google.golang.org/grpc/stats"
43 "google.golang.org/grpc/transport"
47 // ErrClientConnClosing indicates that the operation is illegal because
48 // the ClientConn is closing.
49 ErrClientConnClosing = errors.New("grpc: the client connection is closing")
50 // ErrClientConnTimeout indicates that the ClientConn cannot establish the
51 // underlying connections within the specified timeout.
52 // DEPRECATED: Please use context.DeadlineExceeded instead.
53 ErrClientConnTimeout = errors.New("grpc: timed out when dialing")
55 // errNoTransportSecurity indicates that there is no transport security
56 // being set for ClientConn. Users should either set one or explicitly
57 // call WithInsecure DialOption to disable security.
58 errNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithInsecure() explicitly or set credentials)")
59 // errTransportCredentialsMissing indicates that users want to transmit security
60 // information (e.g., oauth2 token) which requires secure connection on an insecure
62 errTransportCredentialsMissing = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportCredentials() to set)")
63 // errCredentialsConflict indicates that grpc.WithTransportCredentials()
64 // and grpc.WithInsecure() are both called for a connection.
65 errCredentialsConflict = errors.New("grpc: transport credentials are set for an insecure connection (grpc.WithTransportCredentials() and grpc.WithInsecure() are both called)")
66 // errNetworkIO indicates that the connection is down due to some network I/O error.
67 errNetworkIO = errors.New("grpc: failed with network I/O error")
68 // errConnDrain indicates that the connection starts to be drained and does not accept any new RPCs.
69 errConnDrain = errors.New("grpc: the connection is drained")
70 // errConnClosing indicates that the connection is closing.
71 errConnClosing = errors.New("grpc: the connection is closing")
72 // errConnUnavailable indicates that the connection is unavailable.
73 errConnUnavailable = errors.New("grpc: the connection is unavailable")
74 // errBalancerClosed indicates that the balancer is closed.
75 errBalancerClosed = errors.New("grpc: balancer is closed")
76 // minimum time to give a connection to complete
77 minConnectTimeout = 20 * time.Second
80 // dialOptions configure a Dial call. dialOptions are set by the DialOption
81 // values passed to Dial.
82 type dialOptions struct {
83 unaryInt UnaryClientInterceptor
84 streamInt StreamClientInterceptor
92 scChan <-chan ServiceConfig
93 copts transport.ConnectOptions
94 callOptions []CallOption
95 // This is to support v1 balancer.
96 balancerBuilder balancer.Builder
100 defaultClientMaxReceiveMessageSize = 1024 * 1024 * 4
101 defaultClientMaxSendMessageSize = math.MaxInt32
104 // DialOption configures how we set up the connection.
105 type DialOption func(*dialOptions)
107 // WithWriteBufferSize lets you set the size of write buffer, this determines how much data can be batched
108 // before doing a write on the wire.
109 func WithWriteBufferSize(s int) DialOption {
110 return func(o *dialOptions) {
111 o.copts.WriteBufferSize = s
115 // WithReadBufferSize lets you set the size of read buffer, this determines how much data can be read at most
116 // for each read syscall.
117 func WithReadBufferSize(s int) DialOption {
118 return func(o *dialOptions) {
119 o.copts.ReadBufferSize = s
123 // WithInitialWindowSize returns a DialOption which sets the value for initial window size on a stream.
124 // The lower bound for window size is 64K and any value smaller than that will be ignored.
125 func WithInitialWindowSize(s int32) DialOption {
126 return func(o *dialOptions) {
127 o.copts.InitialWindowSize = s
131 // WithInitialConnWindowSize returns a DialOption which sets the value for initial window size on a connection.
132 // The lower bound for window size is 64K and any value smaller than that will be ignored.
133 func WithInitialConnWindowSize(s int32) DialOption {
134 return func(o *dialOptions) {
135 o.copts.InitialConnWindowSize = s
139 // WithMaxMsgSize returns a DialOption which sets the maximum message size the client can receive. Deprecated: use WithDefaultCallOptions(MaxCallRecvMsgSize(s)) instead.
140 func WithMaxMsgSize(s int) DialOption {
141 return WithDefaultCallOptions(MaxCallRecvMsgSize(s))
144 // WithDefaultCallOptions returns a DialOption which sets the default CallOptions for calls over the connection.
145 func WithDefaultCallOptions(cos ...CallOption) DialOption {
146 return func(o *dialOptions) {
147 o.callOptions = append(o.callOptions, cos...)
151 // WithCodec returns a DialOption which sets a codec for message marshaling and unmarshaling.
152 func WithCodec(c Codec) DialOption {
153 return func(o *dialOptions) {
158 // WithCompressor returns a DialOption which sets a CompressorGenerator for generating message
160 func WithCompressor(cp Compressor) DialOption {
161 return func(o *dialOptions) {
166 // WithDecompressor returns a DialOption which sets a DecompressorGenerator for generating
167 // message decompressor.
168 func WithDecompressor(dc Decompressor) DialOption {
169 return func(o *dialOptions) {
174 // WithBalancer returns a DialOption which sets a load balancer with the v1 API.
175 // Name resolver will be ignored if this DialOption is specified.
176 // Deprecated: use the new balancer APIs in balancer package instead.
177 func WithBalancer(b Balancer) DialOption {
178 return func(o *dialOptions) {
179 o.balancerBuilder = &balancerWrapperBuilder{
185 // WithBalancerBuilder is for testing only. Users using custom balancers should
186 // register their balancer and use service config to choose the balancer to use.
187 func WithBalancerBuilder(b balancer.Builder) DialOption {
188 // TODO(bar) remove this when switching balancer is done.
189 return func(o *dialOptions) {
190 o.balancerBuilder = b
194 // WithServiceConfig returns a DialOption which has a channel to read the service configuration.
195 // DEPRECATED: service config should be received through name resolver, as specified here.
196 // https://github.com/grpc/grpc/blob/master/doc/service_config.md
197 func WithServiceConfig(c <-chan ServiceConfig) DialOption {
198 return func(o *dialOptions) {
203 // WithBackoffMaxDelay configures the dialer to use the provided maximum delay
204 // when backing off after failed connection attempts.
205 func WithBackoffMaxDelay(md time.Duration) DialOption {
206 return WithBackoffConfig(BackoffConfig{MaxDelay: md})
209 // WithBackoffConfig configures the dialer to use the provided backoff
210 // parameters after connection failures.
212 // Use WithBackoffMaxDelay until more parameters on BackoffConfig are opened up
214 func WithBackoffConfig(b BackoffConfig) DialOption {
215 // Set defaults to ensure that provided BackoffConfig is valid and
216 // unexported fields get default values.
218 return withBackoff(b)
221 // withBackoff sets the backoff strategy used for retries after a
222 // failed connection attempt.
224 // This can be exported if arbitrary backoff strategies are allowed by gRPC.
225 func withBackoff(bs backoffStrategy) DialOption {
226 return func(o *dialOptions) {
231 // WithBlock returns a DialOption which makes caller of Dial blocks until the underlying
232 // connection is up. Without this, Dial returns immediately and connecting the server
233 // happens in background.
234 func WithBlock() DialOption {
235 return func(o *dialOptions) {
240 // WithInsecure returns a DialOption which disables transport security for this ClientConn.
241 // Note that transport security is required unless WithInsecure is set.
242 func WithInsecure() DialOption {
243 return func(o *dialOptions) {
248 // WithTransportCredentials returns a DialOption which configures a
249 // connection level security credentials (e.g., TLS/SSL).
250 func WithTransportCredentials(creds credentials.TransportCredentials) DialOption {
251 return func(o *dialOptions) {
252 o.copts.TransportCredentials = creds
256 // WithPerRPCCredentials returns a DialOption which sets
257 // credentials and places auth state on each outbound RPC.
258 func WithPerRPCCredentials(creds credentials.PerRPCCredentials) DialOption {
259 return func(o *dialOptions) {
260 o.copts.PerRPCCredentials = append(o.copts.PerRPCCredentials, creds)
264 // WithTimeout returns a DialOption that configures a timeout for dialing a ClientConn
265 // initially. This is valid if and only if WithBlock() is present.
266 // Deprecated: use DialContext and context.WithTimeout instead.
267 func WithTimeout(d time.Duration) DialOption {
268 return func(o *dialOptions) {
273 // WithDialer returns a DialOption that specifies a function to use for dialing network addresses.
274 // If FailOnNonTempDialError() is set to true, and an error is returned by f, gRPC checks the error's
275 // Temporary() method to decide if it should try to reconnect to the network address.
276 func WithDialer(f func(string, time.Duration) (net.Conn, error)) DialOption {
277 return func(o *dialOptions) {
278 o.copts.Dialer = func(ctx context.Context, addr string) (net.Conn, error) {
279 if deadline, ok := ctx.Deadline(); ok {
280 return f(addr, deadline.Sub(time.Now()))
287 // WithStatsHandler returns a DialOption that specifies the stats handler
288 // for all the RPCs and underlying network connections in this ClientConn.
289 func WithStatsHandler(h stats.Handler) DialOption {
290 return func(o *dialOptions) {
291 o.copts.StatsHandler = h
295 // FailOnNonTempDialError returns a DialOption that specifies if gRPC fails on non-temporary dial errors.
296 // If f is true, and dialer returns a non-temporary error, gRPC will fail the connection to the network
297 // address and won't try to reconnect.
298 // The default value of FailOnNonTempDialError is false.
299 // This is an EXPERIMENTAL API.
300 func FailOnNonTempDialError(f bool) DialOption {
301 return func(o *dialOptions) {
302 o.copts.FailOnNonTempDialError = f
306 // WithUserAgent returns a DialOption that specifies a user agent string for all the RPCs.
307 func WithUserAgent(s string) DialOption {
308 return func(o *dialOptions) {
309 o.copts.UserAgent = s
313 // WithKeepaliveParams returns a DialOption that specifies keepalive parameters for the client transport.
314 func WithKeepaliveParams(kp keepalive.ClientParameters) DialOption {
315 return func(o *dialOptions) {
316 o.copts.KeepaliveParams = kp
320 // WithUnaryInterceptor returns a DialOption that specifies the interceptor for unary RPCs.
321 func WithUnaryInterceptor(f UnaryClientInterceptor) DialOption {
322 return func(o *dialOptions) {
327 // WithStreamInterceptor returns a DialOption that specifies the interceptor for streaming RPCs.
328 func WithStreamInterceptor(f StreamClientInterceptor) DialOption {
329 return func(o *dialOptions) {
334 // WithAuthority returns a DialOption that specifies the value to be used as
335 // the :authority pseudo-header. This value only works with WithInsecure and
336 // has no effect if TransportCredentials are present.
337 func WithAuthority(a string) DialOption {
338 return func(o *dialOptions) {
339 o.copts.Authority = a
343 // Dial creates a client connection to the given target.
344 func Dial(target string, opts ...DialOption) (*ClientConn, error) {
345 return DialContext(context.Background(), target, opts...)
348 // DialContext creates a client connection to the given target. ctx can be used to
349 // cancel or expire the pending connection. Once this function returns, the
350 // cancellation and expiration of ctx will be noop. Users should call ClientConn.Close
351 // to terminate all the pending operations after this function returns.
352 func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) {
355 csMgr: &connectivityStateManager{},
356 conns: make(map[*addrConn]struct{}),
358 blockingpicker: newPickerWrapper(),
360 cc.ctx, cc.cancel = context.WithCancel(context.Background())
362 for _, opt := range opts {
366 if !cc.dopts.insecure {
367 if cc.dopts.copts.TransportCredentials == nil {
368 return nil, errNoTransportSecurity
371 if cc.dopts.copts.TransportCredentials != nil {
372 return nil, errCredentialsConflict
374 for _, cd := range cc.dopts.copts.PerRPCCredentials {
375 if cd.RequireTransportSecurity() {
376 return nil, errTransportCredentialsMissing
381 cc.mkp = cc.dopts.copts.KeepaliveParams
383 if cc.dopts.copts.Dialer == nil {
384 cc.dopts.copts.Dialer = newProxyDialer(
385 func(ctx context.Context, addr string) (net.Conn, error) {
386 return dialContext(ctx, "tcp", addr)
391 if cc.dopts.copts.UserAgent != "" {
392 cc.dopts.copts.UserAgent += " " + grpcUA
394 cc.dopts.copts.UserAgent = grpcUA
397 if cc.dopts.timeout > 0 {
398 var cancel context.CancelFunc
399 ctx, cancel = context.WithTimeout(ctx, cc.dopts.timeout)
406 conn, err = nil, ctx.Err()
416 if cc.dopts.scChan != nil {
417 // Try to get an initial service config.
419 case sc, ok := <-cc.dopts.scChan:
428 if cc.dopts.codec == nil {
429 cc.dopts.codec = protoCodec{}
431 if cc.dopts.bs == nil {
432 cc.dopts.bs = DefaultBackoffConfig
434 cc.parsedTarget = parseTarget(cc.target)
435 creds := cc.dopts.copts.TransportCredentials
436 if creds != nil && creds.Info().ServerName != "" {
437 cc.authority = creds.Info().ServerName
438 } else if cc.dopts.insecure && cc.dopts.copts.Authority != "" {
439 cc.authority = cc.dopts.copts.Authority
441 // Use endpoint from "scheme://authority/endpoint" as the default
442 // authority for ClientConn.
443 cc.authority = cc.parsedTarget.Endpoint
446 if cc.dopts.scChan != nil && !scSet {
447 // Blocking wait for the initial service config.
449 case sc, ok := <-cc.dopts.scChan:
454 return nil, ctx.Err()
457 if cc.dopts.scChan != nil {
461 var credsClone credentials.TransportCredentials
462 if creds := cc.dopts.copts.TransportCredentials; creds != nil {
463 credsClone = creds.Clone()
465 cc.balancerBuildOpts = balancer.BuildOptions{
466 DialCreds: credsClone,
467 Dialer: cc.dopts.copts.Dialer,
470 if cc.dopts.balancerBuilder != nil {
471 cc.customBalancer = true
472 // Build should not take long time. So it's ok to not have a goroutine for it.
473 cc.balancerWrapper = newCCBalancerWrapper(cc, cc.dopts.balancerBuilder, cc.balancerBuildOpts)
476 // Build the resolver.
477 cc.resolverWrapper, err = newCCResolverWrapper(cc)
479 return nil, fmt.Errorf("failed to build resolver: %v", err)
482 // A blocking dial blocks until the clientConn is ready.
486 if s == connectivity.Ready {
489 if !cc.WaitForStateChange(ctx, s) {
490 // ctx got timeout or canceled.
491 return nil, ctx.Err()
499 // connectivityStateManager keeps the connectivity.State of ClientConn.
500 // This struct will eventually be exported so the balancers can access it.
501 type connectivityStateManager struct {
503 state connectivity.State
504 notifyChan chan struct{}
507 // updateState updates the connectivity.State of ClientConn.
508 // If there's a change it notifies goroutines waiting on state change to
510 func (csm *connectivityStateManager) updateState(state connectivity.State) {
512 defer csm.mu.Unlock()
513 if csm.state == connectivity.Shutdown {
516 if csm.state == state {
520 if csm.notifyChan != nil {
521 // There are other goroutines waiting on this channel.
522 close(csm.notifyChan)
527 func (csm *connectivityStateManager) getState() connectivity.State {
529 defer csm.mu.Unlock()
533 func (csm *connectivityStateManager) getNotifyChan() <-chan struct{} {
535 defer csm.mu.Unlock()
536 if csm.notifyChan == nil {
537 csm.notifyChan = make(chan struct{})
539 return csm.notifyChan
542 // ClientConn represents a client connection to an RPC server.
543 type ClientConn struct {
545 cancel context.CancelFunc
548 parsedTarget resolver.Target
551 csMgr *connectivityStateManager
553 customBalancer bool // If this is true, switching balancer will be disabled.
554 balancerBuildOpts balancer.BuildOptions
555 resolverWrapper *ccResolverWrapper
556 blockingpicker *pickerWrapper
561 conns map[*addrConn]struct{}
562 // Keepalive parameter can be updated if a GoAway is received.
563 mkp keepalive.ClientParameters
564 curBalancerName string
565 curAddresses []resolver.Address
566 balancerWrapper *ccBalancerWrapper
569 // WaitForStateChange waits until the connectivity.State of ClientConn changes from sourceState or
570 // ctx expires. A true value is returned in former case and false in latter.
571 // This is an EXPERIMENTAL API.
572 func (cc *ClientConn) WaitForStateChange(ctx context.Context, sourceState connectivity.State) bool {
573 ch := cc.csMgr.getNotifyChan()
574 if cc.csMgr.getState() != sourceState {
585 // GetState returns the connectivity.State of ClientConn.
586 // This is an EXPERIMENTAL API.
587 func (cc *ClientConn) GetState() connectivity.State {
588 return cc.csMgr.getState()
591 func (cc *ClientConn) scWatcher() {
594 case sc, ok := <-cc.dopts.scChan:
599 // TODO: load balance policy runtime change is ignored.
600 // We may revist this decision in the future.
604 case <-cc.ctx.Done():
610 func (cc *ClientConn) handleResolvedAddrs(addrs []resolver.Address, err error) {
617 // TODO(bar switching) when grpclb is submitted, check address type and start grpclb.
618 if !cc.customBalancer && cc.balancerWrapper == nil {
619 // No customBalancer was specified by DialOption, and this is the first
620 // time handling resolved addresses, create a pickfirst balancer.
621 builder := newPickfirstBuilder()
622 cc.curBalancerName = builder.Name()
623 cc.balancerWrapper = newCCBalancerWrapper(cc, builder, cc.balancerBuildOpts)
626 // TODO(bar switching) compare addresses, if there's no update, don't notify balancer.
627 cc.curAddresses = addrs
628 cc.balancerWrapper.handleResolvedAddrs(addrs, nil)
631 // switchBalancer starts the switching from current balancer to the balancer with name.
632 func (cc *ClientConn) switchBalancer(name string) {
638 grpclog.Infof("ClientConn switching balancer to %q", name)
640 if cc.customBalancer {
641 grpclog.Infoln("ignoring service config balancer configuration: WithBalancer DialOption used instead")
645 if cc.curBalancerName == name {
649 // TODO(bar switching) change this to two steps: drain and close.
650 // Keep track of sc in wrapper.
651 cc.balancerWrapper.close()
653 builder := balancer.Get(name)
655 grpclog.Infof("failed to get balancer builder for: %v (this should never happen...)", name)
656 builder = newPickfirstBuilder()
658 cc.curBalancerName = builder.Name()
659 cc.balancerWrapper = newCCBalancerWrapper(cc, builder, cc.balancerBuildOpts)
660 cc.balancerWrapper.handleResolvedAddrs(cc.curAddresses, nil)
663 func (cc *ClientConn) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
669 // TODO(bar switching) send updates to all balancer wrappers when balancer
670 // gracefully switching is supported.
671 cc.balancerWrapper.handleSubConnStateChange(sc, s)
675 // newAddrConn creates an addrConn for addrs and adds it to cc.conns.
676 func (cc *ClientConn) newAddrConn(addrs []resolver.Address) (*addrConn, error) {
682 ac.ctx, ac.cancel = context.WithCancel(cc.ctx)
683 // Track ac in cc. This needs to be done before any getTransport(...) is called.
687 return nil, ErrClientConnClosing
689 cc.conns[ac] = struct{}{}
694 // removeAddrConn removes the addrConn in the subConn from clientConn.
695 // It also tears down the ac with the given error.
696 func (cc *ClientConn) removeAddrConn(ac *addrConn, err error) {
707 // connect starts to creating transport and also starts the transport monitor
708 // goroutine for this ac.
709 // It does nothing if the ac is not IDLE.
710 // TODO(bar) Move this to the addrConn section.
711 // This was part of resetAddrConn, keep it here to make the diff look clean.
712 func (ac *addrConn) connect() error {
714 if ac.state == connectivity.Shutdown {
716 return errConnClosing
718 if ac.state != connectivity.Idle {
722 ac.state = connectivity.Connecting
723 ac.cc.handleSubConnStateChange(ac.acbw, ac.state)
726 // Start a goroutine connecting to the server asynchronously.
728 if err := ac.resetTransport(); err != nil {
729 grpclog.Warningf("Failed to dial %s: %v; please retry.", ac.addrs[0].Addr, err)
730 if err != errConnClosing {
731 // Keep this ac in cc.conns, to get the reason it's torn down.
736 ac.transportMonitor()
741 // tryUpdateAddrs tries to update ac.addrs with the new addresses list.
743 // It checks whether current connected address of ac is in the new addrs list.
744 // - If true, it updates ac.addrs and returns true. The ac will keep using
745 // the existing connection.
746 // - If false, it does nothing and returns false.
747 func (ac *addrConn) tryUpdateAddrs(addrs []resolver.Address) bool {
750 grpclog.Infof("addrConn: tryUpdateAddrs curAddr: %v, addrs: %v", ac.curAddr, addrs)
751 if ac.state == connectivity.Shutdown {
756 var curAddrFound bool
757 for _, a := range addrs {
758 if reflect.DeepEqual(ac.curAddr, a) {
763 grpclog.Infof("addrConn: tryUpdateAddrs curAddrFound: %v", curAddrFound)
771 // GetMethodConfig gets the method config of the input method.
772 // If there's an exact match for input method (i.e. /service/method), we return
773 // the corresponding MethodConfig.
774 // If there isn't an exact match for the input method, we look for the default config
775 // under the service (i.e /service/). If there is a default MethodConfig for
776 // the serivce, we return it.
777 // Otherwise, we return an empty MethodConfig.
778 func (cc *ClientConn) GetMethodConfig(method string) MethodConfig {
779 // TODO: Avoid the locking here.
781 defer cc.mu.RUnlock()
782 m, ok := cc.sc.Methods[method]
784 i := strings.LastIndex(method, "/")
785 m, _ = cc.sc.Methods[method[:i+1]]
790 func (cc *ClientConn) getTransport(ctx context.Context, failfast bool) (transport.ClientTransport, func(balancer.DoneInfo), error) {
791 t, done, err := cc.blockingpicker.pick(ctx, failfast, balancer.PickOptions{})
793 return nil, nil, toRPCErr(err)
798 // handleServiceConfig parses the service config string in JSON format to Go native
799 // struct ServiceConfig, and store both the struct and the JSON string in ClientConn.
800 func (cc *ClientConn) handleServiceConfig(js string) error {
801 sc, err := parseServiceConfig(js)
812 // Close tears down the ClientConn and all underlying connections.
813 func (cc *ClientConn) Close() error {
819 return ErrClientConnClosing
823 cc.csMgr.updateState(connectivity.Shutdown)
825 rWrapper := cc.resolverWrapper
826 cc.resolverWrapper = nil
827 bWrapper := cc.balancerWrapper
828 cc.balancerWrapper = nil
830 cc.blockingpicker.close()
837 for ac := range conns {
838 ac.tearDown(ErrClientConnClosing)
843 // addrConn is a network connection to a given address.
844 type addrConn struct {
846 cancel context.CancelFunc
849 curAddr resolver.Address
850 addrs []resolver.Address
852 events trace.EventLog
853 acbw balancer.SubConn
856 state connectivity.State
857 // ready is closed and becomes nil when a new transport is up or failed
860 transport transport.ClientTransport
862 // The reason this addrConn is torn down.
866 // adjustParams updates parameters used to create transports upon
867 // receiving a GoAway.
868 func (ac *addrConn) adjustParams(r transport.GoAwayReason) {
870 case transport.TooManyPings:
871 v := 2 * ac.dopts.copts.KeepaliveParams.Time
873 if v > ac.cc.mkp.Time {
880 // printf records an event in ac's event log, unless ac has been closed.
881 // REQUIRES ac.mu is held.
882 func (ac *addrConn) printf(format string, a ...interface{}) {
883 if ac.events != nil {
884 ac.events.Printf(format, a...)
888 // errorf records an error in ac's event log, unless ac has been closed.
889 // REQUIRES ac.mu is held.
890 func (ac *addrConn) errorf(format string, a ...interface{}) {
891 if ac.events != nil {
892 ac.events.Errorf(format, a...)
896 // resetTransport recreates a transport to the address for ac. The old
897 // transport will close itself on error or when the clientconn is closed.
899 // TODO(bar) make sure all state transitions are valid.
900 func (ac *addrConn) resetTransport() error {
902 if ac.state == connectivity.Shutdown {
904 return errConnClosing
911 ac.curAddr = resolver.Address{}
914 ac.dopts.copts.KeepaliveParams = ac.cc.mkp
916 for retries := 0; ; retries++ {
917 sleepTime := ac.dopts.bs.backoff(retries)
918 timeout := minConnectTimeout
920 if timeout < time.Duration(int(sleepTime)/len(ac.addrs)) {
921 timeout = time.Duration(int(sleepTime) / len(ac.addrs))
923 connectTime := time.Now()
924 if ac.state == connectivity.Shutdown {
926 return errConnClosing
928 ac.printf("connecting")
929 if ac.state != connectivity.Connecting {
930 ac.state = connectivity.Connecting
931 ac.cc.handleSubConnStateChange(ac.acbw, ac.state)
933 // copy ac.addrs in case of race
934 addrsIter := make([]resolver.Address, len(ac.addrs))
935 copy(addrsIter, ac.addrs)
936 copts := ac.dopts.copts
938 for _, addr := range addrsIter {
940 if ac.state == connectivity.Shutdown {
941 // ac.tearDown(...) has been invoked.
943 return errConnClosing
946 sinfo := transport.TargetInfo{
948 Metadata: addr.Metadata,
949 Authority: ac.cc.authority,
951 newTransport, err := transport.NewClientTransport(ac.cc.ctx, sinfo, copts, timeout)
953 if e, ok := err.(transport.ConnectionError); ok && !e.Temporary() {
956 grpclog.Warningf("grpc: addrConn.resetTransport failed to create client transport: %v; Reconnecting to %v", err, addr)
958 if ac.state == connectivity.Shutdown {
959 // ac.tearDown(...) has been invoked.
961 return errConnClosing
968 if ac.state == connectivity.Shutdown {
969 // ac.tearDown(...) has been invoked.
972 return errConnClosing
974 ac.state = connectivity.Ready
975 ac.cc.handleSubConnStateChange(ac.acbw, ac.state)
977 ac.transport = newTransport
990 ac.state = connectivity.TransientFailure
991 ac.cc.handleSubConnStateChange(ac.acbw, ac.state)
997 timer := time.NewTimer(sleepTime - time.Since(connectTime))
1000 case <-ac.ctx.Done():
1008 // Run in a goroutine to track the error in transport and create the
1009 // new transport if an error happens. It returns when the channel is closing.
1010 func (ac *addrConn) transportMonitor() {
1015 // Block until we receive a goaway or an error occurs.
1020 // If a GoAway happened, regardless of error, adjust our keepalive
1021 // parameters as appropriate.
1024 ac.adjustParams(t.GetGoAwayReason())
1028 // Set connectivity state to TransientFailure before calling
1029 // resetTransport. Transition READY->CONNECTING is not valid.
1030 ac.state = connectivity.TransientFailure
1031 ac.cc.handleSubConnStateChange(ac.acbw, ac.state)
1032 ac.curAddr = resolver.Address{}
1034 if err := ac.resetTransport(); err != nil {
1036 ac.printf("transport exiting: %v", err)
1038 grpclog.Warningf("grpc: addrConn.transportMonitor exits due to: %v", err)
1039 if err != errConnClosing {
1040 // Keep this ac in cc.conns, to get the reason it's torn down.
1048 // wait blocks until i) the new transport is up or ii) ctx is done or iii) ac is closed or
1049 // iv) transport is in connectivity.TransientFailure and there is a balancer/failfast is true.
1050 func (ac *addrConn) wait(ctx context.Context, hasBalancer, failfast bool) (transport.ClientTransport, error) {
1054 case ac.state == connectivity.Shutdown:
1055 if failfast || !hasBalancer {
1056 // RPC is failfast or balancer is nil. This RPC should fail with ac.tearDownErr.
1057 err := ac.tearDownErr
1062 return nil, errConnClosing
1063 case ac.state == connectivity.Ready:
1067 case ac.state == connectivity.TransientFailure:
1068 if failfast || hasBalancer {
1070 return nil, errConnUnavailable
1075 ready = make(chan struct{})
1081 return nil, toRPCErr(ctx.Err())
1082 // Wait until the new transport is ready or failed.
1088 // getReadyTransport returns the transport if ac's state is READY.
1089 // Otherwise it returns nil, false.
1090 // If ac's state is IDLE, it will trigger ac to connect.
1091 func (ac *addrConn) getReadyTransport() (transport.ClientTransport, bool) {
1093 if ac.state == connectivity.Ready {
1099 if ac.state == connectivity.Idle {
1103 // Trigger idle ac to connect.
1110 // tearDown starts to tear down the addrConn.
1111 // TODO(zhaoq): Make this synchronous to avoid unbounded memory consumption in
1112 // some edge cases (e.g., the caller opens and closes many addrConn's in a
1114 // tearDown doesn't remove ac from ac.cc.conns.
1115 func (ac *addrConn) tearDown(err error) {
1118 defer ac.mu.Unlock()
1119 ac.curAddr = resolver.Address{}
1120 if err == errConnDrain && ac.transport != nil {
1121 // GracefulClose(...) may be executed multiple times when
1122 // i) receiving multiple GoAway frames from the server; or
1123 // ii) there are concurrent name resolver/Balancer triggered
1124 // address removal and GoAway.
1125 ac.transport.GracefulClose()
1127 if ac.state == connectivity.Shutdown {
1130 ac.state = connectivity.Shutdown
1131 ac.tearDownErr = err
1132 ac.cc.handleSubConnStateChange(ac.acbw, ac.state)
1133 if ac.events != nil {
1137 if ac.ready != nil {
1144 func (ac *addrConn) getState() connectivity.State {
1146 defer ac.mu.Unlock()