Skip to content

Commit

Permalink
parse target in clientconn dial
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Oct 20, 2017
1 parent 2bca6ad commit cf5713f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
31 changes: 16 additions & 15 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
29 changes: 13 additions & 16 deletions resolver_conn_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
}
Expand Down

0 comments on commit cf5713f

Please sign in to comment.