StreamingResponseCallContext.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import Foundation
  2. import SwiftProtobuf
  3. import NIO
  4. import NIOHTTP1
  5. /// Abstract base class exposing a method to send multiple messages over the wire and a promise for the final RPC status.
  6. ///
  7. /// - When `statusPromise` is fulfilled, the call is closed and the provided status transmitted.
  8. /// - If `statusPromise` is failed and the error is of type `GRPCStatusTransformable`,
  9. /// the result of `error.asGRPCStatus()` will be returned to the client.
  10. /// - If `error.asGRPCStatus()` is not available, `GRPCStatus.processingError` is returned to the client.
  11. open class StreamingResponseCallContext<ResponseMessage: Message>: ServerCallContextBase {
  12. public typealias WrappedResponse = GRPCServerResponsePart<ResponseMessage>
  13. public let statusPromise: EventLoopPromise<GRPCStatus>
  14. public override init(eventLoop: EventLoop, request: HTTPRequestHead) {
  15. self.statusPromise = eventLoop.makePromise()
  16. super.init(eventLoop: eventLoop, request: request)
  17. }
  18. open func sendResponse(_ message: ResponseMessage) -> EventLoopFuture<Void> {
  19. fatalError("needs to be overridden")
  20. }
  21. }
  22. /// Concrete implementation of `StreamingResponseCallContext` used by our generated code.
  23. open class StreamingResponseCallContextImpl<ResponseMessage: Message>: StreamingResponseCallContext<ResponseMessage> {
  24. public let channel: Channel
  25. public init(channel: Channel, request: HTTPRequestHead) {
  26. self.channel = channel
  27. super.init(eventLoop: channel.eventLoop, request: request)
  28. statusPromise.futureResult
  29. // Ensure that any error provided can be transformed to `GRPCStatus`, using "internal server error" as a fallback.
  30. .recover { error in
  31. (error as? GRPCStatusTransformable)?.asGRPCStatus() ?? .processingError
  32. }
  33. // Finish the call by returning the final status.
  34. .whenSuccess {
  35. self.channel.writeAndFlush(NIOAny(WrappedResponse.status($0)), promise: nil)
  36. }
  37. }
  38. open override func sendResponse(_ message: ResponseMessage) -> EventLoopFuture<Void> {
  39. let promise: EventLoopPromise<Void> = eventLoop.makePromise()
  40. channel.writeAndFlush(NIOAny(WrappedResponse.message(message)), promise: promise)
  41. return promise.futureResult
  42. }
  43. }
  44. /// Concrete implementation of `StreamingResponseCallContext` used for testing.
  45. ///
  46. /// Simply records all sent messages.
  47. open class StreamingResponseCallContextTestStub<ResponseMessage: Message>: StreamingResponseCallContext<ResponseMessage> {
  48. open var recordedResponses: [ResponseMessage] = []
  49. open override func sendResponse(_ message: ResponseMessage) -> EventLoopFuture<Void> {
  50. recordedResponses.append(message)
  51. return eventLoop.makeSucceededFuture(())
  52. }
  53. }