GRPCAsyncClientStreamingCall.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.5)
  17. import NIOHPACK
  18. /// Async-await variant of `ClientStreamingCall`.
  19. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  20. public struct GRPCAsyncClientStreamingCall<Request, Response> {
  21. private let call: Call<Request, Response>
  22. private let responseParts: UnaryResponseParts<Response>
  23. /// The options used to make the RPC.
  24. public var options: CallOptions {
  25. return self.call.options
  26. }
  27. /// Cancel this RPC if it hasn't already completed.
  28. public func cancel() async throws {
  29. try await self.call.cancel().get()
  30. }
  31. // MARK: - Response Parts
  32. /// The initial metadata returned from the server.
  33. ///
  34. /// - Important: The initial metadata will only be available when the response has been received.
  35. public var initialMetadata: HPACKHeaders {
  36. // swiftformat:disable:next redundantGet
  37. get async throws {
  38. try await self.responseParts.initialMetadata.get()
  39. }
  40. }
  41. /// The response returned by the server.
  42. public var response: Response {
  43. // swiftformat:disable:next redundantGet
  44. get async throws {
  45. try await self.responseParts.response.get()
  46. }
  47. }
  48. /// The trailing metadata returned from the server.
  49. ///
  50. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  51. public var trailingMetadata: HPACKHeaders {
  52. // swiftformat:disable:next redundantGet
  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. // swiftformat:disable:next redundantGet
  62. get async {
  63. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  64. try! await self.responseParts.status.get()
  65. }
  66. }
  67. private init(call: Call<Request, Response>) {
  68. self.call = call
  69. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  70. self.call.invokeStreamingRequests(
  71. onError: self.responseParts.handleError(_:),
  72. onResponsePart: self.responseParts.handle(_:)
  73. )
  74. }
  75. /// We expose this as the only non-private initializer so that the caller
  76. /// knows that invocation is part of initialisation.
  77. internal static func makeAndInvoke(call: Call<Request, Response>) -> Self {
  78. Self(call: call)
  79. }
  80. // MARK: - Requests
  81. /// Sends a message to the service.
  82. ///
  83. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()`.
  84. ///
  85. /// - Parameters:
  86. /// - message: The message to send.
  87. /// - compression: Whether compression should be used for this message. Ignored if compression
  88. /// was not enabled for the RPC.
  89. public func sendMessage(
  90. _ message: Request,
  91. compression: Compression = .deferToCallDefault
  92. ) async throws {
  93. let compress = self.call.compress(compression)
  94. let promise = self.call.eventLoop.makePromise(of: Void.self)
  95. self.call.send(.message(message, .init(compress: compress, flush: true)), promise: promise)
  96. // TODO: This waits for the message to be written to the socket. We should probably just wait for it to be written to the channel?
  97. try await promise.futureResult.get()
  98. }
  99. /// Sends a sequence of messages to the service.
  100. ///
  101. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()`.
  102. ///
  103. /// - Parameters:
  104. /// - messages: The sequence of messages to send.
  105. /// - compression: Whether compression should be used for this message. Ignored if compression
  106. /// was not enabled for the RPC.
  107. public func sendMessages<S>(
  108. _ messages: S,
  109. compression: Compression = .deferToCallDefault
  110. ) async throws where S: Sequence, S.Element == Request {
  111. let promise = self.call.eventLoop.makePromise(of: Void.self)
  112. self.call.sendMessages(messages, compression: compression, promise: promise)
  113. try await promise.futureResult.get()
  114. }
  115. /// Terminates a stream of messages sent to the service.
  116. ///
  117. /// - Important: This should only ever be called once.
  118. public func sendEnd() async throws {
  119. let promise = self.call.eventLoop.makePromise(of: Void.self)
  120. self.call.send(.end, promise: promise)
  121. try await promise.futureResult.get()
  122. }
  123. }
  124. #endif