GRPCClientStreamHandler.swift 9.1 KB

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