GRPCAsyncUnaryCall.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 NIOHPACK
  17. /// A unary gRPC call. The request is sent on initialization.
  18. ///
  19. /// Note: while this object is a `struct`, its implementation delegates to ``Call``. It therefore
  20. /// has reference semantics.
  21. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  22. public struct GRPCAsyncUnaryCall<Request: Sendable, Response: Sendable>: Sendable {
  23. private let call: Call<Request, Response>
  24. private let responseParts: UnaryResponseParts<Response>
  25. /// The options used to make the RPC.
  26. public var options: CallOptions {
  27. self.call.options
  28. }
  29. /// The path used to make the RPC.
  30. public var path: String {
  31. self.call.path
  32. }
  33. /// Cancel this RPC if it hasn't already completed.
  34. public func cancel() {
  35. self.call.cancel(promise: nil)
  36. }
  37. // MARK: - Response Parts
  38. private func withRPCCancellation<R: Sendable>(_ fn: () async throws -> R) async rethrows -> R {
  39. return try await withTaskCancellationHandler(operation: fn) {
  40. self.cancel()
  41. }
  42. }
  43. /// The initial metadata returned from the server.
  44. ///
  45. /// - Important: The initial metadata will only be available when the response has been received.
  46. public var initialMetadata: HPACKHeaders {
  47. get async throws {
  48. try await self.withRPCCancellation {
  49. try await self.responseParts.initialMetadata.get()
  50. }
  51. }
  52. }
  53. /// The response message returned from the service if the call is successful. This may be throw
  54. /// if the call encounters an error.
  55. ///
  56. /// Callers should rely on the `status` of the call for the canonical outcome.
  57. public var response: Response {
  58. get async throws {
  59. try await self.withRPCCancellation {
  60. try await self.responseParts.response.get()
  61. }
  62. }
  63. }
  64. /// The trailing metadata returned from the server.
  65. ///
  66. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  67. public var trailingMetadata: HPACKHeaders {
  68. get async throws {
  69. try await self.withRPCCancellation {
  70. try await self.responseParts.trailingMetadata.get()
  71. }
  72. }
  73. }
  74. /// The final status of the the RPC.
  75. ///
  76. /// - Important: Awaiting this property will suspend until the responses have been consumed.
  77. public var status: GRPCStatus {
  78. get async {
  79. // force-try acceptable because any error is encapsulated in a successful GRPCStatus future.
  80. await self.withRPCCancellation {
  81. try! await self.responseParts.status.get()
  82. }
  83. }
  84. }
  85. private init(
  86. call: Call<Request, Response>,
  87. _ request: Request
  88. ) {
  89. self.call = call
  90. self.responseParts = UnaryResponseParts(on: call.eventLoop)
  91. self.call.invokeUnaryRequest(
  92. request,
  93. onStart: {},
  94. onError: self.responseParts.handleError(_:),
  95. onResponsePart: self.responseParts.handle(_:)
  96. )
  97. }
  98. /// We expose this as the only non-private initializer so that the caller
  99. /// knows that invocation is part of initialisation.
  100. internal static func makeAndInvoke(
  101. call: Call<Request, Response>,
  102. _ request: Request
  103. ) -> Self {
  104. Self(call: call, request)
  105. }
  106. }