ClientCall.swift 6.7 KB

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