resolver.go 5.2 KB

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