GRPCAsyncClientStreamingCall.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ``ClientStreamingCall``.
  20. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  21. public struct GRPCAsyncClientStreamingCall<Request: Sendable, Response: Sendable>: Sendable {
  22. private let call: Call<Request, Response>
  23. private let responseParts: UnaryResponseParts<Response>
  24. private let requestSink: AsyncSink<(Request, Compression)>
  25. /// A request stream writer for sending messages to the server.
  26. public let requestStream: GRPCAsyncRequestStreamWriter<Request>
  27. /// The options used to make the RPC.
  28. public var options: CallOptions {
  29. return self.call.options
  30. }
  31. /// The path used to make the RPC.
  32. public var path: String {
  33. return self.call.path
  34. }
  35. /// Cancel this RPC if it hasn't already completed.
  36. public func cancel() {
  37. self.call.cancel(promise: nil)
  38. }
  39. // MARK: - Response Parts
  40. /// The initial metadata returned from the server.
  41. ///
  42. /// - Important: The initial metadata will only be available when the response has been received.
  43. public var initialMetadata: HPACKHeaders {
  44. get async throws {
  45. try await self.responseParts.initialMetadata.get()
  46. }
  47. }
  48. /// The response returned by the server.
  49. public var response: Response {
  50. get async throws {
  51. try await self.responseParts.response.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. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  74. let (requestStream, requestSink) = call.makeRequestStreamWriter()
  75. self.requestStream = requestStream
  76. self.requestSink = AsyncSink(wrapping: requestSink)
  77. }
  78. /// We expose this as the only non-private initializer so that the caller
  79. /// knows that invocation is part of initialisation.
  80. internal static func makeAndInvoke(call: Call<Request, Response>) -> Self {
  81. let asyncCall = Self(call: call)
  82. asyncCall.call.invokeStreamingRequests(
  83. onStart: {
  84. asyncCall.requestSink.setWritability(to: true)
  85. },
  86. onError: { error in
  87. asyncCall.responseParts.handleError(error)
  88. asyncCall.requestSink.finish(error: error)
  89. },
  90. onResponsePart: AsyncCall.makeResponsePartHandler(
  91. responseParts: asyncCall.responseParts,
  92. requestStream: asyncCall.requestStream
  93. )
  94. )
  95. return asyncCall
  96. }
  97. }
  98. #endif