3 * Copyright 2017 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.
25 "google.golang.org/grpc/grpclog"
26 "google.golang.org/grpc/resolver"
29 // ccResolverWrapper is a wrapper on top of cc for resolvers.
30 // It implements resolver.ClientConnection interface.
31 type ccResolverWrapper struct {
33 resolver resolver.Resolver
34 addrCh chan []resolver.Address
39 // split2 returns the values from strings.SplitN(s, sep, 2).
40 // If sep is not found, it returns ("", s, false) instead.
41 func split2(s, sep string) (string, string, bool) {
42 spl := strings.SplitN(s, sep, 2)
46 return spl[0], spl[1], true
49 // parseTarget splits target into a struct containing scheme, authority and
51 func parseTarget(target string) (ret resolver.Target) {
53 ret.Scheme, ret.Endpoint, ok = split2(target, "://")
55 return resolver.Target{Endpoint: target}
57 ret.Authority, ret.Endpoint, _ = split2(ret.Endpoint, "/")
61 // newCCResolverWrapper parses cc.target for scheme and gets the resolver
62 // builder for this scheme. It then builds the resolver and starts the
63 // monitoring goroutine for it.
64 func newCCResolverWrapper(cc *ClientConn) (*ccResolverWrapper, error) {
65 grpclog.Infof("dialing to target with scheme: %q", cc.parsedTarget.Scheme)
67 rb := resolver.Get(cc.parsedTarget.Scheme)
69 return nil, fmt.Errorf("could not get resolver for scheme: %q", cc.parsedTarget.Scheme)
72 ccr := &ccResolverWrapper{
74 addrCh: make(chan []resolver.Address, 1),
75 scCh: make(chan string, 1),
76 done: make(chan struct{}),
80 ccr.resolver, err = rb.Build(cc.parsedTarget, ccr, resolver.BuildOption{})
88 // watcher processes address updates and service config updates sequencially.
89 // Otherwise, we need to resolve possible races between address and service
90 // config (e.g. they specify different balancer types).
91 func (ccr *ccResolverWrapper) watcher() {
100 case addrs := <-ccr.addrCh:
106 grpclog.Infof("ccResolverWrapper: sending new addresses to cc: %v", addrs)
107 ccr.cc.handleResolvedAddrs(addrs, nil)
108 case sc := <-ccr.scCh:
114 grpclog.Infof("ccResolverWrapper: got new service config: %v", sc)
115 ccr.cc.handleServiceConfig(sc)
122 func (ccr *ccResolverWrapper) close() {
127 // NewAddress is called by the resolver implemenetion to send addresses to gRPC.
128 func (ccr *ccResolverWrapper) NewAddress(addrs []resolver.Address) {
136 // NewServiceConfig is called by the resolver implemenetion to send service
138 func (ccr *ccResolverWrapper) NewServiceConfig(sc string) {