UnaryServerHandler.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright 2021, 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 NIOCore
  17. import NIOHPACK
  18. public final class UnaryServerHandler<
  19. Serializer: MessageSerializer,
  20. Deserializer: MessageDeserializer
  21. >: GRPCServerHandlerProtocol {
  22. public typealias Request = Deserializer.Output
  23. public typealias Response = Serializer.Input
  24. /// A response serializer.
  25. @usableFromInline
  26. internal let serializer: Serializer
  27. /// A request deserializer.
  28. @usableFromInline
  29. internal let deserializer: Deserializer
  30. /// A pipeline of user provided interceptors.
  31. @usableFromInline
  32. internal var interceptors: ServerInterceptorPipeline<Request, Response>!
  33. /// The context required in order create the function.
  34. @usableFromInline
  35. internal let context: CallHandlerContext
  36. /// A reference to a `UserInfo`.
  37. @usableFromInline
  38. internal let userInfoRef: Ref<UserInfo>
  39. /// The user provided function to execute.
  40. @usableFromInline
  41. internal let userFunction: (Request, StatusOnlyCallContext) -> EventLoopFuture<Response>
  42. /// The state of the function invocation.
  43. @usableFromInline
  44. internal var state: State = .idle
  45. @usableFromInline
  46. internal enum State {
  47. // Initial state. Nothing has happened yet.
  48. case idle
  49. // Headers have been received and now we're holding a context with which to invoke the user
  50. // function when we receive a message.
  51. case createdContext(UnaryResponseCallContext<Response>)
  52. // The user function has been invoked, we're waiting for the response.
  53. case invokedFunction(UnaryResponseCallContext<Response>)
  54. // The function has completed or we are no longer proceeding with execution (because of an error
  55. // or unexpected closure).
  56. case completed
  57. }
  58. @inlinable
  59. public init(
  60. context: CallHandlerContext,
  61. requestDeserializer: Deserializer,
  62. responseSerializer: Serializer,
  63. interceptors: [ServerInterceptor<Request, Response>],
  64. userFunction: @escaping (Request, StatusOnlyCallContext) -> EventLoopFuture<Response>
  65. ) {
  66. self.userFunction = userFunction
  67. self.serializer = responseSerializer
  68. self.deserializer = requestDeserializer
  69. self.context = context
  70. let userInfoRef = Ref(UserInfo())
  71. self.userInfoRef = userInfoRef
  72. self.interceptors = ServerInterceptorPipeline(
  73. logger: context.logger,
  74. eventLoop: context.eventLoop,
  75. path: context.path,
  76. callType: .unary,
  77. remoteAddress: context.remoteAddress,
  78. userInfoRef: userInfoRef,
  79. closeFuture: context.closeFuture,
  80. interceptors: interceptors,
  81. onRequestPart: self.receiveInterceptedPart(_:),
  82. onResponsePart: self.sendInterceptedPart(_:promise:)
  83. )
  84. }
  85. // MARK: - Public API: gRPC to Interceptors
  86. @inlinable
  87. public func receiveMetadata(_ metadata: HPACKHeaders) {
  88. self.interceptors.receive(.metadata(metadata))
  89. }
  90. @inlinable
  91. public func receiveMessage(_ bytes: ByteBuffer) {
  92. do {
  93. let message = try self.deserializer.deserialize(byteBuffer: bytes)
  94. self.interceptors.receive(.message(message))
  95. } catch {
  96. self.handleError(error)
  97. }
  98. }
  99. @inlinable
  100. public func receiveEnd() {
  101. self.interceptors.receive(.end)
  102. }
  103. @inlinable
  104. public func receiveError(_ error: Error) {
  105. self.handleError(error)
  106. self.finish()
  107. }
  108. @inlinable
  109. public func finish() {
  110. switch self.state {
  111. case .idle:
  112. self.interceptors = nil
  113. self.state = .completed
  114. case let .createdContext(context),
  115. let .invokedFunction(context):
  116. context.responsePromise.fail(GRPCStatus(code: .unavailable, message: nil))
  117. self.context.eventLoop.execute {
  118. self.interceptors = nil
  119. }
  120. case .completed:
  121. self.interceptors = nil
  122. }
  123. }
  124. // MARK: - Interceptors to User Function
  125. @inlinable
  126. internal func receiveInterceptedPart(_ part: GRPCServerRequestPart<Request>) {
  127. switch part {
  128. case let .metadata(headers):
  129. self.receiveInterceptedMetadata(headers)
  130. case let .message(message):
  131. self.receiveInterceptedMessage(message)
  132. case .end:
  133. self.receiveInterceptedEnd()
  134. }
  135. }
  136. @inlinable
  137. internal func receiveInterceptedMetadata(_ headers: HPACKHeaders) {
  138. switch self.state {
  139. case .idle:
  140. // Make a context to invoke the user function with.
  141. let context = UnaryResponseCallContext<Response>(
  142. eventLoop: self.context.eventLoop,
  143. headers: headers,
  144. logger: self.context.logger,
  145. userInfoRef: self.userInfoRef,
  146. closeFuture: self.context.closeFuture
  147. )
  148. // Move to the next state.
  149. self.state = .createdContext(context)
  150. // Register a callback on the response future. The user function will complete this promise.
  151. context.responsePromise.futureResult.whenComplete(self.userFunctionCompletedWithResult(_:))
  152. // Send back response headers.
  153. self.interceptors.send(.metadata([:]), promise: nil)
  154. case .createdContext, .invokedFunction:
  155. self.handleError(GRPCError.ProtocolViolation("Multiple header blocks received on RPC"))
  156. case .completed:
  157. // We may receive headers from the interceptor pipeline if we have already finished (i.e. due
  158. // to an error or otherwise) and an interceptor doing some async work later emitting headers.
  159. // Dropping them is fine.
  160. ()
  161. }
  162. }
  163. @inlinable
  164. internal func receiveInterceptedMessage(_ request: Request) {
  165. switch self.state {
  166. case .idle:
  167. self.handleError(GRPCError.ProtocolViolation("Message received before headers"))
  168. case let .createdContext(context):
  169. // Happy path: execute the function; complete the promise with the result.
  170. self.state = .invokedFunction(context)
  171. context.responsePromise.completeWith(self.userFunction(request, context))
  172. case .invokedFunction:
  173. // The function's already been invoked with a message.
  174. self.handleError(GRPCError.ProtocolViolation("Multiple messages received on unary RPC"))
  175. case .completed:
  176. // We received a message but we're already done: this may happen if we terminate the RPC
  177. // due to a channel error, for example.
  178. ()
  179. }
  180. }
  181. @inlinable
  182. internal func receiveInterceptedEnd() {
  183. switch self.state {
  184. case .idle:
  185. self.handleError(GRPCError.ProtocolViolation("End received before headers"))
  186. case .createdContext:
  187. self.handleError(GRPCError.ProtocolViolation("End received before message"))
  188. case .invokedFunction, .completed:
  189. ()
  190. }
  191. }
  192. // MARK: - User Function To Interceptors
  193. @inlinable
  194. internal func userFunctionCompletedWithResult(_ result: Result<Response, Error>) {
  195. switch self.state {
  196. case .idle:
  197. // Invalid state: the user function can only complete if it was executed.
  198. preconditionFailure()
  199. // 'created' is allowed here: we may have to (and tear down) after receiving headers
  200. // but before receiving a message.
  201. case let .createdContext(context),
  202. let .invokedFunction(context):
  203. switch result {
  204. case let .success(response):
  205. // Complete, as we're sending 'end'.
  206. self.state = .completed
  207. // Compression depends on whether it's enabled on the server and the setting in the caller
  208. // context.
  209. let compress = self.context.encoding.isEnabled && context.compressionEnabled
  210. let metadata = MessageMetadata(compress: compress, flush: false)
  211. self.interceptors.send(.message(response, metadata), promise: nil)
  212. self.interceptors.send(.end(context.responseStatus, context.trailers), promise: nil)
  213. case let .failure(error):
  214. self.handleError(error, thrownFromHandler: true)
  215. }
  216. case .completed:
  217. // We've already failed. Ignore this.
  218. ()
  219. }
  220. }
  221. @inlinable
  222. internal func sendInterceptedPart(
  223. _ part: GRPCServerResponsePart<Response>,
  224. promise: EventLoopPromise<Void>?
  225. ) {
  226. switch part {
  227. case let .metadata(headers):
  228. // We can delay this flush until the end of the RPC.
  229. self.context.responseWriter.sendMetadata(headers, flush: false, promise: promise)
  230. case let .message(message, metadata):
  231. do {
  232. let bytes = try self.serializer.serialize(message, allocator: self.context.allocator)
  233. self.context.responseWriter.sendMessage(bytes, metadata: metadata, promise: promise)
  234. } catch {
  235. // Serialization failed: fail the promise and send end.
  236. promise?.fail(error)
  237. let (status, trailers) = ServerErrorProcessor.processLibraryError(
  238. error,
  239. delegate: self.context.errorDelegate
  240. )
  241. // Loop back via the interceptors.
  242. self.interceptors.send(.end(status, trailers), promise: nil)
  243. }
  244. case let .end(status, trailers):
  245. self.context.responseWriter.sendEnd(status: status, trailers: trailers, promise: promise)
  246. }
  247. }
  248. @inlinable
  249. internal func handleError(_ error: Error, thrownFromHandler isHandlerError: Bool = false) {
  250. switch self.state {
  251. case .idle:
  252. assert(!isHandlerError)
  253. self.state = .completed
  254. // We don't have a promise to fail. Just send back end.
  255. let (status, trailers) = ServerErrorProcessor.processLibraryError(
  256. error,
  257. delegate: self.context.errorDelegate
  258. )
  259. self.interceptors.send(.end(status, trailers), promise: nil)
  260. case let .createdContext(context),
  261. let .invokedFunction(context):
  262. // We don't have a promise to fail. Just send back end.
  263. self.state = .completed
  264. let status: GRPCStatus
  265. let trailers: HPACKHeaders
  266. if isHandlerError {
  267. (status, trailers) = ServerErrorProcessor.processObserverError(
  268. error,
  269. headers: context.headers,
  270. trailers: context.trailers,
  271. delegate: self.context.errorDelegate
  272. )
  273. } else {
  274. (status, trailers) = ServerErrorProcessor.processLibraryError(
  275. error,
  276. delegate: self.context.errorDelegate
  277. )
  278. }
  279. self.interceptors.send(.end(status, trailers), promise: nil)
  280. // We're already in the 'completed' state so failing the promise will be a no-op in the
  281. // callback to 'userFunctionCompletedWithResult' (but we also need to avoid leaking the
  282. // promise.)
  283. context.responsePromise.fail(error)
  284. case .completed:
  285. ()
  286. }
  287. }
  288. }