resolver.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 resolver defines APIs for name resolution in gRPC.
  19. // All APIs in this package are experimental.
  20. package resolver
  21. import (
  22. "google.golang.org/grpc/serviceconfig"
  23. )
  24. var (
  25. // m is a map from scheme to resolver builder.
  26. m = make(map[string]Builder)
  27. // defaultScheme is the default scheme to use.
  28. defaultScheme = "passthrough"
  29. )
  30. // TODO(bar) install dns resolver in init(){}.
  31. // Register registers the resolver builder to the resolver map. b.Scheme will be
  32. // used as the scheme registered with this builder.
  33. //
  34. // NOTE: this function must only be called during initialization time (i.e. in
  35. // an init() function), and is not thread-safe. If multiple Resolvers are
  36. // registered with the same name, the one registered last will take effect.
  37. func Register(b Builder) {
  38. m[b.Scheme()] = b
  39. }
  40. // Get returns the resolver builder registered with the given scheme.
  41. //
  42. // If no builder is register with the scheme, nil will be returned.
  43. func Get(scheme string) Builder {
  44. if b, ok := m[scheme]; ok {
  45. return b
  46. }
  47. return nil
  48. }
  49. // SetDefaultScheme sets the default scheme that will be used. The default
  50. // default scheme is "passthrough".
  51. //
  52. // NOTE: this function must only be called during initialization time (i.e. in
  53. // an init() function), and is not thread-safe. The scheme set last overrides
  54. // previously set values.
  55. func SetDefaultScheme(scheme string) {
  56. defaultScheme = scheme
  57. }
  58. // GetDefaultScheme gets the default scheme that will be used.
  59. func GetDefaultScheme() string {
  60. return defaultScheme
  61. }
  62. // AddressType indicates the address type returned by name resolution.
  63. type AddressType uint8
  64. const (
  65. // Backend indicates the address is for a backend server.
  66. Backend AddressType = iota
  67. // GRPCLB indicates the address is for a grpclb load balancer.
  68. GRPCLB
  69. )
  70. // Address represents a server the client connects to.
  71. // This is the EXPERIMENTAL API and may be changed or extended in the future.
  72. type Address struct {
  73. // Addr is the server address on which a connection will be established.
  74. Addr string
  75. // Type is the type of this address.
  76. Type AddressType
  77. // ServerName is the name of this address.
  78. //
  79. // e.g. if Type is GRPCLB, ServerName should be the name of the remote load
  80. // balancer, not the name of the backend.
  81. ServerName string
  82. // Metadata is the information associated with Addr, which may be used
  83. // to make load balancing decision.
  84. Metadata interface{}
  85. }
  86. // BuildOption includes additional information for the builder to create
  87. // the resolver.
  88. type BuildOption struct {
  89. // DisableServiceConfig indicates whether resolver should fetch service config data.
  90. DisableServiceConfig bool
  91. }
  92. // State contains the current Resolver state relevant to the ClientConn.
  93. type State struct {
  94. Addresses []Address // Resolved addresses for the target
  95. // ServiceConfig is the parsed service config; obtained from
  96. // serviceconfig.Parse.
  97. ServiceConfig serviceconfig.Config
  98. // TODO: add Err error
  99. }
  100. // ClientConn contains the callbacks for resolver to notify any updates
  101. // to the gRPC ClientConn.
  102. //
  103. // This interface is to be implemented by gRPC. Users should not need a
  104. // brand new implementation of this interface. For the situations like
  105. // testing, the new implementation should embed this interface. This allows
  106. // gRPC to add new methods to this interface.
  107. type ClientConn interface {
  108. // UpdateState updates the state of the ClientConn appropriately.
  109. UpdateState(State)
  110. // NewAddress is called by resolver to notify ClientConn a new list
  111. // of resolved addresses.
  112. // The address list should be the complete list of resolved addresses.
  113. //
  114. // Deprecated: Use UpdateState instead.
  115. NewAddress(addresses []Address)
  116. // NewServiceConfig is called by resolver to notify ClientConn a new
  117. // service config. The service config should be provided as a json string.
  118. //
  119. // Deprecated: Use UpdateState instead.
  120. NewServiceConfig(serviceConfig string)
  121. }
  122. // Target represents a target for gRPC, as specified in:
  123. // https://github.com/grpc/grpc/blob/master/doc/naming.md.
  124. // It is parsed from the target string that gets passed into Dial or DialContext by the user. And
  125. // grpc passes it to the resolver and the balancer.
  126. //
  127. // If the target follows the naming spec, and the parsed scheme is registered with grpc, we will
  128. // parse the target string according to the spec. e.g. "dns://some_authority/foo.bar" will be parsed
  129. // into &Target{Scheme: "dns", Authority: "some_authority", Endpoint: "foo.bar"}
  130. //
  131. // If the target does not contain a scheme, we will apply the default scheme, and set the Target to
  132. // be the full target string. e.g. "foo.bar" will be parsed into
  133. // &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "foo.bar"}.
  134. //
  135. // If the parsed scheme is not registered (i.e. no corresponding resolver available to resolve the
  136. // endpoint), we set the Scheme to be the default scheme, and set the Endpoint to be the full target
  137. // string. e.g. target string "unknown_scheme://authority/endpoint" will be parsed into
  138. // &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "unknown_scheme://authority/endpoint"}.
  139. type Target struct {
  140. Scheme string
  141. Authority string
  142. Endpoint string
  143. }
  144. // Builder creates a resolver that will be used to watch name resolution updates.
  145. type Builder interface {
  146. // Build creates a new resolver for the given target.
  147. //
  148. // gRPC dial calls Build synchronously, and fails if the returned error is
  149. // not nil.
  150. Build(target Target, cc ClientConn, opts BuildOption) (Resolver, error)
  151. // Scheme returns the scheme supported by this resolver.
  152. // Scheme is defined at https://github.com/grpc/grpc/blob/master/doc/naming.md.
  153. Scheme() string
  154. }
  155. // ResolveNowOption includes additional information for ResolveNow.
  156. type ResolveNowOption struct{}
  157. // Resolver watches for the updates on the specified target.
  158. // Updates include address updates and service config updates.
  159. type Resolver interface {
  160. // ResolveNow will be called by gRPC to try to resolve the target name
  161. // again. It's just a hint, resolver can ignore this if it's not necessary.
  162. //
  163. // It could be called multiple times concurrently.
  164. ResolveNow(ResolveNowOption)
  165. // Close closes the resolver.
  166. Close()
  167. }
  168. // UnregisterForTesting removes the resolver builder with the given scheme from the
  169. // resolver map.
  170. // This function is for testing only.
  171. func UnregisterForTesting(scheme string) {
  172. delete(m, scheme)
  173. }