resolver.go 5.3 KB

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