diff --git a/clientconn.go b/clientconn.go index 03ec304ba6e5..cdf5c3118930 100644 --- a/clientconn.go +++ b/clientconn.go @@ -446,6 +446,17 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * if cc.dopts.scChan != nil { go cc.scWatcher() } + cc.parsedTarget = parseTarget(cc.target) + creds := cc.dopts.copts.TransportCredentials + if creds != nil && creds.Info().ServerName != "" { + cc.authority = creds.Info().ServerName + } else if cc.dopts.insecure && cc.dopts.copts.Authority != "" { + cc.authority = cc.dopts.copts.Authority + } else { + // Use endpoint from "scheme://authority/endpoint" as the default + // authority for ClientConn. + cc.authority = cc.parsedTarget.Endpoint + } var credsClone credentials.TransportCredentials if creds := cc.dopts.copts.TransportCredentials; creds != nil { @@ -468,17 +479,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * return nil, fmt.Errorf("failed to build resolver: %v", err) } - creds := cc.dopts.copts.TransportCredentials - if creds != nil && creds.Info().ServerName != "" { - cc.authority = creds.Info().ServerName - } else if cc.dopts.insecure && cc.dopts.copts.Authority != "" { - cc.authority = cc.dopts.copts.Authority - } else { - // Use endpoint from "scheme://authority/endpoint" as the default - // authority for ClientConn. - cc.authority = cc.resolverWrapper.parsedTarget.Endpoint - } - // A blocking dial blocks until the clientConn is ready. if cc.dopts.block { for { @@ -544,10 +544,11 @@ type ClientConn struct { ctx context.Context cancel context.CancelFunc - target string - authority string - dopts dialOptions - csMgr *connectivityStateManager + target string + parsedTarget resolver.Target + authority string + dopts dialOptions + csMgr *connectivityStateManager customBalancer bool // If this is true, switching balancer will be disabled. balancerBuildOpts balancer.BuildOptions diff --git a/resolver_conn_wrapper.go b/resolver_conn_wrapper.go index 165609af1b87..53fb77a6c6dc 100644 --- a/resolver_conn_wrapper.go +++ b/resolver_conn_wrapper.go @@ -29,12 +29,11 @@ import ( // ccResolverWrapper is a wrapper on top of cc for resolvers. // It implements resolver.ClientConnection interface. type ccResolverWrapper struct { - cc *ClientConn - parsedTarget resolver.Target - resolver resolver.Resolver - addrCh chan []resolver.Address - scCh chan string - done chan struct{} + cc *ClientConn + resolver resolver.Resolver + addrCh chan []resolver.Address + scCh chan string + done chan struct{} } // split2 returns the values from strings.SplitN(s, sep, 2). @@ -59,24 +58,22 @@ func parseTarget(target string) (ret resolver.Target) { // builder for this scheme. It then builds the resolver and starts the // monitoring goroutine for it. func newCCResolverWrapper(cc *ClientConn) (*ccResolverWrapper, error) { - target := parseTarget(cc.target) - grpclog.Infof("dialing to target with scheme: %q", target.Scheme) + grpclog.Infof("dialing to target with scheme: %q", cc.parsedTarget.Scheme) - rb := resolver.Get(target.Scheme) + rb := resolver.Get(cc.parsedTarget.Scheme) if rb == nil { - return nil, fmt.Errorf("could not get resolver for scheme: %q", target.Scheme) + return nil, fmt.Errorf("could not get resolver for scheme: %q", cc.parsedTarget.Scheme) } ccr := &ccResolverWrapper{ - cc: cc, - parsedTarget: target, - addrCh: make(chan []resolver.Address, 1), - scCh: make(chan string, 1), - done: make(chan struct{}), + cc: cc, + addrCh: make(chan []resolver.Address, 1), + scCh: make(chan string, 1), + done: make(chan struct{}), } var err error - ccr.resolver, err = rb.Build(target, ccr, resolver.BuildOption{}) + ccr.resolver, err = rb.Build(cc.parsedTarget, ccr, resolver.BuildOption{}) if err != nil { return nil, err }