GRPCAsyncServerStreamingCall.swift 4.1 KB

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