NameResolver.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPCCore
  17. /// A name resolver can provide resolved addresses and service configuration values over time.
  18. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  19. public struct NameResolver: Sendable {
  20. /// A sequence of name resolution results.
  21. ///
  22. /// Resolvers may be push or pull based. Resolvers with the ``UpdateMode-swift.enum/push``
  23. /// update mode have addresses pushed to them by an external source and you should subscribe
  24. /// to changes in addresses by awaiting for new values in a loop.
  25. ///
  26. /// Resolvers with the ``UpdateMode-swift.enum/pull`` update mode shouldn't be subscribed to,
  27. /// instead you should create an iterator and ask for new results as and when necessary.
  28. public var names: RPCAsyncSequence<NameResolutionResult>
  29. /// How ``names`` is updated and should be consumed.
  30. public let updateMode: UpdateMode
  31. public struct UpdateMode: Hashable, Sendable {
  32. enum Value: Hashable, Sendable {
  33. case push
  34. case pull
  35. }
  36. let value: Value
  37. private init(_ value: Value) {
  38. self.value = value
  39. }
  40. /// Addresses are pushed to the resolve by an external source.
  41. public static var push: Self { Self(.push) }
  42. /// Addresses are resolved lazily, when the caller asks them to be resolved.
  43. public static var pull: Self { Self(.pull) }
  44. }
  45. /// Create a new name resolver.
  46. public init(results: RPCAsyncSequence<NameResolutionResult>, updateMode: UpdateMode) {
  47. self.names = results
  48. self.updateMode = updateMode
  49. }
  50. }
  51. /// The result of name resolution, a list of endpoints to connect to and the service
  52. /// configuration reported by the resolver.
  53. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  54. public struct NameResolutionResult: Hashable, Sendable {
  55. /// A list of endpoints to connect to.
  56. public var endpoints: [Endpoint]
  57. /// The service configuration reported by the resolver, or an error if it couldn't be parsed.
  58. public var serviceConfiguration: Result<ServiceConfiguration, RPCError>
  59. public init(
  60. endpoints: [Endpoint],
  61. serviceConfiguration: Result<ServiceConfiguration, RPCError>
  62. ) {
  63. self.endpoints = endpoints
  64. self.serviceConfiguration = serviceConfiguration
  65. }
  66. }
  67. /// A group of addresses which are considered equivalent when establishing a connection.
  68. public struct Endpoint: Hashable, Sendable {
  69. /// A list of equivalent addresses.
  70. ///
  71. /// Earlier addresses are typically but not always connected to first. Some load balancers may
  72. /// choose to ignore the order.
  73. public var addresses: [SocketAddress]
  74. /// Create a new ``Endpoint``.
  75. /// - Parameter addresses: A list of equivalent addresses.
  76. public init(addresses: [SocketAddress]) {
  77. self.addresses = addresses
  78. }
  79. }
  80. /// A resolver capable of resolving targets of type ``Target``.
  81. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  82. public protocol NameResolverFactory<Target> {
  83. /// The type of ``ResolvableTarget`` this factory makes resolvers for.
  84. associatedtype Target: ResolvableTarget
  85. /// Creates a resolver for the given target.
  86. ///
  87. /// - Parameter target: The target to make a resolver for.
  88. /// - Returns: The name resolver for the target.
  89. func resolver(for target: Target) -> NameResolver
  90. }
  91. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  92. extension NameResolverFactory {
  93. /// Returns whether the given target is compatible with this factory.
  94. ///
  95. /// - Parameter target: The target to check the compatibility of.
  96. /// - Returns: Whether the target is compatible with this factory.
  97. func isCompatible<Other: ResolvableTarget>(withTarget target: Other) -> Bool {
  98. return target is Target
  99. }
  100. /// Returns a name resolver if the given target is compatible.
  101. ///
  102. /// - Parameter target: The target to make a name resolver for.
  103. /// - Returns: A name resolver or `nil` if the target isn't compatible.
  104. func makeResolverIfCompatible<Other: ResolvableTarget>(_ target: Other) -> NameResolver? {
  105. guard let target = target as? Target else { return nil }
  106. return self.resolver(for: target)
  107. }
  108. }
  109. /// A target which can be resolved to a ``SocketAddress``.
  110. public protocol ResolvableTarget {}