2
0

GRPCAsyncClientStreamingCall.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public var initialMetadata: HPACKHeaders {
  34. // swiftformat:disable:next redundantGet
  35. get async throws {
  36. try await self.responseParts.initialMetadata.get()
  37. }
  38. }
  39. /// The response returned by the server.
  40. public var response: Response {
  41. // swiftformat:disable:next redundantGet
  42. get async throws {
  43. try await self.responseParts.response.get()
  44. }
  45. }
  46. /// The trailing metadata returned from the server.
  47. ///
  48. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  49. public var trailingMetadata: HPACKHeaders {
  50. // swiftformat:disable:next redundantGet
  51. get async throws {
  52. try await self.responseParts.trailingMetadata.get()
  53. }
  54. }
  55. /// The final status of the the RPC.
  56. ///
  57. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  58. public var status: GRPCStatus {
  59. // swiftformat:disable:next redundantGet
  60. get async {
  61. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  62. try! await self.responseParts.status.get()
  63. }
  64. }
  65. private init(call: Call<Request, Response>) {
  66. self.call = call
  67. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  68. self.call.invokeStreamingRequests(
  69. onError: self.responseParts.handleError(_:),
  70. onResponsePart: self.responseParts.handle(_:)
  71. )
  72. }
  73. /// We expose this as the only non-private initializer so that the caller
  74. /// knows that invocation is part of initialisation.
  75. internal static func makeAndInvoke(call: Call<Request, Response>) -> Self {
  76. Self(call: call)
  77. }
  78. // MARK: - Requests
  79. /// Sends a message to the service.
  80. ///
  81. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()`.
  82. ///
  83. /// - Parameters:
  84. /// - message: The message to send.
  85. /// - compression: Whether compression should be used for this message. Ignored if compression
  86. /// was not enabled for the RPC.
  87. public func sendMessage(
  88. _ message: Request,
  89. compression: Compression = .deferToCallDefault
  90. ) async throws {
  91. let compress = self.call.compress(compression)
  92. let promise = self.call.eventLoop.makePromise(of: Void.self)
  93. self.call.send(.message(message, .init(compress: compress, flush: true)), promise: promise)
  94. // 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?
  95. try await promise.futureResult.get()
  96. }
  97. /// Sends a sequence of messages to the service.
  98. ///
  99. /// - Important: Callers must terminate the stream of messages by calling `sendEnd()`.
  100. ///
  101. /// - Parameters:
  102. /// - messages: The sequence of messages to send.
  103. /// - compression: Whether compression should be used for this message. Ignored if compression
  104. /// was not enabled for the RPC.
  105. public func sendMessages<S>(
  106. _ messages: S,
  107. compression: Compression = .deferToCallDefault
  108. ) async throws where S: Sequence, S.Element == Request {
  109. let promise = self.call.eventLoop.makePromise(of: Void.self)
  110. self.call.sendMessages(messages, compression: compression, promise: promise)
  111. try await promise.futureResult.get()
  112. }
  113. /// Terminates a stream of messages sent to the service.
  114. ///
  115. /// - Important: This should only ever be called once.
  116. public func sendEnd() async throws {
  117. let promise = self.call.eventLoop.makePromise(of: Void.self)
  118. self.call.send(.end, promise: promise)
  119. try await promise.futureResult.get()
  120. }
  121. }
  122. #endif