resolver_conn_wrapper.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "fmt"
  21. "strings"
  22. "sync/atomic"
  23. "google.golang.org/grpc/grpclog"
  24. "google.golang.org/grpc/internal/channelz"
  25. "google.golang.org/grpc/resolver"
  26. )
  27. // ccResolverWrapper is a wrapper on top of cc for resolvers.
  28. // It implements resolver.ClientConnection interface.
  29. type ccResolverWrapper struct {
  30. cc *ClientConn
  31. resolver resolver.Resolver
  32. addrCh chan []resolver.Address
  33. scCh chan string
  34. done uint32 // accessed atomically; set to 1 when closed.
  35. curState resolver.State
  36. }
  37. // split2 returns the values from strings.SplitN(s, sep, 2).
  38. // If sep is not found, it returns ("", "", false) instead.
  39. func split2(s, sep string) (string, string, bool) {
  40. spl := strings.SplitN(s, sep, 2)
  41. if len(spl) < 2 {
  42. return "", "", false
  43. }
  44. return spl[0], spl[1], true
  45. }
  46. // parseTarget splits target into a struct containing scheme, authority and
  47. // endpoint.
  48. //
  49. // If target is not a valid scheme://authority/endpoint, it returns {Endpoint:
  50. // target}.
  51. func parseTarget(target string) (ret resolver.Target) {
  52. var ok bool
  53. ret.Scheme, ret.Endpoint, ok = split2(target, "://")
  54. if !ok {
  55. return resolver.Target{Endpoint: target}
  56. }
  57. ret.Authority, ret.Endpoint, ok = split2(ret.Endpoint, "/")
  58. if !ok {
  59. return resolver.Target{Endpoint: target}
  60. }
  61. return ret
  62. }
  63. // newCCResolverWrapper parses cc.target for scheme and gets the resolver
  64. // builder for this scheme and builds the resolver. The monitoring goroutine
  65. // for it is not started yet and can be created by calling start().
  66. //
  67. // If withResolverBuilder dial option is set, the specified resolver will be
  68. // used instead.
  69. func newCCResolverWrapper(cc *ClientConn) (*ccResolverWrapper, error) {
  70. rb := cc.dopts.resolverBuilder
  71. if rb == nil {
  72. return nil, fmt.Errorf("could not get resolver for scheme: %q", cc.parsedTarget.Scheme)
  73. }
  74. ccr := &ccResolverWrapper{
  75. cc: cc,
  76. addrCh: make(chan []resolver.Address, 1),
  77. scCh: make(chan string, 1),
  78. }
  79. var err error
  80. ccr.resolver, err = rb.Build(cc.parsedTarget, ccr, resolver.BuildOption{DisableServiceConfig: cc.dopts.disableServiceConfig})
  81. if err != nil {
  82. return nil, err
  83. }
  84. return ccr, nil
  85. }
  86. func (ccr *ccResolverWrapper) resolveNow(o resolver.ResolveNowOption) {
  87. ccr.resolver.ResolveNow(o)
  88. }
  89. func (ccr *ccResolverWrapper) close() {
  90. ccr.resolver.Close()
  91. atomic.StoreUint32(&ccr.done, 1)
  92. }
  93. func (ccr *ccResolverWrapper) isDone() bool {
  94. return atomic.LoadUint32(&ccr.done) == 1
  95. }
  96. func (ccr *ccResolverWrapper) UpdateState(s resolver.State) {
  97. if ccr.isDone() {
  98. return
  99. }
  100. grpclog.Infof("ccResolverWrapper: sending update to cc: %v", s)
  101. if channelz.IsOn() {
  102. ccr.addChannelzTraceEvent(s)
  103. }
  104. ccr.cc.updateResolverState(s)
  105. ccr.curState = s
  106. }
  107. // NewAddress is called by the resolver implementation to send addresses to gRPC.
  108. func (ccr *ccResolverWrapper) NewAddress(addrs []resolver.Address) {
  109. if ccr.isDone() {
  110. return
  111. }
  112. grpclog.Infof("ccResolverWrapper: sending new addresses to cc: %v", addrs)
  113. if channelz.IsOn() {
  114. ccr.addChannelzTraceEvent(resolver.State{Addresses: addrs, ServiceConfig: ccr.curState.ServiceConfig})
  115. }
  116. ccr.curState.Addresses = addrs
  117. ccr.cc.updateResolverState(ccr.curState)
  118. }
  119. // NewServiceConfig is called by the resolver implementation to send service
  120. // configs to gRPC.
  121. func (ccr *ccResolverWrapper) NewServiceConfig(sc string) {
  122. if ccr.isDone() {
  123. return
  124. }
  125. grpclog.Infof("ccResolverWrapper: got new service config: %v", sc)
  126. if channelz.IsOn() {
  127. ccr.addChannelzTraceEvent(resolver.State{Addresses: ccr.curState.Addresses, ServiceConfig: sc})
  128. }
  129. ccr.curState.ServiceConfig = sc
  130. ccr.cc.updateResolverState(ccr.curState)
  131. }
  132. func (ccr *ccResolverWrapper) addChannelzTraceEvent(s resolver.State) {
  133. if s.ServiceConfig == ccr.curState.ServiceConfig && (len(ccr.curState.Addresses) == 0) == (len(s.Addresses) == 0) {
  134. return
  135. }
  136. var updates []string
  137. if s.ServiceConfig != ccr.curState.ServiceConfig {
  138. updates = append(updates, "service config updated")
  139. }
  140. if len(ccr.curState.Addresses) > 0 && len(s.Addresses) == 0 {
  141. updates = append(updates, "resolver returned an empty address list")
  142. } else if len(ccr.curState.Addresses) == 0 && len(s.Addresses) > 0 {
  143. updates = append(updates, "resolver returned new addresses")
  144. }
  145. channelz.AddTraceEvent(ccr.cc.channelzID, &channelz.TraceEventDesc{
  146. Desc: fmt.Sprintf("Resolver state updated: %+v (%v)", s, strings.Join(updates, "; ")),
  147. Severity: channelz.CtINFO,
  148. })
  149. }