UnaryResponseCallContext.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import Foundation
  2. import SwiftProtobuf
  3. import NIO
  4. import NIOHTTP1
  5. /// Abstract base class exposing a method that exposes a promise for the RPC response.
  6. ///
  7. /// - When `responsePromise` is fulfilled, the call is closed and the provided response transmitted with status `responseStatus` (`.ok` by default).
  8. /// - If `statusPromise` is failed and the error is of type `GRPCStatus`, that error will be returned to the client.
  9. /// - For other errors, `GRPCStatus.processingError` is returned to the client.
  10. ///
  11. /// For unary calls, the response is not actually provided by fulfilling `responsePromise`, but instead by completing
  12. /// the future returned by `UnaryCallHandler.EventObserver`.
  13. open class UnaryResponseCallContext<ResponseMessage: Message>: ServerCallContextBase, StatusOnlyCallContext {
  14. public typealias WrappedResponse = GRPCServerResponsePart<ResponseMessage>
  15. public let responsePromise: EventLoopPromise<ResponseMessage>
  16. public var responseStatus: GRPCStatus = .ok
  17. public override init(eventLoop: EventLoop, request: HTTPRequestHead) {
  18. self.responsePromise = eventLoop.newPromise()
  19. super.init(eventLoop: eventLoop, request: request)
  20. }
  21. }
  22. /// Protocol variant of `UnaryResponseCallContext` that only exposes the `responseStatus` field, but not
  23. /// `responsePromise`.
  24. ///
  25. /// Motivation: `UnaryCallHandler` already asks the call handler return an `EventLoopFuture<ResponseMessage>` which
  26. /// is automatically cascaded into `UnaryResponseCallContext.responsePromise`, so that promise does not (and should not)
  27. /// be fulfilled by the user.
  28. ///
  29. /// We can use a protocol (instead of an abstract base class) here because removing the generic `responsePromise` field
  30. /// lets us avoid associated-type requirements on the protocol.
  31. public protocol StatusOnlyCallContext: ServerCallContext {
  32. var responseStatus: GRPCStatus { get set }
  33. }
  34. /// Concrete implementation of `UnaryResponseCallContext` used by our generated code.
  35. open class UnaryResponseCallContextImpl<ResponseMessage: Message>: UnaryResponseCallContext<ResponseMessage> {
  36. public let channel: Channel
  37. public init(channel: Channel, request: HTTPRequestHead) {
  38. self.channel = channel
  39. super.init(eventLoop: channel.eventLoop, request: request)
  40. responsePromise.futureResult
  41. .map { responseMessage in
  42. // Send the response provided to the promise.
  43. //! FIXME: It would be nicer to chain sending the status onto a successful write, but for some reason the
  44. // "write message" future doesn't seem to get fulfilled?
  45. self.channel.writeAndFlush(NIOAny(WrappedResponse.message(responseMessage)), promise: nil)
  46. return self.responseStatus
  47. }
  48. // Ensure that any error provided is of type `GRPCStatus`, using "internal server error" as a fallback.
  49. .mapIfError { error in
  50. (error as? GRPCStatus) ?? .processingError
  51. }
  52. // Finish the call by returning the final status.
  53. .whenSuccess { status in
  54. self.channel.writeAndFlush(NIOAny(WrappedResponse.status(status)), promise: nil)
  55. }
  56. }
  57. }
  58. /// Concrete implementation of `UnaryResponseCallContext` used for testing.
  59. ///
  60. /// Only provided to make it clear in tests that no "real" implementation is used.
  61. open class UnaryResponseCallContextTestStub<ResponseMessage: Message>: UnaryResponseCallContext<ResponseMessage> { }