PlatformSupport.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2019, 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 Logging
  17. import NIOCore
  18. import NIOPosix
  19. import NIOSSL
  20. import NIOTransportServices
  21. /// How a network implementation should be chosen.
  22. public struct NetworkPreference: Hashable {
  23. private enum Wrapped: Hashable {
  24. case best
  25. case userDefined(NetworkImplementation)
  26. }
  27. private var wrapped: Wrapped
  28. private init(_ wrapped: Wrapped) {
  29. self.wrapped = wrapped
  30. }
  31. /// Use the best available, that is, Network.framework (and NIOTransportServices) when it is
  32. /// available on Darwin platforms (macOS 10.14+, iOS 12.0+, tvOS 12.0+, watchOS 6.0+), and
  33. /// falling back to the POSIX network model otherwise.
  34. public static let best = NetworkPreference(.best)
  35. /// Use the given implementation. Doing so may require additional availability checks depending
  36. /// on the implementation.
  37. public static func userDefined(_ implementation: NetworkImplementation) -> NetworkPreference {
  38. return NetworkPreference(.userDefined(implementation))
  39. }
  40. }
  41. /// The network implementation to use: POSIX sockets or Network.framework. This also determines
  42. /// which variant of NIO to use; NIO or NIOTransportServices, respectively.
  43. public struct NetworkImplementation: Hashable {
  44. fileprivate enum Wrapped: Hashable {
  45. case networkFramework
  46. case posix
  47. }
  48. fileprivate var wrapped: Wrapped
  49. private init(_ wrapped: Wrapped) {
  50. self.wrapped = wrapped
  51. }
  52. #if canImport(Network)
  53. /// Network.framework (NIOTransportServices).
  54. @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  55. public static let networkFramework = NetworkImplementation(.networkFramework)
  56. #endif
  57. /// POSIX (NIO).
  58. public static let posix = NetworkImplementation(.posix)
  59. internal static func matchingEventLoopGroup(_ group: EventLoopGroup) -> NetworkImplementation {
  60. #if canImport(Network)
  61. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  62. if PlatformSupport.isTransportServicesEventLoopGroup(group) {
  63. return .networkFramework
  64. }
  65. }
  66. #endif
  67. return .posix
  68. }
  69. }
  70. extension NetworkPreference {
  71. /// The network implementation, and by extension the NIO variant which will be used.
  72. ///
  73. /// Network.framework is available on macOS 10.14+, iOS 12.0+, tvOS 12.0+ and watchOS 6.0+.
  74. ///
  75. /// This isn't directly useful when implementing code which branches on the network preference
  76. /// since that code will still need the appropriate availability check:
  77. ///
  78. /// - `@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)`, or
  79. /// - `#available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)`.
  80. public var implementation: NetworkImplementation {
  81. switch self.wrapped {
  82. case .best:
  83. #if canImport(Network)
  84. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  85. return .networkFramework
  86. } else {
  87. // Older platforms must use the POSIX loop.
  88. return .posix
  89. }
  90. #else
  91. return .posix
  92. #endif
  93. case let .userDefined(implementation):
  94. return implementation
  95. }
  96. }
  97. }
  98. // MARK: - Generic Bootstraps
  99. // TODO: Revisit the handling of NIO/NIOTS once https://github.com/apple/swift-nio/issues/796
  100. // is addressed.
  101. /// This protocol is intended as a layer of abstraction over `ClientBootstrap` and
  102. /// `NIOTSConnectionBootstrap`.
  103. public protocol ClientBootstrapProtocol {
  104. func connect(to: SocketAddress) -> EventLoopFuture<Channel>
  105. func connect(host: String, port: Int) -> EventLoopFuture<Channel>
  106. func connect(unixDomainSocketPath: String) -> EventLoopFuture<Channel>
  107. func connectTimeout(_ timeout: TimeAmount) -> Self
  108. func channelOption<T>(_ option: T, value: T.Value) -> Self where T: ChannelOption
  109. func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
  110. }
  111. extension ClientBootstrap: ClientBootstrapProtocol {}
  112. #if canImport(Network)
  113. @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  114. extension NIOTSConnectionBootstrap: ClientBootstrapProtocol {}
  115. #endif
  116. /// This protocol is intended as a layer of abstraction over `ServerBootstrap` and
  117. /// `NIOTSListenerBootstrap`.
  118. public protocol ServerBootstrapProtocol {
  119. func bind(to: SocketAddress) -> EventLoopFuture<Channel>
  120. func bind(host: String, port: Int) -> EventLoopFuture<Channel>
  121. func bind(unixDomainSocketPath: String) -> EventLoopFuture<Channel>
  122. func serverChannelInitializer(_ initializer: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
  123. func serverChannelOption<T>(_ option: T, value: T.Value) -> Self where T: ChannelOption
  124. func childChannelInitializer(_ initializer: @escaping (Channel) -> EventLoopFuture<Void>) -> Self
  125. func childChannelOption<T>(_ option: T, value: T.Value) -> Self where T: ChannelOption
  126. }
  127. extension ServerBootstrap: ServerBootstrapProtocol {}
  128. #if canImport(Network)
  129. @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
  130. extension NIOTSListenerBootstrap: ServerBootstrapProtocol {}
  131. #endif
  132. // MARK: - Bootstrap / EventLoopGroup helpers
  133. public enum PlatformSupport {
  134. /// Makes a new event loop group based on the network preference.
  135. ///
  136. /// If `.best` is chosen and `Network.framework` is available then `NIOTSEventLoopGroup` will
  137. /// be returned. A `MultiThreadedEventLoopGroup` will be returned otherwise.
  138. ///
  139. /// - Parameter loopCount: The number of event loops to create in the event loop group.
  140. /// - Parameter networkPreference: Network preference; defaulting to `.best`.
  141. public static func makeEventLoopGroup(
  142. loopCount: Int,
  143. networkPreference: NetworkPreference = .best,
  144. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  145. ) -> EventLoopGroup {
  146. logger.debug("making EventLoopGroup for \(networkPreference) network preference")
  147. switch networkPreference.implementation.wrapped {
  148. case .networkFramework:
  149. #if canImport(Network)
  150. guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else {
  151. logger.critical("Network.framework can be imported but is not supported on this platform")
  152. // This is gated by the availability of `.networkFramework` so should never happen.
  153. fatalError(".networkFramework is being used on an unsupported platform")
  154. }
  155. logger.debug("created NIOTSEventLoopGroup for \(networkPreference) preference")
  156. return NIOTSEventLoopGroup(loopCount: loopCount)
  157. #else
  158. fatalError(".networkFramework is being used on an unsupported platform")
  159. #endif
  160. case .posix:
  161. logger.debug("created MultiThreadedEventLoopGroup for \(networkPreference) preference")
  162. return MultiThreadedEventLoopGroup(numberOfThreads: loopCount)
  163. }
  164. }
  165. /// Makes a new client bootstrap using the given `EventLoopGroup`.
  166. ///
  167. /// If the `EventLoopGroup` is a `NIOTSEventLoopGroup` then the returned bootstrap will be a
  168. /// `NIOTSConnectionBootstrap`, otherwise it will be a `ClientBootstrap`.
  169. ///
  170. /// - Parameter group: The `EventLoopGroup` to use.
  171. public static func makeClientBootstrap(
  172. group: EventLoopGroup,
  173. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  174. ) -> ClientBootstrapProtocol {
  175. logger.debug("making client bootstrap with event loop group of type \(type(of: group))")
  176. #if canImport(Network)
  177. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  178. if isTransportServicesEventLoopGroup(group) {
  179. logger.debug(
  180. "Network.framework is available and the EventLoopGroup is compatible with NIOTS, creating a NIOTSConnectionBootstrap"
  181. )
  182. return NIOTSConnectionBootstrap(group: group)
  183. } else {
  184. logger.debug(
  185. "Network.framework is available but the EventLoopGroup is not compatible with NIOTS, falling back to ClientBootstrap"
  186. )
  187. }
  188. }
  189. #endif
  190. logger.debug("creating a ClientBootstrap")
  191. return ClientBootstrap(group: group)
  192. }
  193. internal static func isTransportServicesEventLoopGroup(_ group: EventLoopGroup) -> Bool {
  194. #if canImport(Network)
  195. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  196. return group is NIOTSEventLoopGroup || group is QoSEventLoop
  197. }
  198. #endif
  199. return false
  200. }
  201. internal static func makeClientBootstrap(
  202. group: EventLoopGroup,
  203. tlsConfiguration: GRPCTLSConfiguration?,
  204. logger: Logger
  205. ) -> ClientBootstrapProtocol {
  206. let bootstrap = self.makeClientBootstrap(group: group, logger: logger)
  207. guard let tlsConfigruation = tlsConfiguration else {
  208. return bootstrap
  209. }
  210. #if canImport(Network)
  211. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *),
  212. let transportServicesBootstrap = bootstrap as? NIOTSConnectionBootstrap {
  213. return transportServicesBootstrap.tlsOptions(from: tlsConfigruation)
  214. }
  215. #endif
  216. return bootstrap
  217. }
  218. /// Makes a new server bootstrap using the given `EventLoopGroup`.
  219. ///
  220. /// If the `EventLoopGroup` is a `NIOTSEventLoopGroup` then the returned bootstrap will be a
  221. /// `NIOTSListenerBootstrap`, otherwise it will be a `ServerBootstrap`.
  222. ///
  223. /// - Parameter group: The `EventLoopGroup` to use.
  224. public static func makeServerBootstrap(
  225. group: EventLoopGroup,
  226. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  227. ) -> ServerBootstrapProtocol {
  228. logger.debug("making server bootstrap with event loop group of type \(type(of: group))")
  229. #if canImport(Network)
  230. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  231. if let tsGroup = group as? NIOTSEventLoopGroup {
  232. logger
  233. .debug(
  234. "Network.framework is available and the group is correctly typed, creating a NIOTSListenerBootstrap"
  235. )
  236. return NIOTSListenerBootstrap(group: tsGroup)
  237. } else if let qosEventLoop = group as? QoSEventLoop {
  238. logger
  239. .debug(
  240. "Network.framework is available and the group is correctly typed, creating a NIOTSListenerBootstrap"
  241. )
  242. return NIOTSListenerBootstrap(group: qosEventLoop)
  243. }
  244. logger
  245. .debug(
  246. "Network.framework is available but the group is not typed for NIOTS, falling back to ServerBootstrap"
  247. )
  248. }
  249. #endif
  250. logger.debug("creating a ServerBootstrap")
  251. return ServerBootstrap(group: group)
  252. }
  253. /// Determines whether we may need to work around an issue in Network.framework with zero-length writes.
  254. ///
  255. /// See https://github.com/apple/swift-nio-transport-services/pull/72 for more.
  256. static func requiresZeroLengthWriteWorkaround(group: EventLoopGroup, hasTLS: Bool) -> Bool {
  257. #if canImport(Network)
  258. if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) {
  259. if group is NIOTSEventLoopGroup || group is QoSEventLoop {
  260. // We need the zero-length write workaround on NIOTS when not using TLS.
  261. return !hasTLS
  262. } else {
  263. return false
  264. }
  265. } else {
  266. return false
  267. }
  268. #else
  269. return false
  270. #endif
  271. }
  272. }
  273. extension GRPCTLSConfiguration {
  274. /// Provides a `GRPCTLSConfiguration` suitable for the given network preference.
  275. public static func makeClientDefault(
  276. for networkPreference: NetworkPreference
  277. ) -> GRPCTLSConfiguration {
  278. switch networkPreference.implementation.wrapped {
  279. case .networkFramework:
  280. #if canImport(Network)
  281. guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else {
  282. // This is gated by the availability of `.networkFramework` so should never happen.
  283. fatalError(".networkFramework is being used on an unsupported platform")
  284. }
  285. return .makeClientConfigurationBackedByNetworkFramework()
  286. #else
  287. fatalError(".networkFramework is being used on an unsupported platform")
  288. #endif
  289. case .posix:
  290. return .makeClientConfigurationBackedByNIOSSL()
  291. }
  292. }
  293. }
  294. extension EventLoopGroup {
  295. internal func isCompatible(with tlsConfiguration: GRPCTLSConfiguration) -> Bool {
  296. let isTransportServicesGroup = PlatformSupport.isTransportServicesEventLoopGroup(self)
  297. let isNetworkFrameworkTLSBackend = tlsConfiguration.isNetworkFrameworkTLSBackend
  298. // If the group is from NIOTransportServices then we can use either the NIOSSL or the
  299. // Network.framework TLS backend.
  300. //
  301. // If it isn't then we must not use the Network.Framework TLS backend.
  302. return isTransportServicesGroup || !isNetworkFrameworkTLSBackend
  303. }
  304. internal func preconditionCompatible(
  305. with tlsConfiguration: GRPCTLSConfiguration,
  306. file: StaticString = #file,
  307. line: UInt = #line
  308. ) {
  309. precondition(
  310. self.isCompatible(with: tlsConfiguration),
  311. "Unsupported 'EventLoopGroup' and 'GRPCLSConfiguration' pairing (Network.framework backed TLS configurations MUST use an EventLoopGroup from NIOTransportServices)",
  312. file: file,
  313. line: line
  314. )
  315. }
  316. }