GRPCAsyncServerStreamingCall.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2021, 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. #if compiler(>=5.6)
  17. import NIOCore
  18. import NIOHPACK
  19. /// Async-await variant of ``ServerStreamingCall``.
  20. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  21. public struct GRPCAsyncServerStreamingCall<Request: Sendable, Response: Sendable> {
  22. private let call: Call<Request, Response>
  23. private let responseParts: StreamingResponseParts<Response>
  24. private let responseSource: NIOThrowingAsyncSequenceProducer<
  25. Response,
  26. Error,
  27. NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark,
  28. GRPCAsyncSequenceProducerDelegate
  29. >.Source
  30. /// The stream of responses from the server.
  31. public let responseStream: GRPCAsyncResponseStream<Response>
  32. /// The options used to make the RPC.
  33. public var options: CallOptions {
  34. return self.call.options
  35. }
  36. /// The path used to make the RPC.
  37. public var path: String {
  38. return self.call.path
  39. }
  40. /// Cancel this RPC if it hasn't already completed.
  41. public func cancel() {
  42. self.call.cancel(promise: nil)
  43. }
  44. // MARK: - Response Parts
  45. /// The initial metadata returned from the server.
  46. ///
  47. /// - Important: The initial metadata will only be available when the first response has been
  48. /// received. However, it is not necessary for the response to have been consumed before reading
  49. /// this property.
  50. public var initialMetadata: HPACKHeaders {
  51. get async throws {
  52. try await self.responseParts.initialMetadata.get()
  53. }
  54. }
  55. /// The trailing metadata returned from the server.
  56. ///
  57. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  58. public var trailingMetadata: HPACKHeaders {
  59. get async throws {
  60. try await self.responseParts.trailingMetadata.get()
  61. }
  62. }
  63. /// The final status of the the RPC.
  64. ///
  65. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  66. public var status: GRPCStatus {
  67. get async {
  68. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  69. try! await self.responseParts.status.get()
  70. }
  71. }
  72. private init(call: Call<Request, Response>) {
  73. self.call = call
  74. // We ignore messages in the closure and instead feed them into the response source when we
  75. // invoke the `call`.
  76. self.responseParts = StreamingResponseParts(on: call.eventLoop) { _ in }
  77. let backpressureStrategy = NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark(
  78. lowWatermark: 10,
  79. highWatermark: 50
  80. )
  81. let sequenceProducer = NIOThrowingAsyncSequenceProducer.makeSequence(
  82. elementType: Response.self,
  83. failureType: Error.self,
  84. backPressureStrategy: backpressureStrategy,
  85. delegate: GRPCAsyncSequenceProducerDelegate()
  86. )
  87. self.responseSource = sequenceProducer.source
  88. self.responseStream = .init(sequenceProducer.sequence)
  89. }
  90. /// We expose this as the only non-private initializer so that the caller
  91. /// knows that invocation is part of initialisation.
  92. internal static func makeAndInvoke(
  93. call: Call<Request, Response>,
  94. _ request: Request
  95. ) -> Self {
  96. let asyncCall = Self(call: call)
  97. asyncCall.call.invokeUnaryRequest(
  98. request,
  99. onStart: {},
  100. onError: { error in
  101. asyncCall.responseParts.handleError(error)
  102. asyncCall.responseSource.finish(error)
  103. },
  104. onResponsePart: AsyncCall.makeResponsePartHandler(
  105. responseParts: asyncCall.responseParts,
  106. responseSource: asyncCall.responseSource,
  107. requestStream: nil,
  108. requestType: Request.self
  109. )
  110. )
  111. return asyncCall
  112. }
  113. }
  114. #endif