NameResolver.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public import GRPCCore
  17. /// A name resolver can provide resolved addresses and service configuration values over time.
  18. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.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, any Error>
  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(names: RPCAsyncSequence<NameResolutionResult, any Error>, updateMode: UpdateMode) {
  47. self.names = names
  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. /// This value may be `nil` if the resolver doesn't support fetching service configuration.
  59. public var serviceConfig: Result<ServiceConfig, RPCError>?
  60. public init(
  61. endpoints: [Endpoint],
  62. serviceConfig: Result<ServiceConfig, RPCError>?
  63. ) {
  64. self.endpoints = endpoints
  65. self.serviceConfig = serviceConfig
  66. }
  67. }
  68. /// A group of addresses which are considered equivalent when establishing a connection.
  69. public struct Endpoint: Hashable, Sendable {
  70. /// A list of equivalent addresses.
  71. ///
  72. /// Earlier addresses are typically but not always connected to first. Some load balancers may
  73. /// choose to ignore the order.
  74. public var addresses: [SocketAddress]
  75. /// Create a new ``Endpoint``.
  76. /// - Parameter addresses: A list of equivalent addresses.
  77. public init(addresses: [SocketAddress]) {
  78. self.addresses = addresses
  79. }
  80. }
  81. /// A resolver capable of resolving targets of type ``Target``.
  82. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  83. public protocol NameResolverFactory<Target> {
  84. /// The type of ``ResolvableTarget`` this factory makes resolvers for.
  85. associatedtype Target: ResolvableTarget
  86. /// Creates a resolver for the given target.
  87. ///
  88. /// - Parameter target: The target to make a resolver for.
  89. /// - Returns: The name resolver for the target.
  90. func resolver(for target: Target) -> NameResolver
  91. }
  92. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  93. extension NameResolverFactory {
  94. /// Returns whether the given target is compatible with this factory.
  95. ///
  96. /// - Parameter target: The target to check the compatibility of.
  97. /// - Returns: Whether the target is compatible with this factory.
  98. func isCompatible<Other: ResolvableTarget>(withTarget target: Other) -> Bool {
  99. return target is Target
  100. }
  101. /// Returns a name resolver if the given target is compatible.
  102. ///
  103. /// - Parameter target: The target to make a name resolver for.
  104. /// - Returns: A name resolver or `nil` if the target isn't compatible.
  105. func makeResolverIfCompatible<Other: ResolvableTarget>(_ target: Other) -> NameResolver? {
  106. guard let target = target as? Target else { return nil }
  107. return self.resolver(for: target)
  108. }
  109. }
  110. /// A target which can be resolved to a ``SocketAddress``.
  111. public protocol ResolvableTarget {}
  112. /// A namespace for resolvable targets.
  113. public enum ResolvableTargets {}
  114. /// A namespace for name resolver factories.
  115. public enum NameResolvers {}