NameResolver+UDS.swift 2.3 KB

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