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/internal/channelz"
27 "google.golang.org/grpc/resolver"
30 // ccResolverWrapper is a wrapper on top of cc for resolvers.
31 // It implements resolver.ClientConnection interface.
32 type ccResolverWrapper struct {
34 resolver resolver.Resolver
35 addrCh chan []resolver.Address
38 lastAddressesCount int
41 // split2 returns the values from strings.SplitN(s, sep, 2).
42 // If sep is not found, it returns ("", "", false) instead.
43 func split2(s, sep string) (string, string, bool) {
44 spl := strings.SplitN(s, sep, 2)
48 return spl[0], spl[1], true
51 // parseTarget splits target into a struct containing scheme, authority and
54 // If target is not a valid scheme://authority/endpoint, it returns {Endpoint:
56 func parseTarget(target string) (ret resolver.Target) {
58 ret.Scheme, ret.Endpoint, ok = split2(target, "://")
60 return resolver.Target{Endpoint: target}
62 ret.Authority, ret.Endpoint, ok = split2(ret.Endpoint, "/")
64 return resolver.Target{Endpoint: target}
69 // newCCResolverWrapper parses cc.target for scheme and gets the resolver
70 // builder for this scheme and builds the resolver. The monitoring goroutine
71 // for it is not started yet and can be created by calling start().
73 // If withResolverBuilder dial option is set, the specified resolver will be
75 func newCCResolverWrapper(cc *ClientConn) (*ccResolverWrapper, error) {
76 rb := cc.dopts.resolverBuilder
78 return nil, fmt.Errorf("could not get resolver for scheme: %q", cc.parsedTarget.Scheme)
81 ccr := &ccResolverWrapper{
83 addrCh: make(chan []resolver.Address, 1),
84 scCh: make(chan string, 1),
85 done: make(chan struct{}),
89 ccr.resolver, err = rb.Build(cc.parsedTarget, ccr, resolver.BuildOption{DisableServiceConfig: cc.dopts.disableServiceConfig})
96 func (ccr *ccResolverWrapper) resolveNow(o resolver.ResolveNowOption) {
97 ccr.resolver.ResolveNow(o)
100 func (ccr *ccResolverWrapper) close() {
105 // NewAddress is called by the resolver implemenetion to send addresses to gRPC.
106 func (ccr *ccResolverWrapper) NewAddress(addrs []resolver.Address) {
112 grpclog.Infof("ccResolverWrapper: sending new addresses to cc: %v", addrs)
114 ccr.addChannelzTraceEvent(addrs)
116 ccr.cc.handleResolvedAddrs(addrs, nil)
119 // NewServiceConfig is called by the resolver implemenetion to send service
121 func (ccr *ccResolverWrapper) NewServiceConfig(sc string) {
127 grpclog.Infof("ccResolverWrapper: got new service config: %v", sc)
128 ccr.cc.handleServiceConfig(sc)
131 func (ccr *ccResolverWrapper) addChannelzTraceEvent(addrs []resolver.Address) {
132 if len(addrs) == 0 && ccr.lastAddressesCount != 0 {
133 channelz.AddTraceEvent(ccr.cc.channelzID, &channelz.TraceEventDesc{
134 Desc: "Resolver returns an empty address list",
135 Severity: channelz.CtWarning,
137 } else if len(addrs) != 0 && ccr.lastAddressesCount == 0 {
139 for i, a := range addrs {
140 if a.ServerName != "" {
141 s += a.Addr + "(" + a.ServerName + ")"
145 if i != len(addrs)-1 {
149 channelz.AddTraceEvent(ccr.cc.channelzID, &channelz.TraceEventDesc{
150 Desc: fmt.Sprintf("Resolver returns a non-empty address list (previous one was empty) %q", s),
151 Severity: channelz.CtINFO,
154 ccr.lastAddressesCount = len(addrs)