ClientCall.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import NIOCore
  18. import NIOHPACK
  19. import NIOHTTP1
  20. import NIOHTTP2
  21. import SwiftProtobuf
  22. /// Base protocol for a client call to a gRPC service.
  23. public protocol ClientCall {
  24. /// The type of the request message for the call.
  25. associatedtype RequestPayload
  26. /// The type of the response message for the call.
  27. associatedtype ResponsePayload
  28. /// The event loop this call is running on.
  29. var eventLoop: EventLoop { get }
  30. /// The options used to make the RPC.
  31. var options: CallOptions { get }
  32. /// HTTP/2 stream that requests and responses are sent and received on.
  33. var subchannel: EventLoopFuture<Channel> { get }
  34. /// Initial response metadata.
  35. var initialMetadata: EventLoopFuture<HPACKHeaders> { get }
  36. /// Status of this call which may be populated by the server or client.
  37. ///
  38. /// The client may populate the status if, for example, it was not possible to connect to the service.
  39. ///
  40. /// Note: despite ``GRPCStatus`` conforming to `Error`, the value will be __always__ delivered as a __success__
  41. /// result even if the status represents a __negative__ outcome. This future will __never__ be fulfilled
  42. /// with an error.
  43. var status: EventLoopFuture<GRPCStatus> { get }
  44. /// Trailing response metadata.
  45. var trailingMetadata: EventLoopFuture<HPACKHeaders> { get }
  46. /// Cancel the current call.
  47. ///
  48. /// Closes the HTTP/2 stream once it becomes available. Additional writes to the channel will be ignored.
  49. /// Any unfulfilled promises will be failed with a cancelled status (excepting ``status`` which will be
  50. /// succeeded, if not already succeeded).
  51. func cancel(promise: EventLoopPromise<Void>?)
  52. }
  53. extension ClientCall {
  54. func cancel() -> EventLoopFuture<Void> {
  55. let promise = self.eventLoop.makePromise(of: Void.self)
  56. self.cancel(promise: promise)
  57. return promise.futureResult
  58. }
  59. }
  60. /// A ``ClientCall`` with request streaming; i.e. client-streaming and bidirectional-streaming.
  61. public protocol StreamingRequestClientCall: ClientCall {
  62. /// Sends a message to the service.
  63. ///
  64. /// - Important: Callers must terminate the stream of messages by calling <doc:/documentation/GRPC/StreamingRequestClientCall/sendEnd()-7zuxl>
  65. /// or ``sendEnd(promise:)``.
  66. ///
  67. /// - Parameters:
  68. /// - message: The message to send.
  69. /// - compression: Whether compression should be used for this message. Ignored if compression
  70. /// was not enabled for the RPC.
  71. /// - Returns: A future which will be fullfilled when the message has been sent.
  72. func sendMessage(_ message: RequestPayload, compression: Compression) -> EventLoopFuture<Void>
  73. /// Sends a message to the service.
  74. ///
  75. /// - Important: Callers must terminate the stream of messages by calling ``sendEnd()-7bhdp`` or ``sendEnd(promise:)``.
  76. ///
  77. /// - Parameters:
  78. /// - message: The message to send.
  79. /// - compression: Whether compression should be used for this message. Ignored if compression
  80. /// was not enabled for the RPC.
  81. /// - promise: A promise to be fulfilled when the message has been sent.
  82. func sendMessage(
  83. _ message: RequestPayload,
  84. compression: Compression,
  85. promise: EventLoopPromise<Void>?
  86. )
  87. /// Sends a sequence of messages to the service.
  88. ///
  89. /// - Important: Callers must terminate the stream of messages by calling <doc:/documentation/GRPC/StreamingRequestClientCall/sendEnd()-7bhdp>
  90. /// or ``sendEnd(promise:)``.
  91. ///
  92. /// - Parameters:
  93. /// - messages: The sequence of messages to send.
  94. /// - compression: Whether compression should be used for this message. Ignored if compression
  95. /// was not enabled for the RPC.
  96. func sendMessages<S: Sequence>(_ messages: S, compression: Compression) -> EventLoopFuture<Void>
  97. where S.Element == RequestPayload
  98. /// Sends a sequence of messages to the service.
  99. ///
  100. /// - Important: Callers must terminate the stream of messages by calling ``sendEnd()-7bhdp`` or ``sendEnd(promise:)``.
  101. ///
  102. /// - Parameters:
  103. /// - messages: The sequence of messages to send.
  104. /// - compression: Whether compression should be used for this message. Ignored if compression
  105. /// was not enabled for the RPC.
  106. /// - promise: A promise to be fulfilled when all messages have been sent successfully.
  107. func sendMessages<S: Sequence>(
  108. _ messages: S,
  109. compression: Compression,
  110. promise: EventLoopPromise<Void>?
  111. ) where S.Element == RequestPayload
  112. /// Terminates a stream of messages sent to the service.
  113. ///
  114. /// - Important: This should only ever be called once.
  115. /// - Returns: A future which will be fulfilled when the end has been sent.
  116. func sendEnd() -> EventLoopFuture<Void>
  117. /// Terminates a stream of messages sent to the service.
  118. ///
  119. /// - Important: This should only ever be called once.
  120. /// - Parameter promise: A promise to be fulfilled when the end has been sent.
  121. func sendEnd(promise: EventLoopPromise<Void>?)
  122. }
  123. extension StreamingRequestClientCall {
  124. public func sendMessage(
  125. _ message: RequestPayload,
  126. compression: Compression = .deferToCallDefault
  127. ) -> EventLoopFuture<Void> {
  128. let promise = self.eventLoop.makePromise(of: Void.self)
  129. self.sendMessage(message, compression: compression, promise: promise)
  130. return promise.futureResult
  131. }
  132. public func sendMessages<S: Sequence>(
  133. _ messages: S,
  134. compression: Compression = .deferToCallDefault
  135. ) -> EventLoopFuture<Void>
  136. where S.Element == RequestPayload {
  137. let promise = self.eventLoop.makePromise(of: Void.self)
  138. self.sendMessages(messages, compression: compression, promise: promise)
  139. return promise.futureResult
  140. }
  141. public func sendEnd() -> EventLoopFuture<Void> {
  142. let promise = self.eventLoop.makePromise(of: Void.self)
  143. self.sendEnd(promise: promise)
  144. return promise.futureResult
  145. }
  146. }
  147. /// A ``ClientCall`` with a unary response; i.e. unary and client-streaming.
  148. public protocol UnaryResponseClientCall: ClientCall {
  149. /// The response message returned from the service if the call is successful. This may be failed
  150. /// if the call encounters an error.
  151. ///
  152. /// Callers should rely on the `status` of the call for the canonical outcome.
  153. var response: EventLoopFuture<ResponsePayload> { get }
  154. }