GRPCAsyncServerStreamingCall.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 `ServerStreamingCall`.
  19. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  20. public struct GRPCAsyncServerStreamingCall<Request, Response> {
  21. private let call: Call<Request, Response>
  22. private let responseParts: StreamingResponseParts<Response>
  23. /// The stream of responses from the server.
  24. public let responses: GRPCAsyncResponseStream<Response>
  25. /// The options used to make the RPC.
  26. public var options: CallOptions {
  27. return self.call.options
  28. }
  29. /// Cancel this RPC if it hasn't already completed.
  30. public func cancel() async throws {
  31. try await self.call.cancel().get()
  32. }
  33. // MARK: - Response Parts
  34. /// The initial metadata returned from the server.
  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 trailing metadata returned from the server.
  42. ///
  43. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  44. public var trailingMetadata: HPACKHeaders {
  45. // swiftformat:disable:next redundantGet
  46. get async throws {
  47. try await self.responseParts.trailingMetadata.get()
  48. }
  49. }
  50. /// The final status of the the RPC.
  51. ///
  52. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  53. public var status: GRPCStatus {
  54. // swiftformat:disable:next redundantGet
  55. get async {
  56. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  57. try! await self.responseParts.status.get()
  58. }
  59. }
  60. private init(
  61. call: Call<Request, Response>,
  62. _ request: Request
  63. ) {
  64. self.call = call
  65. // Initialise `responseParts` with an empty response handler because we
  66. // provide the responses as an AsyncSequence in `responseStream`.
  67. self.responseParts = StreamingResponseParts(on: call.eventLoop) { _ in }
  68. // Call and StreamingResponseParts are reference types so we grab a
  69. // referecence to them here to avoid capturing mutable self in the closure
  70. // passed to the AsyncThrowingStream initializer.
  71. //
  72. // The alternative would be to declare the responseStream as:
  73. // ```
  74. // public private(set) var responseStream: AsyncThrowingStream<ResponsePayload>!
  75. // ```
  76. //
  77. // UPDATE: Additionally we expect to replace this soon with an AsyncSequence
  78. // implementation that supports yielding values from outside the closure.
  79. let call = self.call
  80. let responseParts = self.responseParts
  81. self
  82. .responses = GRPCAsyncResponseStream(AsyncThrowingStream(Response.self) { continuation in
  83. call.invokeUnaryRequest(request) { error in
  84. responseParts.handleError(error)
  85. continuation.finish(throwing: error)
  86. } onResponsePart: { responsePart in
  87. responseParts.handle(responsePart)
  88. switch responsePart {
  89. case let .message(response):
  90. continuation.yield(response)
  91. case .metadata:
  92. break
  93. case .end:
  94. continuation.finish()
  95. }
  96. }
  97. })
  98. }
  99. /// We expose this as the only non-private initializer so that the caller
  100. /// knows that invocation is part of initialisation.
  101. internal static func makeAndInvoke(
  102. call: Call<Request, Response>,
  103. _ request: Request
  104. ) -> Self {
  105. Self(call: call, request)
  106. }
  107. }
  108. #endif