resolver.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. "context"
  23. "net"
  24. "google.golang.org/grpc/attributes"
  25. "google.golang.org/grpc/credentials"
  26. "google.golang.org/grpc/serviceconfig"
  27. )
  28. var (
  29. // m is a map from scheme to resolver builder.
  30. m = make(map[string]Builder)
  31. // defaultScheme is the default scheme to use.
  32. defaultScheme = "passthrough"
  33. )
  34. // TODO(bar) install dns resolver in init(){}.
  35. // Register registers the resolver builder to the resolver map. b.Scheme will be
  36. // used as the scheme registered with this builder.
  37. //
  38. // NOTE: this function must only be called during initialization time (i.e. in
  39. // an init() function), and is not thread-safe. If multiple Resolvers are
  40. // registered with the same name, the one registered last will take effect.
  41. func Register(b Builder) {
  42. m[b.Scheme()] = b
  43. }
  44. // Get returns the resolver builder registered with the given scheme.
  45. //
  46. // If no builder is register with the scheme, nil will be returned.
  47. func Get(scheme string) Builder {
  48. if b, ok := m[scheme]; ok {
  49. return b
  50. }
  51. return nil
  52. }
  53. // SetDefaultScheme sets the default scheme that will be used. The default
  54. // default scheme is "passthrough".
  55. //
  56. // NOTE: this function must only be called during initialization time (i.e. in
  57. // an init() function), and is not thread-safe. The scheme set last overrides
  58. // previously set values.
  59. func SetDefaultScheme(scheme string) {
  60. defaultScheme = scheme
  61. }
  62. // GetDefaultScheme gets the default scheme that will be used.
  63. func GetDefaultScheme() string {
  64. return defaultScheme
  65. }
  66. // AddressType indicates the address type returned by name resolution.
  67. //
  68. // Deprecated: use Attributes in Address instead.
  69. type AddressType uint8
  70. const (
  71. // Backend indicates the address is for a backend server.
  72. //
  73. // Deprecated: use Attributes in Address instead.
  74. Backend AddressType = iota
  75. // GRPCLB indicates the address is for a grpclb load balancer.
  76. //
  77. // Deprecated: use Attributes in Address instead.
  78. GRPCLB
  79. )
  80. // Address represents a server the client connects to.
  81. // This is the EXPERIMENTAL API and may be changed or extended in the future.
  82. type Address struct {
  83. // Addr is the server address on which a connection will be established.
  84. Addr string
  85. // ServerName is the name of this address.
  86. // If non-empty, the ServerName is used as the transport certification authority for
  87. // the address, instead of the hostname from the Dial target string. In most cases,
  88. // this should not be set.
  89. //
  90. // If Type is GRPCLB, ServerName should be the name of the remote load
  91. // balancer, not the name of the backend.
  92. //
  93. // WARNING: ServerName must only be populated with trusted values. It
  94. // is insecure to populate it with data from untrusted inputs since untrusted
  95. // values could be used to bypass the authority checks performed by TLS.
  96. ServerName string
  97. // Attributes contains arbitrary data about this address intended for
  98. // consumption by the load balancing policy.
  99. Attributes *attributes.Attributes
  100. // Type is the type of this address.
  101. //
  102. // Deprecated: use Attributes instead.
  103. Type AddressType
  104. // Metadata is the information associated with Addr, which may be used
  105. // to make load balancing decision.
  106. //
  107. // Deprecated: use Attributes instead.
  108. Metadata interface{}
  109. }
  110. // BuildOption is a type alias of BuildOptions for legacy reasons.
  111. //
  112. // Deprecated: use BuildOptions instead.
  113. type BuildOption = BuildOptions
  114. // BuildOptions includes additional information for the builder to create
  115. // the resolver.
  116. type BuildOptions struct {
  117. // DisableServiceConfig indicates whether a resolver implementation should
  118. // fetch service config data.
  119. DisableServiceConfig bool
  120. // DialCreds is the transport credentials used by the ClientConn for
  121. // communicating with the target gRPC service (set via
  122. // WithTransportCredentials). In cases where a name resolution service
  123. // requires the same credentials, the resolver may use this field. In most
  124. // cases though, it is not appropriate, and this field may be ignored.
  125. DialCreds credentials.TransportCredentials
  126. // CredsBundle is the credentials bundle used by the ClientConn for
  127. // communicating with the target gRPC service (set via
  128. // WithCredentialsBundle). In cases where a name resolution service
  129. // requires the same credentials, the resolver may use this field. In most
  130. // cases though, it is not appropriate, and this field may be ignored.
  131. CredsBundle credentials.Bundle
  132. // Dialer is the custom dialer used by the ClientConn for dialling the
  133. // target gRPC service (set via WithDialer). In cases where a name
  134. // resolution service requires the same dialer, the resolver may use this
  135. // field. In most cases though, it is not appropriate, and this field may
  136. // be ignored.
  137. Dialer func(context.Context, string) (net.Conn, error)
  138. }
  139. // State contains the current Resolver state relevant to the ClientConn.
  140. type State struct {
  141. // Addresses is the latest set of resolved addresses for the target.
  142. Addresses []Address
  143. // ServiceConfig contains the result from parsing the latest service
  144. // config. If it is nil, it indicates no service config is present or the
  145. // resolver does not provide service configs.
  146. ServiceConfig *serviceconfig.ParseResult
  147. // Attributes contains arbitrary data about the resolver intended for
  148. // consumption by the load balancing policy.
  149. Attributes *attributes.Attributes
  150. }
  151. // ClientConn contains the callbacks for resolver to notify any updates
  152. // to the gRPC ClientConn.
  153. //
  154. // This interface is to be implemented by gRPC. Users should not need a
  155. // brand new implementation of this interface. For the situations like
  156. // testing, the new implementation should embed this interface. This allows
  157. // gRPC to add new methods to this interface.
  158. type ClientConn interface {
  159. // UpdateState updates the state of the ClientConn appropriately.
  160. UpdateState(State)
  161. // ReportError notifies the ClientConn that the Resolver encountered an
  162. // error. The ClientConn will notify the load balancer and begin calling
  163. // ResolveNow on the Resolver with exponential backoff.
  164. ReportError(error)
  165. // NewAddress is called by resolver to notify ClientConn a new list
  166. // of resolved addresses.
  167. // The address list should be the complete list of resolved addresses.
  168. //
  169. // Deprecated: Use UpdateState instead.
  170. NewAddress(addresses []Address)
  171. // NewServiceConfig is called by resolver to notify ClientConn a new
  172. // service config. The service config should be provided as a json string.
  173. //
  174. // Deprecated: Use UpdateState instead.
  175. NewServiceConfig(serviceConfig string)
  176. // ParseServiceConfig parses the provided service config and returns an
  177. // object that provides the parsed config.
  178. ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult
  179. }
  180. // Target represents a target for gRPC, as specified in:
  181. // https://github.com/grpc/grpc/blob/master/doc/naming.md.
  182. // It is parsed from the target string that gets passed into Dial or DialContext by the user. And
  183. // grpc passes it to the resolver and the balancer.
  184. //
  185. // If the target follows the naming spec, and the parsed scheme is registered with grpc, we will
  186. // parse the target string according to the spec. e.g. "dns://some_authority/foo.bar" will be parsed
  187. // into &Target{Scheme: "dns", Authority: "some_authority", Endpoint: "foo.bar"}
  188. //
  189. // If the target does not contain a scheme, we will apply the default scheme, and set the Target to
  190. // be the full target string. e.g. "foo.bar" will be parsed into
  191. // &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "foo.bar"}.
  192. //
  193. // If the parsed scheme is not registered (i.e. no corresponding resolver available to resolve the
  194. // endpoint), we set the Scheme to be the default scheme, and set the Endpoint to be the full target
  195. // string. e.g. target string "unknown_scheme://authority/endpoint" will be parsed into
  196. // &Target{Scheme: resolver.GetDefaultScheme(), Endpoint: "unknown_scheme://authority/endpoint"}.
  197. type Target struct {
  198. Scheme string
  199. Authority string
  200. Endpoint string
  201. }
  202. // Builder creates a resolver that will be used to watch name resolution updates.
  203. type Builder interface {
  204. // Build creates a new resolver for the given target.
  205. //
  206. // gRPC dial calls Build synchronously, and fails if the returned error is
  207. // not nil.
  208. Build(target Target, cc ClientConn, opts BuildOptions) (Resolver, error)
  209. // Scheme returns the scheme supported by this resolver.
  210. // Scheme is defined at https://github.com/grpc/grpc/blob/master/doc/naming.md.
  211. Scheme() string
  212. }
  213. // ResolveNowOption is a type alias of ResolveNowOptions for legacy reasons.
  214. //
  215. // Deprecated: use ResolveNowOptions instead.
  216. type ResolveNowOption = ResolveNowOptions
  217. // ResolveNowOptions includes additional information for ResolveNow.
  218. type ResolveNowOptions struct{}
  219. // Resolver watches for the updates on the specified target.
  220. // Updates include address updates and service config updates.
  221. type Resolver interface {
  222. // ResolveNow will be called by gRPC to try to resolve the target name
  223. // again. It's just a hint, resolver can ignore this if it's not necessary.
  224. //
  225. // It could be called multiple times concurrently.
  226. ResolveNow(ResolveNowOptions)
  227. // Close closes the resolver.
  228. Close()
  229. }
  230. // UnregisterForTesting removes the resolver builder with the given scheme from the
  231. // resolver map.
  232. // This function is for testing only.
  233. func UnregisterForTesting(scheme string) {
  234. delete(m, scheme)
  235. }