Server.swift 11 KB

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