resolver.go 9.7 KB

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