resolver.go 9.5 KB

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