ClientCallTransport.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 NIO
  17. import NIOHTTP2
  18. import NIOHPACK
  19. import Logging
  20. /// This class provides much of the boilerplate for the four types of gRPC call objects returned to
  21. /// framework users. It is the glue between a call object and the underlying transport (typically a
  22. /// NIO Channel).
  23. ///
  24. /// Typically, each call will be configured on an HTTP/2 stream channel. The stream channel will
  25. /// will be configured as such:
  26. ///
  27. /// ```
  28. /// ┌────────────────────────────────────┐
  29. /// │ ChannelTransport<Request,Response> │
  30. /// └─────▲───────────────────────┬──────┘
  31. /// │ │
  32. /// --------------------------------│-----------------------│------------------------------
  33. /// HTTP2StreamChannel │ │
  34. /// ┌────────────┴──────────┐ │
  35. /// │ GRPCClientCallHandler │ │
  36. /// └────────────▲──────────┘ │
  37. /// GRPCClientResponsePart<Response>│ │GRPCClientRequestPart<Request>
  38. /// ┌─┴───────────────────────▼─┐
  39. /// │ GRPCClientChannelHandler │
  40. /// └─▲───────────────────────┬─┘
  41. /// HTTP2Frame│ │HTTP2Frame
  42. /// | |
  43. ///```
  44. ///
  45. /// Note: the "main" pipeline provided by the channel in `ClientConnection`.
  46. internal class ChannelTransport<Request: GRPCPayload, Response: GRPCPayload> {
  47. internal typealias RequestPart = _GRPCClientRequestPart<Request>
  48. internal typealias ResponsePart = _GRPCClientResponsePart<Response>
  49. /// The `EventLoop` this call is running on.
  50. internal let eventLoop: EventLoop
  51. /// A logger.
  52. private let logger: Logger
  53. /// The current state of the call.
  54. private var state: State
  55. /// A scheduled timeout for the call.
  56. private var scheduledTimeout: Scheduled<Void>?
  57. // Note: initial capacity is 4 because it's a power of 2 and most calls are unary so will
  58. // have 3 parts.
  59. /// A buffer to store requests in before the channel has become active.
  60. private var requestBuffer = MarkedCircularBuffer<BufferedRequest>(initialCapacity: 4)
  61. /// A request that we'll deal with at a later point in time.
  62. private struct BufferedRequest {
  63. /// The request to write.
  64. var message: _GRPCClientRequestPart<Request>
  65. /// Any promise associated with the request.
  66. var promise: EventLoopPromise<Void>?
  67. }
  68. /// An error delegate provided by the user.
  69. private var errorDelegate: ClientErrorDelegate?
  70. /// A container for response part promises for the call.
  71. internal var responseContainer: ResponsePartContainer<Response>
  72. /// A stopwatch for timing the RPC.
  73. private var stopwatch: Stopwatch?
  74. enum State {
  75. // Waiting for a stream to become active.
  76. //
  77. // Valid transitions:
  78. // - active
  79. // - closed
  80. case buffering(EventLoopFuture<Channel>)
  81. // We have a channel, we're doing the RPC, there may be a timeout.
  82. //
  83. // Valid transitions:
  84. // - closed
  85. case active(Channel)
  86. // We're closed; the RPC is done for one reason or another. This is terminal.
  87. case closed
  88. }
  89. private init(
  90. eventLoop: EventLoop,
  91. state: State,
  92. responseContainer: ResponsePartContainer<Response>,
  93. errorDelegate: ClientErrorDelegate?,
  94. logger: Logger
  95. ) {
  96. self.eventLoop = eventLoop
  97. self.state = state
  98. self.responseContainer = responseContainer
  99. self.errorDelegate = errorDelegate
  100. self.logger = logger
  101. self.startTimer()
  102. }
  103. internal convenience init(
  104. eventLoop: EventLoop,
  105. responseContainer: ResponsePartContainer<Response>,
  106. timeLimit: TimeLimit,
  107. errorDelegate: ClientErrorDelegate?,
  108. logger: Logger,
  109. channelProvider: (ChannelTransport<Request, Response>, EventLoopPromise<Channel>) -> ()
  110. ) {
  111. let channelPromise = eventLoop.makePromise(of: Channel.self)
  112. self.init(
  113. eventLoop: eventLoop,
  114. state: .buffering(channelPromise.futureResult),
  115. responseContainer: responseContainer,
  116. errorDelegate: errorDelegate,
  117. logger: logger
  118. )
  119. // If the channel creation fails we need to error the call. Note that we receive an
  120. // 'activation' from the channel instead of relying on the success of the future.
  121. channelPromise.futureResult.whenFailure { error in
  122. self.handleError(error, promise: nil)
  123. }
  124. // Schedule the timeout.
  125. let deadline = timeLimit.makeDeadline()
  126. if deadline != .distantFuture {
  127. self.scheduledTimeout = eventLoop.scheduleTask(deadline: deadline) {
  128. self.timedOut(after: timeLimit)
  129. }
  130. }
  131. // Now attempt to make the channel.
  132. channelProvider(self, channelPromise)
  133. }
  134. internal convenience init(
  135. multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>,
  136. responseContainer: ResponsePartContainer<Response>,
  137. callType: GRPCCallType,
  138. timeLimit: TimeLimit,
  139. errorDelegate: ClientErrorDelegate?,
  140. logger: Logger
  141. ) {
  142. self.init(
  143. eventLoop: multiplexer.eventLoop,
  144. responseContainer: responseContainer,
  145. timeLimit: timeLimit,
  146. errorDelegate: errorDelegate,
  147. logger: logger
  148. ) { call, streamPromise in
  149. multiplexer.whenComplete { result in
  150. switch result {
  151. case .success(let mux):
  152. mux.createStreamChannel(promise: streamPromise) { stream, streamID in
  153. stream.pipeline.addHandlers([
  154. _GRPCClientChannelHandler<Request, Response>(streamID: streamID, callType: callType, logger: logger),
  155. GRPCClientCallHandler(call: call)
  156. ])
  157. }
  158. case .failure(let error):
  159. streamPromise.fail(error)
  160. }
  161. }
  162. }
  163. }
  164. }
  165. // MARK: - Call API (i.e. called from {Unary,ClientStreaming,...}Call)
  166. extension ChannelTransport: ClientCallOutbound {
  167. /// Send a request part.
  168. ///
  169. /// Does not have to be called from the event loop.
  170. internal func sendRequest(_ part: RequestPart, promise: EventLoopPromise<Void>?) {
  171. if self.eventLoop.inEventLoop {
  172. self.writePart(part, flush: true, promise: promise)
  173. } else {
  174. self.eventLoop.execute {
  175. self.writePart(part, flush: true, promise: promise)
  176. }
  177. }
  178. }
  179. /// Send multiple request parts.
  180. ///
  181. /// Does not have to be called from the event loop.
  182. internal func sendRequests<S>(
  183. _ parts: S,
  184. promise: EventLoopPromise<Void>?
  185. ) where S: Sequence, S.Element == RequestPart {
  186. if self.eventLoop.inEventLoop {
  187. self._sendRequests(parts, promise: promise)
  188. } else {
  189. self.eventLoop.execute {
  190. self._sendRequests(parts, promise: promise)
  191. }
  192. }
  193. }
  194. /// Request that the RPC is cancelled.
  195. ///
  196. /// Does not have to be called from the event loop.
  197. internal func cancel(promise: EventLoopPromise<Void>?) {
  198. self.logger.info("rpc cancellation requested")
  199. if self.eventLoop.inEventLoop {
  200. self.handleError(GRPCError.RPCCancelledByClient().captureContext(), promise: promise)
  201. } else {
  202. self.eventLoop.execute {
  203. self.handleError(GRPCError.RPCCancelledByClient().captureContext(), promise: promise)
  204. }
  205. }
  206. }
  207. /// Returns the `Channel` for the HTTP/2 stream that this RPC is using.
  208. internal func streamChannel() -> EventLoopFuture<Channel> {
  209. if self.eventLoop.inEventLoop {
  210. return self.getStreamChannel()
  211. } else {
  212. return self.eventLoop.flatSubmit {
  213. self.getStreamChannel()
  214. }
  215. }
  216. }
  217. }
  218. extension ChannelTransport {
  219. /// Return a future for the stream channel.
  220. ///
  221. /// Must be called from the event loop.
  222. private func getStreamChannel() -> EventLoopFuture<Channel> {
  223. self.eventLoop.preconditionInEventLoop()
  224. switch self.state {
  225. case .buffering(let future):
  226. return future
  227. case .active(let channel):
  228. return self.eventLoop.makeSucceededFuture(channel)
  229. case .closed:
  230. return self.eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
  231. }
  232. }
  233. /// Send many requests.
  234. ///
  235. /// Must be called from the event loop.
  236. private func _sendRequests<S>(
  237. _ parts: S,
  238. promise: EventLoopPromise<Void>?
  239. ) where S: Sequence, S.Element == RequestPart {
  240. self.eventLoop.preconditionInEventLoop()
  241. // We have a promise: create one for each request part and cascade the overall result to it.
  242. // If we're flushing we'll do it at the end.
  243. if let promise = promise {
  244. let loop = promise.futureResult.eventLoop
  245. let futures: [EventLoopFuture<Void>] = parts.map { part in
  246. let partPromise = loop.makePromise(of: Void.self)
  247. self.writePart(part, flush: false, promise: partPromise)
  248. return partPromise.futureResult
  249. }
  250. // Cascade the futures to the provided promise.
  251. EventLoopFuture.andAllSucceed(futures, on: loop).cascade(to: promise)
  252. } else {
  253. for part in parts {
  254. self.writePart(part, flush: false, promise: nil)
  255. }
  256. }
  257. // Now flush.
  258. self.flush()
  259. }
  260. /// Buffer or send a flush.
  261. ///
  262. /// Must be called from the event loop.
  263. private func flush() {
  264. self.eventLoop.preconditionInEventLoop()
  265. switch self.state {
  266. case .buffering:
  267. self.requestBuffer.mark()
  268. case .active(let stream):
  269. stream.flush()
  270. case .closed:
  271. ()
  272. }
  273. }
  274. /// Write a request part.
  275. ///
  276. /// Must be called from the event loop.
  277. ///
  278. /// - Parameters:
  279. /// - part: The part to write.
  280. /// - flush: Whether we should flush the channel after this write.
  281. /// - promise: A promise to fulfill when the part has been written.
  282. private func writePart(_ part: RequestPart, flush: Bool, promise: EventLoopPromise<Void>?) {
  283. self.eventLoop.assertInEventLoop()
  284. switch self.state {
  285. // We're buffering, so buffer the message.
  286. case .buffering:
  287. self.logger.debug("buffering request part", metadata: [
  288. "request_part": "\(part.name)",
  289. "call_state": "\(self.describeCallState())"
  290. ])
  291. self.requestBuffer.append(BufferedRequest(message: part, promise: promise))
  292. if flush {
  293. self.requestBuffer.mark()
  294. }
  295. // We have an active stream, just pass the write and promise through.
  296. case .active(let stream):
  297. self.logger.debug("writing request part", metadata: ["request_part": "\(part.name)"])
  298. stream.write(part, promise: promise)
  299. if flush {
  300. stream.flush()
  301. }
  302. // We're closed: drop the request.
  303. case .closed:
  304. self.logger.debug("dropping request part", metadata: [
  305. "request_part": "\(part.name)",
  306. "call_state": "\(self.describeCallState())"
  307. ])
  308. promise?.fail(ChannelError.ioOnClosedChannel)
  309. }
  310. }
  311. /// The scheduled timeout triggered: timeout the RPC if it's not yet finished.
  312. ///
  313. /// Must be called from the event loop.
  314. private func timedOut(after timeLimit: TimeLimit) {
  315. self.eventLoop.preconditionInEventLoop()
  316. let error = GRPCError.RPCTimedOut(timeLimit).captureContext()
  317. self.handleError(error, promise: nil)
  318. }
  319. /// Handle an error and optionally fail the provided promise with the error.
  320. ///
  321. /// Must be called from the event loop.
  322. private func handleError(_ error: Error, promise: EventLoopPromise<Void>?) {
  323. self.eventLoop.preconditionInEventLoop()
  324. switch self.state {
  325. // We only care about errors if we're not shutdown yet.
  326. case .buffering, .active:
  327. // Add our current state to the logger we provide to the callback.
  328. var loggerWithState = self.logger
  329. loggerWithState[metadataKey: "call_state"] = "\(self.describeCallState())"
  330. let errorStatus: GRPCStatus
  331. if let errorWithContext = error as? GRPCError.WithContext {
  332. errorStatus = errorWithContext.error.makeGRPCStatus()
  333. self.errorDelegate?.didCatchError(
  334. errorWithContext.error,
  335. logger: loggerWithState,
  336. file: errorWithContext.file,
  337. line: errorWithContext.line
  338. )
  339. } else if let transformable = error as? GRPCStatusTransformable {
  340. errorStatus = transformable.makeGRPCStatus()
  341. self.errorDelegate?.didCatchErrorWithoutContext(error, logger: loggerWithState)
  342. } else {
  343. errorStatus = .processingError
  344. self.errorDelegate?.didCatchErrorWithoutContext(error, logger: loggerWithState)
  345. }
  346. // Update our state: we're closing.
  347. self.close(withStatus: errorStatus)
  348. promise?.fail(errorStatus)
  349. case .closed:
  350. promise?.fail(ChannelError.alreadyClosed)
  351. }
  352. }
  353. /// Close the call, if it's not yet closed with the given status.
  354. ///
  355. /// Must be called from the event loop.
  356. private func close(withStatus status: GRPCStatus) {
  357. self.eventLoop.preconditionInEventLoop()
  358. switch self.state {
  359. case .buffering(let streamFuture):
  360. // We're closed now.
  361. self.state = .closed
  362. self.stopTimer(status: status)
  363. // We're done; cancel the timeout.
  364. self.scheduledTimeout?.cancel()
  365. self.scheduledTimeout = nil
  366. // Fail any outstanding promises.
  367. self.responseContainer.fail(with: status)
  368. // Fail any buffered writes.
  369. while !self.requestBuffer.isEmpty {
  370. let write = self.requestBuffer.removeFirst()
  371. write.promise?.fail(status)
  372. }
  373. // Close the channel, if it comes up.
  374. streamFuture.whenSuccess {
  375. $0.close(mode: .all, promise: nil)
  376. }
  377. case .active(let channel):
  378. // We're closed now.
  379. self.state = .closed
  380. self.stopTimer(status: status)
  381. // We're done; cancel the timeout.
  382. self.scheduledTimeout?.cancel()
  383. self.scheduledTimeout = nil
  384. // Fail any outstanding promises.
  385. self.responseContainer.fail(with: status)
  386. // Close the channel.
  387. channel.close(mode: .all, promise: nil)
  388. case .closed:
  389. ()
  390. }
  391. }
  392. }
  393. // MARK: - Channel Inbound
  394. extension ChannelTransport: ClientCallInbound {
  395. /// Receive an error from the Channel.
  396. ///
  397. /// Must be called on the event loop.
  398. internal func receiveError(_ error: Error) {
  399. self.eventLoop.preconditionInEventLoop()
  400. self.handleError(error, promise: nil)
  401. }
  402. /// Receive a response part from the Channel.
  403. ///
  404. /// Must be called on the event loop.
  405. func receiveResponse(_ part: _GRPCClientResponsePart<Response>) {
  406. self.eventLoop.preconditionInEventLoop()
  407. switch self.state {
  408. case .buffering:
  409. preconditionFailure("Received response part in 'buffering' state")
  410. case .active:
  411. self.logger.debug("received response part", metadata: ["response_part": "\(part.name)"])
  412. switch part {
  413. case .initialMetadata(let metadata):
  414. self.responseContainer.lazyInitialMetadataPromise.completeWith(.success(metadata))
  415. case .message(let messageContext):
  416. switch self.responseContainer.responseHandler {
  417. case .unary(let responsePromise):
  418. responsePromise.succeed(messageContext.message)
  419. case .stream(let handler):
  420. handler(messageContext.message)
  421. }
  422. case .trailingMetadata(let metadata):
  423. self.responseContainer.lazyTrailingMetadataPromise.succeed(metadata)
  424. case .status(let status):
  425. // We're done; cancel the timeout.
  426. self.scheduledTimeout?.cancel()
  427. self.scheduledTimeout = nil
  428. // We're closed now.
  429. self.state = .closed
  430. self.stopTimer(status: status)
  431. // We're not really failing the status here; in some cases the server may fast fail, in which
  432. // case we'll only see trailing metadata and status: we should fail the initial metadata and
  433. // response in that case.
  434. self.responseContainer.fail(with: status)
  435. }
  436. case .closed:
  437. self.logger.debug("dropping response part", metadata: [
  438. "response_part": "\(part.name)",
  439. "call_state": "\(self.describeCallState())"
  440. ])
  441. }
  442. }
  443. /// The underlying channel become active and can start accepting writes.
  444. ///
  445. /// Must be called on the event loop.
  446. internal func activate(stream: Channel) {
  447. self.eventLoop.preconditionInEventLoop()
  448. // The channel has become active: what now?
  449. switch self.state {
  450. case .buffering:
  451. while !self.requestBuffer.isEmpty {
  452. // Are we marked?
  453. let hadMark = self.requestBuffer.hasMark
  454. let request = self.requestBuffer.removeFirst()
  455. // We became unmarked: we need to flush.
  456. let shouldFlush = hadMark && !self.requestBuffer.hasMark
  457. self.logger.debug("unbuffering request part", metadata: ["request_part": "\(request.message.name)"])
  458. stream.write(request.message, promise: request.promise)
  459. if shouldFlush {
  460. stream.flush()
  461. }
  462. }
  463. self.logger.debug("request buffer drained")
  464. self.state = .active(stream)
  465. case .active:
  466. preconditionFailure("Invalid state: stream is already active")
  467. case .closed:
  468. // The channel became active but we're already closed: we must've timed out waiting for the
  469. // channel to activate so close the channel now.
  470. stream.close(mode: .all, promise: nil)
  471. }
  472. }
  473. }
  474. // MARK: Private Helpers
  475. extension ChannelTransport {
  476. private func describeCallState() -> String {
  477. self.eventLoop.preconditionInEventLoop()
  478. switch self.state {
  479. case .buffering:
  480. return "waiting for connection; \(self.requestBuffer.count) request part(s) buffered"
  481. case .active:
  482. return "active"
  483. case .closed:
  484. return "closed"
  485. }
  486. }
  487. private func startTimer() {
  488. assert(self.stopwatch == nil)
  489. self.stopwatch = Stopwatch()
  490. self.logger.debug("starting rpc")
  491. }
  492. private func stopTimer(status: GRPCStatus) {
  493. self.eventLoop.preconditionInEventLoop()
  494. if let stopwatch = self.stopwatch {
  495. let millis = stopwatch.elapsedMillis()
  496. self.logger.debug("rpc call finished", metadata: [
  497. "duration_ms": "\(millis)",
  498. "status_code": "\(status.code.rawValue)",
  499. "status_message": "\(status.message ?? "nil")"
  500. ])
  501. self.stopwatch = nil
  502. }
  503. }
  504. }
  505. extension _GRPCClientRequestPart {
  506. fileprivate var name: String {
  507. switch self {
  508. case .head:
  509. return "head"
  510. case .message:
  511. return "message"
  512. case .end:
  513. return "end"
  514. }
  515. }
  516. }
  517. extension _GRPCClientResponsePart {
  518. fileprivate var name: String {
  519. switch self {
  520. case .initialMetadata:
  521. return "initial metadata"
  522. case .message:
  523. return "message"
  524. case .trailingMetadata:
  525. return "trailing metadata"
  526. case .status:
  527. return "status"
  528. }
  529. }
  530. }