GRPCChannel.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2020, 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 NIOCore
  17. import NIOHTTP2
  18. import SwiftProtobuf
  19. public protocol GRPCChannel: GRPCPreconcurrencySendable {
  20. /// Makes a gRPC call on the channel with requests and responses conforming to
  21. /// `SwiftProtobuf.Message`.
  22. ///
  23. /// Note: this is a lower-level construct that any of `UnaryCall`, `ClientStreamingCall`,
  24. /// `ServerStreamingCall` or `BidirectionalStreamingCall` and does not have an API to protect
  25. /// users against protocol violations (such as sending to requests on a unary call).
  26. ///
  27. /// After making the `Call`, users must `invoke` the call with a callback which is invoked
  28. /// for each response part (or error) received. Any call to `send(_:promise:)` prior to calling
  29. /// `invoke` will fail and not be sent. Users are also responsible for closing the request stream
  30. /// by sending the `.end` request part.
  31. ///
  32. /// - Parameters:
  33. /// - path: The path of the RPC, e.g. "/echo.Echo/get".
  34. /// - type: The type of the RPC, e.g. `.unary`.
  35. /// - callOptions: Options for the RPC.
  36. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  37. func makeCall<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>(
  38. path: String,
  39. type: GRPCCallType,
  40. callOptions: CallOptions,
  41. interceptors: [ClientInterceptor<Request, Response>]
  42. ) -> Call<Request, Response>
  43. /// Makes a gRPC call on the channel with requests and responses conforming to `GRPCPayload`.
  44. ///
  45. /// Note: this is a lower-level construct that any of `UnaryCall`, `ClientStreamingCall`,
  46. /// `ServerStreamingCall` or `BidirectionalStreamingCall` and does not have an API to protect
  47. /// users against protocol violations (such as sending to requests on a unary call).
  48. ///
  49. /// After making the `Call`, users must `invoke` the call with a callback which is invoked
  50. /// for each response part (or error) received. Any call to `send(_:promise:)` prior to calling
  51. /// `invoke` will fail and not be sent. Users are also responsible for closing the request stream
  52. /// by sending the `.end` request part.
  53. ///
  54. /// - Parameters:
  55. /// - path: The path of the RPC, e.g. "/echo.Echo/get".
  56. /// - type: The type of the RPC, e.g. `.unary`.
  57. /// - callOptions: Options for the RPC.
  58. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  59. func makeCall<Request: GRPCPayload, Response: GRPCPayload>(
  60. path: String,
  61. type: GRPCCallType,
  62. callOptions: CallOptions,
  63. interceptors: [ClientInterceptor<Request, Response>]
  64. ) -> Call<Request, Response>
  65. /// Close the channel, and any connections associated with it. Any ongoing RPCs may fail.
  66. ///
  67. /// - Returns: Returns a future which will be resolved when shutdown has completed.
  68. func close() -> EventLoopFuture<Void>
  69. /// Close the channel, and any connections associated with it. Any ongoing RPCs may fail.
  70. ///
  71. /// - Parameter promise: A promise which will be completed when shutdown has completed.
  72. func close(promise: EventLoopPromise<Void>)
  73. /// Attempt to gracefully shutdown the channel. New RPCs will be failed immediately and existing
  74. /// RPCs may continue to run until they complete.
  75. ///
  76. /// - Parameters:
  77. /// - deadline: A point in time by which the graceful shutdown must have completed. If the
  78. /// deadline passes and RPCs are still active then the connection will be closed forcefully
  79. /// and any remaining in-flight RPCs may be failed.
  80. /// - promise: A promise which will be completed when shutdown has completed.
  81. func closeGracefully(deadline: NIODeadline, promise: EventLoopPromise<Void>)
  82. }
  83. // Default implementations to avoid breaking API. Implementations provided by GRPC override these.
  84. extension GRPCChannel {
  85. public func close(promise: EventLoopPromise<Void>) {
  86. promise.completeWith(self.close())
  87. }
  88. public func closeGracefully(deadline: NIODeadline, promise: EventLoopPromise<Void>) {
  89. promise.completeWith(self.close())
  90. }
  91. }
  92. extension GRPCChannel {
  93. /// Make a unary gRPC call.
  94. ///
  95. /// - Parameters:
  96. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  97. /// - request: The request to send.
  98. /// - callOptions: Options for the RPC.
  99. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  100. public func makeUnaryCall<Request: Message, Response: Message>(
  101. path: String,
  102. request: Request,
  103. callOptions: CallOptions,
  104. interceptors: [ClientInterceptor<Request, Response>] = []
  105. ) -> UnaryCall<Request, Response> {
  106. let unary: UnaryCall<Request, Response> = UnaryCall(
  107. call: self.makeCall(
  108. path: path,
  109. type: .unary,
  110. callOptions: callOptions,
  111. interceptors: interceptors
  112. )
  113. )
  114. unary.invoke(request)
  115. return unary
  116. }
  117. /// Make a unary gRPC call.
  118. ///
  119. /// - Parameters:
  120. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  121. /// - request: The request to send.
  122. /// - callOptions: Options for the RPC.
  123. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  124. public func makeUnaryCall<Request: GRPCPayload, Response: GRPCPayload>(
  125. path: String,
  126. request: Request,
  127. callOptions: CallOptions,
  128. interceptors: [ClientInterceptor<Request, Response>] = []
  129. ) -> UnaryCall<Request, Response> {
  130. let rpc: UnaryCall<Request, Response> = UnaryCall(
  131. call: self.makeCall(
  132. path: path,
  133. type: .unary,
  134. callOptions: callOptions,
  135. interceptors: interceptors
  136. )
  137. )
  138. rpc.invoke(request)
  139. return rpc
  140. }
  141. /// Makes a client-streaming gRPC call.
  142. ///
  143. /// - Parameters:
  144. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  145. /// - callOptions: Options for the RPC.
  146. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  147. public func makeClientStreamingCall<Request: Message, Response: Message>(
  148. path: String,
  149. callOptions: CallOptions,
  150. interceptors: [ClientInterceptor<Request, Response>] = []
  151. ) -> ClientStreamingCall<Request, Response> {
  152. let rpc: ClientStreamingCall<Request, Response> = ClientStreamingCall(
  153. call: self.makeCall(
  154. path: path,
  155. type: .clientStreaming,
  156. callOptions: callOptions,
  157. interceptors: interceptors
  158. )
  159. )
  160. rpc.invoke()
  161. return rpc
  162. }
  163. /// Makes a client-streaming gRPC call.
  164. ///
  165. /// - Parameters:
  166. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  167. /// - callOptions: Options for the RPC.
  168. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  169. public func makeClientStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  170. path: String,
  171. callOptions: CallOptions,
  172. interceptors: [ClientInterceptor<Request, Response>] = []
  173. ) -> ClientStreamingCall<Request, Response> {
  174. let rpc: ClientStreamingCall<Request, Response> = ClientStreamingCall(
  175. call: self.makeCall(
  176. path: path,
  177. type: .clientStreaming,
  178. callOptions: callOptions,
  179. interceptors: interceptors
  180. )
  181. )
  182. rpc.invoke()
  183. return rpc
  184. }
  185. /// Make a server-streaming gRPC call.
  186. ///
  187. /// - Parameters:
  188. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  189. /// - request: The request to send.
  190. /// - callOptions: Options for the RPC.
  191. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  192. /// - handler: Response handler; called for every response received from the server.
  193. public func makeServerStreamingCall<Request: Message, Response: Message>(
  194. path: String,
  195. request: Request,
  196. callOptions: CallOptions,
  197. interceptors: [ClientInterceptor<Request, Response>] = [],
  198. handler: @escaping (Response) -> Void
  199. ) -> ServerStreamingCall<Request, Response> {
  200. let rpc: ServerStreamingCall<Request, Response> = ServerStreamingCall(
  201. call: self.makeCall(
  202. path: path,
  203. type: .serverStreaming,
  204. callOptions: callOptions,
  205. interceptors: interceptors
  206. ),
  207. callback: handler
  208. )
  209. rpc.invoke(request)
  210. return rpc
  211. }
  212. /// Make a server-streaming gRPC call.
  213. ///
  214. /// - Parameters:
  215. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  216. /// - request: The request to send.
  217. /// - callOptions: Options for the RPC.
  218. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  219. /// - handler: Response handler; called for every response received from the server.
  220. public func makeServerStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  221. path: String,
  222. request: Request,
  223. callOptions: CallOptions,
  224. interceptors: [ClientInterceptor<Request, Response>] = [],
  225. handler: @escaping (Response) -> Void
  226. ) -> ServerStreamingCall<Request, Response> {
  227. let rpc: ServerStreamingCall<Request, Response> = ServerStreamingCall(
  228. call: self.makeCall(
  229. path: path,
  230. type: .serverStreaming,
  231. callOptions: callOptions,
  232. interceptors: []
  233. ),
  234. callback: handler
  235. )
  236. rpc.invoke(request)
  237. return rpc
  238. }
  239. /// Makes a bidirectional-streaming gRPC call.
  240. ///
  241. /// - Parameters:
  242. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  243. /// - callOptions: Options for the RPC.
  244. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  245. /// - handler: Response handler; called for every response received from the server.
  246. public func makeBidirectionalStreamingCall<Request: Message, Response: Message>(
  247. path: String,
  248. callOptions: CallOptions,
  249. interceptors: [ClientInterceptor<Request, Response>] = [],
  250. handler: @escaping (Response) -> Void
  251. ) -> BidirectionalStreamingCall<Request, Response> {
  252. let rpc: BidirectionalStreamingCall<Request, Response> = BidirectionalStreamingCall(
  253. call: self.makeCall(
  254. path: path,
  255. type: .bidirectionalStreaming,
  256. callOptions: callOptions,
  257. interceptors: interceptors
  258. ),
  259. callback: handler
  260. )
  261. rpc.invoke()
  262. return rpc
  263. }
  264. /// Makes a bidirectional-streaming gRPC call.
  265. ///
  266. /// - Parameters:
  267. /// - path: Path of the RPC, e.g. "/echo.Echo/Get"
  268. /// - callOptions: Options for the RPC.
  269. /// - interceptors: A list of interceptors to intercept the request and response stream with.
  270. /// - handler: Response handler; called for every response received from the server.
  271. public func makeBidirectionalStreamingCall<Request: GRPCPayload, Response: GRPCPayload>(
  272. path: String,
  273. callOptions: CallOptions,
  274. interceptors: [ClientInterceptor<Request, Response>] = [],
  275. handler: @escaping (Response) -> Void
  276. ) -> BidirectionalStreamingCall<Request, Response> {
  277. let rpc: BidirectionalStreamingCall<Request, Response> = BidirectionalStreamingCall(
  278. call: self.makeCall(
  279. path: path,
  280. type: .bidirectionalStreaming,
  281. callOptions: callOptions,
  282. interceptors: interceptors
  283. ),
  284. callback: handler
  285. )
  286. rpc.invoke()
  287. return rpc
  288. }
  289. }