GRPCAsyncClientStreamingCall.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ``ClientStreamingCall``.
  19. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  20. public struct GRPCAsyncClientStreamingCall<Request: Sendable, Response: Sendable>: Sendable {
  21. private let call: Call<Request, Response>
  22. private let responseParts: UnaryResponseParts<Response>
  23. private let requestSink: AsyncSink<(Request, Compression)>
  24. /// A request stream writer for sending messages to the server.
  25. public let requestStream: GRPCAsyncRequestStreamWriter<Request>
  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. private func withRPCCancellation<R: Sendable>(_ fn: () async throws -> R) async rethrows -> R {
  40. return try await withTaskCancellationHandler(operation: fn) {
  41. self.cancel()
  42. }
  43. }
  44. /// The initial metadata returned from the server.
  45. ///
  46. /// - Important: The initial metadata will only be available when the response has been received.
  47. public var initialMetadata: HPACKHeaders {
  48. get async throws {
  49. return try await self.withRPCCancellation {
  50. try await self.responseParts.initialMetadata.get()
  51. }
  52. }
  53. }
  54. /// The response returned by the server.
  55. public var response: Response {
  56. get async throws {
  57. return try await self.withRPCCancellation {
  58. try await self.responseParts.response.get()
  59. }
  60. }
  61. }
  62. /// The trailing metadata returned from the server.
  63. ///
  64. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  65. public var trailingMetadata: HPACKHeaders {
  66. get async throws {
  67. return try await self.withRPCCancellation {
  68. try await self.responseParts.trailingMetadata.get()
  69. }
  70. }
  71. }
  72. /// The final status of the the RPC.
  73. ///
  74. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  75. public var status: GRPCStatus {
  76. get async {
  77. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  78. return await self.withRPCCancellation {
  79. try! await self.responseParts.status.get()
  80. }
  81. }
  82. }
  83. private init(call: Call<Request, Response>) {
  84. self.call = call
  85. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  86. let (requestStream, requestSink) = call.makeRequestStreamWriter()
  87. self.requestStream = requestStream
  88. self.requestSink = AsyncSink(wrapping: requestSink)
  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(call: Call<Request, Response>) -> Self {
  93. let asyncCall = Self(call: call)
  94. asyncCall.call.invokeStreamingRequests(
  95. onStart: {
  96. asyncCall.requestSink.setWritability(to: true)
  97. },
  98. onError: { error in
  99. asyncCall.responseParts.handleError(error)
  100. asyncCall.requestSink.finish(error: error)
  101. },
  102. onResponsePart: AsyncCall.makeResponsePartHandler(
  103. responseParts: asyncCall.responseParts,
  104. requestStream: asyncCall.requestStream
  105. )
  106. )
  107. return asyncCall
  108. }
  109. }