2
0

GRPCAsyncServerStreamingCall.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ///
  36. /// - Important: The initial metadata will only be available when the first response has been
  37. /// received. However, it is not necessary for the response to have been consumed before reading
  38. /// this property.
  39. public var initialMetadata: HPACKHeaders {
  40. // swiftformat:disable:next redundantGet
  41. get async throws {
  42. try await self.responseParts.initialMetadata.get()
  43. }
  44. }
  45. /// The trailing metadata returned from the server.
  46. ///
  47. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  48. public var trailingMetadata: HPACKHeaders {
  49. // swiftformat:disable:next redundantGet
  50. get async throws {
  51. try await self.responseParts.trailingMetadata.get()
  52. }
  53. }
  54. /// The final status of the the RPC.
  55. ///
  56. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  57. public var status: GRPCStatus {
  58. // swiftformat:disable:next redundantGet
  59. get async {
  60. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  61. try! await self.responseParts.status.get()
  62. }
  63. }
  64. private init(
  65. call: Call<Request, Response>,
  66. _ request: Request
  67. ) {
  68. self.call = call
  69. // Initialise `responseParts` with an empty response handler because we
  70. // provide the responses as an AsyncSequence in `responseStream`.
  71. self.responseParts = StreamingResponseParts(on: call.eventLoop) { _ in }
  72. // Call and StreamingResponseParts are reference types so we grab a
  73. // referecence to them here to avoid capturing mutable self in the closure
  74. // passed to the AsyncThrowingStream initializer.
  75. //
  76. // The alternative would be to declare the responseStream as:
  77. // ```
  78. // public private(set) var responseStream: AsyncThrowingStream<ResponsePayload>!
  79. // ```
  80. //
  81. // UPDATE: Additionally we expect to replace this soon with an AsyncSequence
  82. // implementation that supports yielding values from outside the closure.
  83. let call = self.call
  84. let responseParts = self.responseParts
  85. self
  86. .responses = GRPCAsyncResponseStream(AsyncThrowingStream(Response.self) { continuation in
  87. call.invokeUnaryRequest(request) { error in
  88. responseParts.handleError(error)
  89. continuation.finish(throwing: error)
  90. } onResponsePart: { responsePart in
  91. responseParts.handle(responsePart)
  92. switch responsePart {
  93. case let .message(response):
  94. continuation.yield(response)
  95. case .metadata:
  96. break
  97. case .end:
  98. continuation.finish()
  99. }
  100. }
  101. })
  102. }
  103. /// We expose this as the only non-private initializer so that the caller
  104. /// knows that invocation is part of initialisation.
  105. internal static func makeAndInvoke(
  106. call: Call<Request, Response>,
  107. _ request: Request
  108. ) -> Self {
  109. Self(call: call, request)
  110. }
  111. }
  112. #endif