Server.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 Foundation
  17. import NIO
  18. import NIOHTTP1
  19. import NIOHTTP2
  20. import NIOSSL
  21. /// Wrapper object to manage the lifecycle of a gRPC server.
  22. ///
  23. /// The pipeline is configured in three stages detailed below. Note: handlers marked with
  24. /// a '*' are responsible for handling errors.
  25. ///
  26. /// 1. Initial stage, prior to HTTP protocol detection.
  27. ///
  28. /// ┌───────────────────────────┐
  29. /// │ HTTPProtocolSwitcher* │
  30. /// └─▲───────────────────────┬─┘
  31. /// ByteBuffer│ │ByteBuffer
  32. /// ┌─┴───────────────────────▼─┐
  33. /// │ NIOSSLHandler │
  34. /// └─▲───────────────────────┬─┘
  35. /// ByteBuffer│ │ByteBuffer
  36. /// │ ▼
  37. ///
  38. /// The NIOSSLHandler is optional and depends on how the framework user has configured
  39. /// their server. The HTTPProtocolSwitched detects which HTTP version is being used and
  40. /// configures the pipeline accordingly.
  41. ///
  42. /// 2. HTTP version detected. "HTTP Handlers" depends on the HTTP version determined by
  43. /// HTTPProtocolSwitcher. All of these handlers are provided by NIO except for the
  44. /// WebCORSHandler which is used for HTTP/1.
  45. ///
  46. /// ┌───────────────────────────┐
  47. /// │ GRPCChannelHandler* │
  48. /// └─▲───────────────────────┬─┘
  49. /// RawGRPCServerRequestPart│ │RawGRPCServerResponsePart
  50. /// ┌─┴───────────────────────▼─┐
  51. /// │ HTTP1ToRawGRPCServerCodec │
  52. /// └─▲───────────────────────┬─┘
  53. /// HTTPServerRequestPart│ │HTTPServerResponsePart
  54. /// ┌─┴───────────────────────▼─┐
  55. /// │ HTTP Handlers │
  56. /// └─▲───────────────────────┬─┘
  57. /// ByteBuffer│ │ByteBuffer
  58. /// ┌─┴───────────────────────▼─┐
  59. /// │ NIOSSLHandler │
  60. /// └─▲───────────────────────┬─┘
  61. /// ByteBuffer│ │ByteBuffer
  62. /// │ ▼
  63. ///
  64. /// The GPRCChannelHandler resolves the request head and configures the rest of the pipeline
  65. /// based on the RPC call being made.
  66. ///
  67. /// 3. The call has been resolved and is a function that this server can handle. Responses are
  68. /// written into `BaseCallHandler` by a user-implemented `CallHandlerProvider`.
  69. ///
  70. /// ┌───────────────────────────┐
  71. /// │ BaseCallHandler* │
  72. /// └─▲───────────────────────┬─┘
  73. /// GRPCServerRequestPart<T1>│ │GRPCServerResponsePart<T2>
  74. /// ┌─┴───────────────────────▼─┐
  75. /// │ GRPCServerCodec │
  76. /// └─▲───────────────────────┬─┘
  77. /// RawGRPCServerRequestPart│ │RawGRPCServerResponsePart
  78. /// ┌─┴───────────────────────▼─┐
  79. /// │ HTTP1ToRawGRPCServerCodec │
  80. /// └─▲───────────────────────┬─┘
  81. /// HTTPServerRequestPart│ │HTTPServerResponsePart
  82. /// ┌─┴───────────────────────▼─┐
  83. /// │ HTTP Handlers │
  84. /// └─▲───────────────────────┬─┘
  85. /// ByteBuffer│ │ByteBuffer
  86. /// ┌─┴───────────────────────▼─┐
  87. /// │ NIOSSLHandler │
  88. /// └─▲───────────────────────┬─┘
  89. /// ByteBuffer│ │ByteBuffer
  90. /// │ ▼
  91. ///
  92. public final class Server {
  93. /// Makes and configures a `ServerBootstrap` using the provided configuration.
  94. public class func makeBootstrap(configuration: Configuration) -> ServerBootstrapProtocol {
  95. let bootstrap = PlatformSupport.makeServerBootstrap(group: configuration.eventLoopGroup)
  96. // Backlog is only available on `ServerBootstrap`.
  97. if bootstrap is ServerBootstrap {
  98. // Specify a backlog to avoid overloading the server.
  99. _ = bootstrap.serverChannelOption(ChannelOptions.backlog, value: 256)
  100. }
  101. return bootstrap
  102. // Enable `SO_REUSEADDR` to avoid "address already in use" error.
  103. .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
  104. // Set the handlers that are applied to the accepted Channels
  105. .childChannelInitializer { channel in
  106. let protocolSwitcher = HTTPProtocolSwitcher(errorDelegate: configuration.errorDelegate) { channel -> EventLoopFuture<Void> in
  107. let handlers: [ChannelHandler] = [
  108. HTTP1ToRawGRPCServerCodec(),
  109. GRPCChannelHandler(
  110. servicesByName: configuration.serviceProvidersByName,
  111. errorDelegate: configuration.errorDelegate
  112. )
  113. ]
  114. return channel.pipeline.addHandlers(handlers)
  115. }
  116. if let tls = configuration.tls {
  117. return channel.configureTLS(configuration: tls).flatMap {
  118. channel.pipeline.addHandler(protocolSwitcher)
  119. }
  120. } else {
  121. return channel.pipeline.addHandler(protocolSwitcher)
  122. }
  123. }
  124. // Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels
  125. .childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
  126. .childChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
  127. }
  128. /// Starts a server with the given configuration. See `Server.Configuration` for the options
  129. /// available to configure the server.
  130. public static func start(configuration: Configuration) -> EventLoopFuture<Server> {
  131. return makeBootstrap(configuration: configuration)
  132. .bind(to: configuration.target)
  133. .map { channel in
  134. Server(channel: channel, errorDelegate: configuration.errorDelegate)
  135. }
  136. }
  137. public let channel: Channel
  138. private var errorDelegate: ServerErrorDelegate?
  139. private init(channel: Channel, errorDelegate: ServerErrorDelegate?) {
  140. self.channel = channel
  141. // Maintain a strong reference to ensure it lives as long as the server.
  142. self.errorDelegate = errorDelegate
  143. // nil out errorDelegate to avoid retain cycles.
  144. onClose.whenComplete { _ in
  145. self.errorDelegate = nil
  146. }
  147. }
  148. /// Fired when the server shuts down.
  149. public var onClose: EventLoopFuture<Void> {
  150. return channel.closeFuture
  151. }
  152. /// Shut down the server; this should be called to avoid leaking resources.
  153. public func close() -> EventLoopFuture<Void> {
  154. return channel.close(mode: .all)
  155. }
  156. }
  157. public typealias BindTarget = ConnectionTarget
  158. extension Server {
  159. /// The configuration for a server.
  160. public struct Configuration {
  161. /// The target to bind to.
  162. public var target: BindTarget
  163. /// The event loop group to run the connection on.
  164. public var eventLoopGroup: EventLoopGroup
  165. /// Providers the server should use to handle gRPC requests.
  166. public var serviceProviders: [CallHandlerProvider]
  167. /// An error delegate which is called when errors are caught. Provided delegates **must not
  168. /// maintain a strong reference to this `Server`**. Doing so will cause a retain cycle.
  169. public var errorDelegate: ServerErrorDelegate?
  170. /// TLS configuration for this connection. `nil` if TLS is not desired.
  171. public var tls: TLS?
  172. /// Create a `Configuration` with some pre-defined defaults.
  173. ///
  174. /// - Parameter target: The target to bind to.
  175. /// - Parameter eventLoopGroup: The event loop group to run the server on.
  176. /// - Parameter serviceProviders: An array of `CallHandlerProvider`s which the server should use
  177. /// to handle requests.
  178. /// - Parameter errorDelegate: The error delegate, defaulting to a logging delegate.
  179. /// - Parameter tlsConfiguration: TLS configuration, defaulting to `nil`.
  180. public init(
  181. target: BindTarget,
  182. eventLoopGroup: EventLoopGroup,
  183. serviceProviders: [CallHandlerProvider],
  184. errorDelegate: ServerErrorDelegate? = LoggingServerErrorDelegate.shared,
  185. tls: TLS? = nil
  186. ) {
  187. self.target = target
  188. self.eventLoopGroup = eventLoopGroup
  189. self.serviceProviders = serviceProviders
  190. self.errorDelegate = errorDelegate
  191. self.tls = tls
  192. }
  193. }
  194. /// The TLS configuration for a connection.
  195. public struct TLSConfiguration {
  196. /// The SSL context to use.
  197. public var sslContext: NIOSSLContext
  198. public init(sslContext: NIOSSLContext) {
  199. self.sslContext = sslContext
  200. }
  201. }
  202. }
  203. fileprivate extension Server.Configuration {
  204. var serviceProvidersByName: [String: CallHandlerProvider] {
  205. return Dictionary(uniqueKeysWithValues: self.serviceProviders.map { ($0.serviceName, $0) })
  206. }
  207. }
  208. fileprivate extension Channel {
  209. /// Configure an SSL handler on the channel.
  210. ///
  211. /// - Parameters:
  212. /// - configuration: The configuration to use when creating the handler.
  213. /// - Returns: A future which will be succeeded when the pipeline has been configured.
  214. func configureTLS(configuration: Server.Configuration.TLS) -> EventLoopFuture<Void> {
  215. do {
  216. let context = try NIOSSLContext(configuration: configuration.configuration)
  217. return self.pipeline.addHandler(try NIOSSLServerHandler(context: context))
  218. } catch {
  219. return self.pipeline.eventLoop.makeFailedFuture(error)
  220. }
  221. }
  222. }
  223. fileprivate extension ServerBootstrapProtocol {
  224. func bind(to target: BindTarget) -> EventLoopFuture<Channel> {
  225. switch target {
  226. case .hostAndPort(let host, let port):
  227. return self.bind(host: host, port: port)
  228. case .unixDomainSocket(let path):
  229. return self.bind(unixDomainSocketPath: path)
  230. case .socketAddress(let address):
  231. return self.bind(to: address)
  232. }
  233. }
  234. }