StreamingResponseCallContext.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /// - Parameters:
  26. /// - channel: The NIO channel the call is handled on.
  27. /// - request: The headers provided with this call.
  28. /// - errorDelegate: Provides a means for transforming status promise failures to `GRPCStatusTransformable` before
  29. /// sending them to the client.
  30. ///
  31. /// Note: `errorDelegate` is not called for status promise that are `succeeded` with a non-OK status.
  32. public init(channel: Channel, request: HTTPRequestHead, errorDelegate: ServerErrorDelegate?) {
  33. self.channel = channel
  34. super.init(eventLoop: channel.eventLoop, request: request)
  35. statusPromise.futureResult
  36. // Ensure that any error provided can be transformed to `GRPCStatus`, using "internal server error" as a fallback.
  37. .recover { [weak errorDelegate] error in
  38. errorDelegate?.observeRequestHandlerError(error, request: request)
  39. return errorDelegate?.transformRequestHandlerError(error, request: request)
  40. ?? (error as? GRPCStatusTransformable)?.asGRPCStatus()
  41. ?? .processingError
  42. }
  43. // Finish the call by returning the final status.
  44. .whenSuccess {
  45. self.channel.writeAndFlush(NIOAny(WrappedResponse.status($0)), promise: nil)
  46. }
  47. }
  48. open override func sendResponse(_ message: ResponseMessage) -> EventLoopFuture<Void> {
  49. let promise: EventLoopPromise<Void> = eventLoop.makePromise()
  50. channel.writeAndFlush(NIOAny(WrappedResponse.message(message)), promise: promise)
  51. return promise.futureResult
  52. }
  53. }
  54. /// Concrete implementation of `StreamingResponseCallContext` used for testing.
  55. ///
  56. /// Simply records all sent messages.
  57. open class StreamingResponseCallContextTestStub<ResponseMessage: Message>: StreamingResponseCallContext<ResponseMessage> {
  58. open var recordedResponses: [ResponseMessage] = []
  59. open override func sendResponse(_ message: ResponseMessage) -> EventLoopFuture<Void> {
  60. recordedResponses.append(message)
  61. return eventLoop.makeSucceededFuture(())
  62. }
  63. }