resolver.go 9.7 KB

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