2
0

NameResolver+VSOCK.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. extension ResolvableTargets {
  18. /// A resolvable target for Virtual Socket addresses.
  19. ///
  20. /// ``VirtualSocket`` addresses can be resolved by the ``NameResolvers/VirtualSocket``
  21. /// resolver which creates a single ``Endpoint`` for target address.
  22. public struct VirtualSocket: ResolvableTarget {
  23. public var address: SocketAddress.VirtualSocket
  24. public init(address: SocketAddress.VirtualSocket) {
  25. self.address = address
  26. }
  27. }
  28. }
  29. extension ResolvableTarget where Self == ResolvableTargets.VirtualSocket {
  30. /// Creates a new resolvable Virtual Socket target.
  31. /// - Parameters:
  32. /// - contextID: The context ID ('cid') of the service.
  33. /// - port: The port to connect to.
  34. public static func vsock(
  35. contextID: SocketAddress.VirtualSocket.ContextID,
  36. port: SocketAddress.VirtualSocket.Port
  37. ) -> Self {
  38. let address = SocketAddress.VirtualSocket(contextID: contextID, port: port)
  39. return ResolvableTargets.VirtualSocket(address: address)
  40. }
  41. }
  42. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  43. extension NameResolvers {
  44. /// A ``NameResolverFactory`` for ``ResolvableTargets/VirtualSocket`` targets.
  45. ///
  46. /// The name resolver for a given target always produces the same values, with a single endpoint.
  47. /// This resolver doesn't support fetching service configuration.
  48. public struct VirtualSocket: NameResolverFactory {
  49. public typealias Target = ResolvableTargets.VirtualSocket
  50. public init() {}
  51. public func resolver(for target: Target) -> NameResolver {
  52. let endpoint = Endpoint(addresses: [.vsock(target.address)])
  53. let resolutionResult = NameResolutionResult(endpoints: [endpoint], serviceConfig: nil)
  54. return NameResolver(names: .constant(resolutionResult), updateMode: .pull)
  55. }
  56. }
  57. }