Server.swift 12 KB

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