GRPCAsyncServerStreamingCall.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 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: PassthroughMessageSource<Response, Error>
  24. /// The stream of responses from the server.
  25. public let responseStream: GRPCAsyncResponseStream<Response>
  26. /// The options used to make the RPC.
  27. public var options: CallOptions {
  28. return self.call.options
  29. }
  30. /// The path used to make the RPC.
  31. public var path: String {
  32. return self.call.path
  33. }
  34. /// Cancel this RPC if it hasn't already completed.
  35. public func cancel() {
  36. self.call.cancel(promise: nil)
  37. }
  38. // MARK: - Response Parts
  39. /// The initial metadata returned from the server.
  40. ///
  41. /// - Important: The initial metadata will only be available when the first response has been
  42. /// received. However, it is not necessary for the response to have been consumed before reading
  43. /// this property.
  44. public var initialMetadata: HPACKHeaders {
  45. get async throws {
  46. try await self.responseParts.initialMetadata.get()
  47. }
  48. }
  49. /// The trailing metadata returned from the server.
  50. ///
  51. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  52. public var trailingMetadata: HPACKHeaders {
  53. get async throws {
  54. try await self.responseParts.trailingMetadata.get()
  55. }
  56. }
  57. /// The final status of the the RPC.
  58. ///
  59. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  60. public var status: GRPCStatus {
  61. get async {
  62. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  63. try! await self.responseParts.status.get()
  64. }
  65. }
  66. private init(call: Call<Request, Response>) {
  67. self.call = call
  68. // We ignore messages in the closure and instead feed them into the response source when we
  69. // invoke the `call`.
  70. self.responseParts = StreamingResponseParts(on: call.eventLoop) { _ in }
  71. self.responseSource = PassthroughMessageSource<Response, Error>()
  72. self.responseStream = .init(PassthroughMessageSequence(consuming: self.responseSource))
  73. }
  74. /// We expose this as the only non-private initializer so that the caller
  75. /// knows that invocation is part of initialisation.
  76. internal static func makeAndInvoke(
  77. call: Call<Request, Response>,
  78. _ request: Request
  79. ) -> Self {
  80. let asyncCall = Self(call: call)
  81. asyncCall.call.invokeUnaryRequest(
  82. request,
  83. onStart: {},
  84. onError: { error in
  85. asyncCall.responseParts.handleError(error)
  86. asyncCall.responseSource.finish(throwing: error)
  87. },
  88. onResponsePart: AsyncCall.makeResponsePartHandler(
  89. responseParts: asyncCall.responseParts,
  90. responseSource: asyncCall.responseSource,
  91. requestStream: nil,
  92. requestType: Request.self
  93. )
  94. )
  95. return asyncCall
  96. }
  97. }
  98. #endif