GRPCClientStreamHandler.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2024, 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. internal import GRPCCore
  17. internal import NIOCore
  18. internal import NIOHTTP2
  19. final class GRPCClientStreamHandler: ChannelDuplexHandler {
  20. typealias InboundIn = HTTP2Frame.FramePayload
  21. typealias InboundOut = RPCResponsePart
  22. typealias OutboundIn = RPCRequestPart
  23. typealias OutboundOut = HTTP2Frame.FramePayload
  24. private var stateMachine: GRPCStreamStateMachine
  25. private var isReading = false
  26. private var flushPending = false
  27. private var requestFinished = false
  28. init(
  29. methodDescriptor: MethodDescriptor,
  30. scheme: Scheme,
  31. authority: String?,
  32. outboundEncoding: CompressionAlgorithm,
  33. acceptedEncodings: CompressionAlgorithmSet,
  34. maxPayloadSize: Int,
  35. skipStateMachineAssertions: Bool = false
  36. ) {
  37. self.stateMachine = .init(
  38. configuration: .client(
  39. .init(
  40. methodDescriptor: methodDescriptor,
  41. scheme: scheme,
  42. authority: authority,
  43. outboundEncoding: outboundEncoding,
  44. acceptedEncodings: acceptedEncodings
  45. )
  46. ),
  47. maxPayloadSize: maxPayloadSize,
  48. skipAssertions: skipStateMachineAssertions
  49. )
  50. }
  51. }
  52. // - MARK: ChannelInboundHandler
  53. extension GRPCClientStreamHandler {
  54. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  55. self.isReading = true
  56. let frame = self.unwrapInboundIn(data)
  57. switch frame {
  58. case .data(let frameData):
  59. let endStream = frameData.endStream
  60. switch frameData.data {
  61. case .byteBuffer(let buffer):
  62. do {
  63. switch try self.stateMachine.receive(buffer: buffer, endStream: endStream) {
  64. case .endRPCAndForwardErrorStatus_clientOnly(let status):
  65. context.fireChannelRead(self.wrapInboundOut(.status(status, [:])))
  66. context.close(promise: nil)
  67. case .forwardErrorAndClose_serverOnly:
  68. assertionFailure("Unexpected client action")
  69. case .readInbound:
  70. loop: while true {
  71. switch self.stateMachine.nextInboundMessage() {
  72. case .receiveMessage(let message):
  73. context.fireChannelRead(self.wrapInboundOut(.message(message)))
  74. case .awaitMoreMessages:
  75. break loop
  76. case .noMoreMessages:
  77. // This could only happen if the server sends a data frame with EOS
  78. // set, without sending status and trailers.
  79. // If this happens, we should have forwarded an error status above
  80. // so we should never reach this point. Do nothing.
  81. break loop
  82. }
  83. }
  84. case .doNothing:
  85. ()
  86. }
  87. } catch let invalidState {
  88. let error = RPCError(invalidState)
  89. context.fireErrorCaught(error)
  90. }
  91. case .fileRegion:
  92. preconditionFailure("Unexpected IOData.fileRegion")
  93. }
  94. case .headers(let headers):
  95. do {
  96. let action = try self.stateMachine.receive(
  97. headers: headers.headers,
  98. endStream: headers.endStream
  99. )
  100. switch action {
  101. case .receivedMetadata(let metadata, _):
  102. context.fireChannelRead(self.wrapInboundOut(.metadata(metadata)))
  103. case .receivedStatusAndMetadata_clientOnly(let status, let metadata):
  104. context.fireChannelRead(self.wrapInboundOut(.status(status, metadata)))
  105. context.fireUserInboundEventTriggered(ChannelEvent.inputClosed)
  106. case .rejectRPC_serverOnly, .protocolViolation_serverOnly:
  107. assertionFailure("Unexpected action '\(action)'")
  108. case .doNothing:
  109. ()
  110. }
  111. } catch let invalidState {
  112. let error = RPCError(invalidState)
  113. context.fireErrorCaught(error)
  114. }
  115. case .rstStream:
  116. self.handleUnexpectedInboundClose(context: context, reason: .streamReset)
  117. case .ping, .goAway, .priority, .settings, .pushPromise, .windowUpdate,
  118. .alternativeService, .origin:
  119. ()
  120. }
  121. }
  122. func channelReadComplete(context: ChannelHandlerContext) {
  123. self.isReading = false
  124. if self.flushPending {
  125. self.flushPending = false
  126. self.flush(context: context)
  127. }
  128. context.fireChannelReadComplete()
  129. }
  130. func handlerRemoved(context: ChannelHandlerContext) {
  131. self.stateMachine.tearDown()
  132. }
  133. func channelInactive(context: ChannelHandlerContext) {
  134. self.handleUnexpectedInboundClose(context: context, reason: .channelInactive)
  135. context.fireChannelInactive()
  136. }
  137. func errorCaught(context: ChannelHandlerContext, error: any Error) {
  138. self.handleUnexpectedInboundClose(context: context, reason: .errorThrown(error))
  139. }
  140. private func handleUnexpectedInboundClose(
  141. context: ChannelHandlerContext,
  142. reason: GRPCStreamStateMachine.UnexpectedInboundCloseReason
  143. ) {
  144. switch self.stateMachine.unexpectedInboundClose(reason: reason) {
  145. case .forwardStatus_clientOnly(let status):
  146. context.fireChannelRead(self.wrapInboundOut(.status(status, [:])))
  147. case .doNothing:
  148. ()
  149. case .fireError_serverOnly:
  150. assertionFailure("`fireError` should only happen on the server side, never on the client.")
  151. }
  152. }
  153. }
  154. // - MARK: ChannelOutboundHandler
  155. extension GRPCClientStreamHandler {
  156. func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  157. switch self.unwrapOutboundIn(data) {
  158. case .metadata(let metadata):
  159. do {
  160. self.flushPending = true
  161. let headers = try self.stateMachine.send(metadata: metadata)
  162. context.write(self.wrapOutboundOut(.headers(.init(headers: headers))), promise: promise)
  163. } catch let invalidState {
  164. let error = RPCError(invalidState)
  165. promise?.fail(error)
  166. context.fireErrorCaught(error)
  167. }
  168. case .message(let message):
  169. do {
  170. try self.stateMachine.send(message: message, promise: promise)
  171. } catch let invalidState {
  172. let error = RPCError(invalidState)
  173. promise?.fail(error)
  174. context.fireErrorCaught(error)
  175. }
  176. }
  177. }
  178. func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) {
  179. switch mode {
  180. case .input:
  181. context.fireUserInboundEventTriggered(ChannelEvent.inputClosed)
  182. promise?.succeed()
  183. case .output:
  184. // We flush all pending messages and update the internal state machine's
  185. // state, but we don't close the outbound end of the channel, because
  186. // forwarding the close in this case would cause the HTTP2 stream handler
  187. // to close the whole channel (as the mode is ignored in its implementation).
  188. do {
  189. try self.stateMachine.closeOutbound()
  190. // Force a flush by calling _flush instead of flush
  191. // (otherwise, we'd skip flushing if we're in a read loop)
  192. self._flush(context: context)
  193. promise?.succeed()
  194. } catch let invalidState {
  195. let error = RPCError(invalidState)
  196. promise?.fail(error)
  197. context.fireErrorCaught(error)
  198. }
  199. case .all:
  200. // Since we're closing the whole channel here, we *do* forward the close
  201. // down the pipeline.
  202. do {
  203. try self.stateMachine.closeOutbound()
  204. // Force a flush by calling _flush
  205. // (otherwise, we'd skip flushing if we're in a read loop)
  206. self._flush(context: context)
  207. context.close(mode: mode, promise: promise)
  208. } catch let invalidState {
  209. let error = RPCError(invalidState)
  210. promise?.fail(error)
  211. context.fireErrorCaught(error)
  212. }
  213. }
  214. }
  215. func flush(context: ChannelHandlerContext) {
  216. if self.isReading {
  217. // We don't want to flush yet if we're still in a read loop.
  218. self.flushPending = true
  219. return
  220. }
  221. self._flush(context: context)
  222. }
  223. private func _flush(context: ChannelHandlerContext) {
  224. do {
  225. loop: while true {
  226. switch try self.stateMachine.nextOutboundFrame() {
  227. case .sendFrame(let byteBuffer, let promise):
  228. self.flushPending = true
  229. context.write(
  230. self.wrapOutboundOut(.data(.init(data: .byteBuffer(byteBuffer)))),
  231. promise: promise
  232. )
  233. case .noMoreMessages:
  234. // If we're done writing (i.e. we have no more messages returned from state
  235. // machine) then return.
  236. if self.requestFinished { return }
  237. self.requestFinished = true
  238. // Write an empty data frame with the EOS flag set, to signal the RPC
  239. // request is now finished.
  240. context.write(
  241. self.wrapOutboundOut(
  242. HTTP2Frame.FramePayload.data(
  243. .init(
  244. data: .byteBuffer(.init()),
  245. endStream: true
  246. )
  247. )
  248. ),
  249. promise: nil
  250. )
  251. context.flush()
  252. break loop
  253. case .awaitMoreMessages:
  254. if self.flushPending {
  255. self.flushPending = false
  256. context.flush()
  257. }
  258. break loop
  259. case .closeAndFailPromise(let promise, let error):
  260. context.close(mode: .all, promise: nil)
  261. promise?.fail(error)
  262. break loop
  263. }
  264. }
  265. } catch let invalidState {
  266. context.fireErrorCaught(RPCError(invalidState))
  267. }
  268. }
  269. }