ServerStreamingCallHandler.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import SwiftProtobuf
  18. import NIO
  19. import NIOHTTP1
  20. import Logging
  21. /// Handles server-streaming calls. Calls the observer block with the request message.
  22. ///
  23. /// - The observer block is implemented by the framework user and calls `context.sendResponse` as needed.
  24. /// - To close the call and send the status, complete the status future returned by the observer block.
  25. public class ServerStreamingCallHandler<RequestMessage: Message, ResponseMessage: Message>: BaseCallHandler<RequestMessage, ResponseMessage> {
  26. public typealias EventObserver = (RequestMessage) -> EventLoopFuture<GRPCStatus>
  27. private var eventObserver: EventObserver?
  28. private var callContext: StreamingResponseCallContext<ResponseMessage>?
  29. public init(callHandlerContext: CallHandlerContext, eventObserverFactory: (StreamingResponseCallContext<ResponseMessage>) -> EventObserver) {
  30. super.init(callHandlerContext: callHandlerContext)
  31. let callContext = StreamingResponseCallContextImpl<ResponseMessage>(
  32. channel: self.callHandlerContext.channel,
  33. request: self.callHandlerContext.request,
  34. errorDelegate: self.callHandlerContext.errorDelegate,
  35. logger: self.callHandlerContext.logger
  36. )
  37. self.callContext = callContext
  38. self.eventObserver = eventObserverFactory(callContext)
  39. callContext.statusPromise.futureResult.whenComplete { _ in
  40. // When done, reset references to avoid retain cycles.
  41. self.eventObserver = nil
  42. self.callContext = nil
  43. }
  44. }
  45. public override func processMessage(_ message: RequestMessage) throws {
  46. guard let eventObserver = self.eventObserver,
  47. let callContext = self.callContext else {
  48. self.logger.error("processMessage(_:) called before the call started or after the call completed")
  49. throw GRPCError.server(.tooManyRequests)
  50. }
  51. let resultFuture = eventObserver(message)
  52. resultFuture
  53. // Fulfill the status promise with whatever status the framework user has provided.
  54. .cascade(to: callContext.statusPromise)
  55. self.eventObserver = nil
  56. }
  57. public override func endOfStreamReceived() throws {
  58. if self.eventObserver != nil {
  59. throw GRPCError.server(.noRequestsButOneExpected)
  60. }
  61. }
  62. override func sendErrorStatus(_ status: GRPCStatus) {
  63. self.callContext?.statusPromise.fail(status)
  64. }
  65. }