ClientConnectionHandler.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Copyright 2024, 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. /// An event which happens on a client's HTTP/2 connection.
  19. @_spi(Package)
  20. public enum ClientConnectionEvent: Sendable {
  21. public enum CloseReason: Sendable {
  22. /// The server sent a GOAWAY frame to the client.
  23. case goAway(HTTP2ErrorCode, String)
  24. /// The keep alive timer fired and subsequently timed out.
  25. case keepaliveExpired
  26. /// The connection became idle.
  27. case idle
  28. /// The local peer initiated the close.
  29. case initiatedLocally
  30. /// The connection was closed unexpectedly
  31. case unexpected((any Error)?, isIdle: Bool)
  32. }
  33. /// The connection is now ready.
  34. case ready
  35. /// The connection has started shutting down, no new streams should be created.
  36. case closing(CloseReason)
  37. }
  38. /// A `ChannelHandler` which manages part of the lifecycle of a gRPC connection over HTTP/2.
  39. ///
  40. /// This handler is responsible for managing several aspects of the connection. These include:
  41. /// 1. Periodically sending keep alive pings to the server (if configured) and closing the
  42. /// connection if necessary.
  43. /// 2. Closing the connection if it is idle (has no open streams) for a configured amount of time.
  44. /// 3. Forwarding lifecycle events to the next handler.
  45. ///
  46. /// Some of the behaviours are described in [gRFC A8](https://github.com/grpc/proposal/blob/master/A8-client-side-keepalive.md).
  47. final class ClientConnectionHandler: ChannelInboundHandler, ChannelOutboundHandler {
  48. typealias InboundIn = HTTP2Frame
  49. typealias InboundOut = ClientConnectionEvent
  50. typealias OutboundIn = Never
  51. typealias OutboundOut = HTTP2Frame
  52. enum OutboundEvent: Hashable, Sendable {
  53. /// Close the connection gracefully
  54. case closeGracefully
  55. }
  56. /// The `EventLoop` of the `Channel` this handler exists in.
  57. private let eventLoop: any EventLoop
  58. /// The maximum amount of time the connection may be idle for. If the connection remains idle
  59. /// (i.e. has no open streams) for this period of time then the connection will be gracefully
  60. /// closed.
  61. private var maxIdleTimer: Timer?
  62. /// The amount of time to wait before sending a keep alive ping.
  63. private var keepaliveTimer: Timer?
  64. /// The amount of time the client has to reply after sending a keep alive ping. Only used if
  65. /// `keepaliveTimer` is set.
  66. private var keepaliveTimeoutTimer: Timer
  67. /// Opaque data sent in keep alive pings.
  68. private let keepalivePingData: HTTP2PingData
  69. /// The current state of the connection.
  70. private var state: StateMachine
  71. /// Whether a flush is pending.
  72. private var flushPending: Bool
  73. /// Whether `channelRead` has been called and `channelReadComplete` hasn't yet been called.
  74. /// Resets once `channelReadComplete` returns.
  75. private var inReadLoop: Bool
  76. /// The context of the channel this handler is in.
  77. private var context: ChannelHandlerContext?
  78. /// Creates a new handler which manages the lifecycle of a connection.
  79. ///
  80. /// - Parameters:
  81. /// - eventLoop: The `EventLoop` of the `Channel` this handler is placed in.
  82. /// - maxIdleTime: The maximum amount time a connection may be idle for before being closed.
  83. /// - keepaliveTime: The amount of time to wait after reading data before sending a keep-alive
  84. /// ping.
  85. /// - keepaliveTimeout: The amount of time the client has to reply after the server sends a
  86. /// keep-alive ping to keep the connection open. The connection is closed if no reply
  87. /// is received.
  88. /// - keepaliveWithoutCalls: Whether the client sends keep-alive pings when there are no calls
  89. /// in progress.
  90. init(
  91. eventLoop: any EventLoop,
  92. maxIdleTime: TimeAmount?,
  93. keepaliveTime: TimeAmount?,
  94. keepaliveTimeout: TimeAmount?,
  95. keepaliveWithoutCalls: Bool
  96. ) {
  97. self.eventLoop = eventLoop
  98. self.maxIdleTimer = maxIdleTime.map { Timer(delay: $0) }
  99. self.keepaliveTimer = keepaliveTime.map { Timer(delay: $0, repeat: true) }
  100. self.keepaliveTimeoutTimer = Timer(delay: keepaliveTimeout ?? .seconds(20))
  101. self.keepalivePingData = HTTP2PingData(withInteger: .random(in: .min ... .max))
  102. self.state = StateMachine(allowKeepaliveWithoutCalls: keepaliveWithoutCalls)
  103. self.flushPending = false
  104. self.inReadLoop = false
  105. }
  106. func handlerAdded(context: ChannelHandlerContext) {
  107. assert(context.eventLoop === self.eventLoop)
  108. self.context = context
  109. }
  110. func handlerRemoved(context: ChannelHandlerContext) {
  111. self.context = nil
  112. }
  113. func channelInactive(context: ChannelHandlerContext) {
  114. switch self.state.closed() {
  115. case .none:
  116. ()
  117. case .unexpectedClose(let error, let isIdle):
  118. let event = self.wrapInboundOut(.closing(.unexpected(error, isIdle: isIdle)))
  119. context.fireChannelRead(event)
  120. case .succeed(let promise):
  121. promise.succeed()
  122. }
  123. self.keepaliveTimer?.cancel()
  124. self.keepaliveTimeoutTimer.cancel()
  125. }
  126. func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  127. switch event {
  128. case let event as NIOHTTP2StreamCreatedEvent:
  129. self._streamCreated(event.streamID, channel: context.channel)
  130. case let event as StreamClosedEvent:
  131. self._streamClosed(event.streamID, channel: context.channel)
  132. default:
  133. ()
  134. }
  135. context.fireUserInboundEventTriggered(event)
  136. }
  137. func errorCaught(context: ChannelHandlerContext, error: any Error) {
  138. // Store the error and close, this will result in the final close event being fired down
  139. // the pipeline with an appropriate close reason and appropriate error. (This avoids
  140. // the async channel just throwing the error.)
  141. self.state.receivedError(error)
  142. context.close(mode: .all, promise: nil)
  143. }
  144. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  145. let frame = self.unwrapInboundIn(data)
  146. self.inReadLoop = true
  147. switch frame.payload {
  148. case .goAway(_, let errorCode, let data):
  149. // Receiving a GOAWAY frame means we need to stop creating streams immediately and start
  150. // closing the connection.
  151. switch self.state.beginGracefulShutdown(promise: nil) {
  152. case .sendGoAway(let close):
  153. // gRPC servers may indicate why the GOAWAY was sent in the opaque data.
  154. let message = data.map { String(buffer: $0) } ?? ""
  155. context.fireChannelRead(self.wrapInboundOut(.closing(.goAway(errorCode, message))))
  156. // Clients should send GOAWAYs when closing a connection.
  157. self.writeAndFlushGoAway(context: context, errorCode: .noError)
  158. if close {
  159. context.close(promise: nil)
  160. }
  161. case .none:
  162. ()
  163. }
  164. case .ping(let data, let ack):
  165. // Pings are ack'd by the HTTP/2 handler so we only pay attention to acks here, and in
  166. // particular only those carrying the keep-alive data.
  167. if ack, data == self.keepalivePingData {
  168. let loopBound = LoopBoundView(handler: self, context: context)
  169. self.keepaliveTimeoutTimer.cancel()
  170. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  171. loopBound.keepaliveTimerFired()
  172. }
  173. }
  174. case .settings(.settings(_)):
  175. let isInitialSettings = self.state.receivedSettings()
  176. // The first settings frame indicates that the connection is now ready to use. The channel
  177. // becoming active is insufficient as, for example, a TLS handshake may fail after
  178. // establishing the TCP connection, or the server isn't configured for gRPC (or HTTP/2).
  179. if isInitialSettings {
  180. let loopBound = LoopBoundView(handler: self, context: context)
  181. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  182. loopBound.keepaliveTimerFired()
  183. }
  184. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  185. loopBound.maxIdleTimerFired()
  186. }
  187. context.fireChannelRead(self.wrapInboundOut(.ready))
  188. }
  189. default:
  190. ()
  191. }
  192. }
  193. func channelReadComplete(context: ChannelHandlerContext) {
  194. while self.flushPending {
  195. self.flushPending = false
  196. context.flush()
  197. }
  198. self.inReadLoop = false
  199. context.fireChannelReadComplete()
  200. }
  201. func triggerUserOutboundEvent(
  202. context: ChannelHandlerContext,
  203. event: Any,
  204. promise: EventLoopPromise<Void>?
  205. ) {
  206. if let event = event as? OutboundEvent {
  207. switch event {
  208. case .closeGracefully:
  209. switch self.state.beginGracefulShutdown(promise: promise) {
  210. case .sendGoAway(let close):
  211. context.fireChannelRead(self.wrapInboundOut(.closing(.initiatedLocally)))
  212. // The client could send a GOAWAY at this point but it's not really necessary, the server
  213. // can't open streams anyway, the client will just close the connection when it's done.
  214. if close {
  215. context.close(promise: nil)
  216. }
  217. case .none:
  218. ()
  219. }
  220. }
  221. } else {
  222. context.triggerUserOutboundEvent(event, promise: promise)
  223. }
  224. }
  225. }
  226. extension ClientConnectionHandler {
  227. struct LoopBoundView: @unchecked Sendable {
  228. private let handler: ClientConnectionHandler
  229. private let context: ChannelHandlerContext
  230. init(handler: ClientConnectionHandler, context: ChannelHandlerContext) {
  231. self.handler = handler
  232. self.context = context
  233. }
  234. func keepaliveTimerFired() {
  235. self.context.eventLoop.assertInEventLoop()
  236. self.handler.keepaliveTimerFired(context: self.context)
  237. }
  238. func keepaliveTimeoutExpired() {
  239. self.context.eventLoop.assertInEventLoop()
  240. self.handler.keepaliveTimeoutExpired(context: self.context)
  241. }
  242. func maxIdleTimerFired() {
  243. self.context.eventLoop.assertInEventLoop()
  244. self.handler.maxIdleTimerFired(context: self.context)
  245. }
  246. }
  247. }
  248. extension ClientConnectionHandler {
  249. struct HTTP2StreamDelegate: @unchecked Sendable, NIOHTTP2StreamDelegate {
  250. // @unchecked is okay: the only methods do the appropriate event-loop dance.
  251. private let handler: ClientConnectionHandler
  252. init(_ handler: ClientConnectionHandler) {
  253. self.handler = handler
  254. }
  255. func streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  256. if self.handler.eventLoop.inEventLoop {
  257. self.handler._streamCreated(id, channel: channel)
  258. } else {
  259. self.handler.eventLoop.execute {
  260. self.handler._streamCreated(id, channel: channel)
  261. }
  262. }
  263. }
  264. func streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  265. if self.handler.eventLoop.inEventLoop {
  266. self.handler._streamClosed(id, channel: channel)
  267. } else {
  268. self.handler.eventLoop.execute {
  269. self.handler._streamClosed(id, channel: channel)
  270. }
  271. }
  272. }
  273. }
  274. var http2StreamDelegate: HTTP2StreamDelegate {
  275. return HTTP2StreamDelegate(self)
  276. }
  277. private func _streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  278. self.eventLoop.assertInEventLoop()
  279. // Stream created, so the connection isn't idle.
  280. self.maxIdleTimer?.cancel()
  281. self.state.streamOpened(id)
  282. }
  283. private func _streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  284. guard let context = self.context else { return }
  285. self.eventLoop.assertInEventLoop()
  286. switch self.state.streamClosed(id) {
  287. case .startIdleTimer(let cancelKeepalive):
  288. // All streams are closed, restart the idle timer, and stop the keep-alive timer (it may
  289. // not stop if keep-alive is allowed when there are no active calls).
  290. let loopBound = LoopBoundView(handler: self, context: context)
  291. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  292. loopBound.maxIdleTimerFired()
  293. }
  294. if cancelKeepalive {
  295. self.keepaliveTimer?.cancel()
  296. }
  297. case .close:
  298. // Connection was closing but waiting for all streams to close. They must all be closed
  299. // now so close the connection.
  300. context.close(promise: nil)
  301. case .none:
  302. ()
  303. }
  304. }
  305. }
  306. extension ClientConnectionHandler {
  307. private func maybeFlush(context: ChannelHandlerContext) {
  308. if self.inReadLoop {
  309. self.flushPending = true
  310. } else {
  311. context.flush()
  312. }
  313. }
  314. private func keepaliveTimerFired(context: ChannelHandlerContext) {
  315. guard self.state.sendKeepalivePing() else { return }
  316. // Cancel the keep alive timer when the client sends a ping. The timer is resumed when the ping
  317. // is acknowledged.
  318. self.keepaliveTimer?.cancel()
  319. let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(self.keepalivePingData, ack: false))
  320. context.write(self.wrapOutboundOut(ping), promise: nil)
  321. self.maybeFlush(context: context)
  322. // Schedule a timeout on waiting for the response.
  323. let loopBound = LoopBoundView(handler: self, context: context)
  324. self.keepaliveTimeoutTimer.schedule(on: context.eventLoop) {
  325. loopBound.keepaliveTimeoutExpired()
  326. }
  327. }
  328. private func keepaliveTimeoutExpired(context: ChannelHandlerContext) {
  329. guard self.state.beginClosing() else { return }
  330. context.fireChannelRead(self.wrapInboundOut(.closing(.keepaliveExpired)))
  331. self.writeAndFlushGoAway(context: context, message: "keepalive_expired")
  332. context.close(promise: nil)
  333. }
  334. private func maxIdleTimerFired(context: ChannelHandlerContext) {
  335. guard self.state.beginClosing() else { return }
  336. context.fireChannelRead(self.wrapInboundOut(.closing(.idle)))
  337. self.writeAndFlushGoAway(context: context, message: "idle")
  338. context.close(promise: nil)
  339. }
  340. private func writeAndFlushGoAway(
  341. context: ChannelHandlerContext,
  342. errorCode: HTTP2ErrorCode = .noError,
  343. message: String? = nil
  344. ) {
  345. let goAway = HTTP2Frame(
  346. streamID: .rootStream,
  347. payload: .goAway(
  348. lastStreamID: 0,
  349. errorCode: errorCode,
  350. opaqueData: message.map { context.channel.allocator.buffer(string: $0) }
  351. )
  352. )
  353. context.write(self.wrapOutboundOut(goAway), promise: nil)
  354. self.maybeFlush(context: context)
  355. }
  356. }
  357. extension ClientConnectionHandler {
  358. struct StateMachine {
  359. private var state: State
  360. private enum State {
  361. case active(Active)
  362. case closing(Closing)
  363. case closed
  364. struct Active {
  365. var openStreams: Set<HTTP2StreamID>
  366. var allowKeepaliveWithoutCalls: Bool
  367. var receivedConnectionPreface: Bool
  368. var error: (any Error)?
  369. init(allowKeepaliveWithoutCalls: Bool) {
  370. self.openStreams = []
  371. self.allowKeepaliveWithoutCalls = allowKeepaliveWithoutCalls
  372. self.receivedConnectionPreface = false
  373. self.error = nil
  374. }
  375. mutating func receivedSettings() -> Bool {
  376. let isFirstSettingsFrame = !self.receivedConnectionPreface
  377. self.receivedConnectionPreface = true
  378. return isFirstSettingsFrame
  379. }
  380. }
  381. struct Closing {
  382. var allowKeepaliveWithoutCalls: Bool
  383. var openStreams: Set<HTTP2StreamID>
  384. var closePromise: Optional<EventLoopPromise<Void>>
  385. init(from state: Active, closePromise: EventLoopPromise<Void>?) {
  386. self.openStreams = state.openStreams
  387. self.allowKeepaliveWithoutCalls = state.allowKeepaliveWithoutCalls
  388. self.closePromise = closePromise
  389. }
  390. }
  391. }
  392. init(allowKeepaliveWithoutCalls: Bool) {
  393. self.state = .active(State.Active(allowKeepaliveWithoutCalls: allowKeepaliveWithoutCalls))
  394. }
  395. /// Record that a SETTINGS frame was received from the remote peer.
  396. ///
  397. /// - Returns: `true` if this was the first SETTINGS frame received.
  398. mutating func receivedSettings() -> Bool {
  399. switch self.state {
  400. case .active(var active):
  401. let isFirstSettingsFrame = active.receivedSettings()
  402. self.state = .active(active)
  403. return isFirstSettingsFrame
  404. case .closing, .closed:
  405. return false
  406. }
  407. }
  408. /// Record that an error was received.
  409. mutating func receivedError(_ error: any Error) {
  410. switch self.state {
  411. case .active(var active):
  412. active.error = error
  413. self.state = .active(active)
  414. case .closing, .closed:
  415. ()
  416. }
  417. }
  418. /// Record that the stream with the given ID has been opened.
  419. mutating func streamOpened(_ id: HTTP2StreamID) {
  420. switch self.state {
  421. case .active(var state):
  422. let (inserted, _) = state.openStreams.insert(id)
  423. assert(inserted, "Can't open stream \(Int(id)), it's already open")
  424. self.state = .active(state)
  425. case .closing(var state):
  426. let (inserted, _) = state.openStreams.insert(id)
  427. assert(inserted, "Can't open stream \(Int(id)), it's already open")
  428. self.state = .closing(state)
  429. case .closed:
  430. ()
  431. }
  432. }
  433. enum OnStreamClosed: Equatable {
  434. /// Start the idle timer, after which the connection should be closed gracefully.
  435. case startIdleTimer(cancelKeepalive: Bool)
  436. /// Close the connection.
  437. case close
  438. /// Do nothing.
  439. case none
  440. }
  441. /// Record that the stream with the given ID has been closed.
  442. mutating func streamClosed(_ id: HTTP2StreamID) -> OnStreamClosed {
  443. let onStreamClosed: OnStreamClosed
  444. switch self.state {
  445. case .active(var state):
  446. let removedID = state.openStreams.remove(id)
  447. assert(removedID != nil, "Can't close stream \(Int(id)), it wasn't open")
  448. if state.openStreams.isEmpty {
  449. onStreamClosed = .startIdleTimer(cancelKeepalive: !state.allowKeepaliveWithoutCalls)
  450. } else {
  451. onStreamClosed = .none
  452. }
  453. self.state = .active(state)
  454. case .closing(var state):
  455. let removedID = state.openStreams.remove(id)
  456. assert(removedID != nil, "Can't close stream \(Int(id)), it wasn't open")
  457. onStreamClosed = state.openStreams.isEmpty ? .close : .none
  458. self.state = .closing(state)
  459. case .closed:
  460. onStreamClosed = .none
  461. }
  462. return onStreamClosed
  463. }
  464. /// Returns whether a keep alive ping should be sent to the server.
  465. mutating func sendKeepalivePing() -> Bool {
  466. let sendKeepalivePing: Bool
  467. // Only send a ping if there are open streams or there are no open streams and keep alive
  468. // is permitted when there are no active calls.
  469. switch self.state {
  470. case .active(let state):
  471. sendKeepalivePing = !state.openStreams.isEmpty || state.allowKeepaliveWithoutCalls
  472. case .closing(let state):
  473. sendKeepalivePing = !state.openStreams.isEmpty || state.allowKeepaliveWithoutCalls
  474. case .closed:
  475. sendKeepalivePing = false
  476. }
  477. return sendKeepalivePing
  478. }
  479. enum OnGracefulShutDown: Equatable {
  480. case sendGoAway(Bool)
  481. case none
  482. }
  483. mutating func beginGracefulShutdown(promise: EventLoopPromise<Void>?) -> OnGracefulShutDown {
  484. let onGracefulShutdown: OnGracefulShutDown
  485. switch self.state {
  486. case .active(let state):
  487. // Only close immediately if there are no open streams. The client doesn't need to
  488. // ratchet down the last stream ID as only the client creates streams in gRPC.
  489. let close = state.openStreams.isEmpty
  490. onGracefulShutdown = .sendGoAway(close)
  491. self.state = .closing(State.Closing(from: state, closePromise: promise))
  492. case .closing(var state):
  493. state.closePromise.setOrCascade(to: promise)
  494. self.state = .closing(state)
  495. onGracefulShutdown = .none
  496. case .closed:
  497. onGracefulShutdown = .none
  498. }
  499. return onGracefulShutdown
  500. }
  501. /// Returns whether the connection should be closed.
  502. mutating func beginClosing() -> Bool {
  503. switch self.state {
  504. case .active(let active):
  505. self.state = .closing(State.Closing(from: active, closePromise: nil))
  506. return true
  507. case .closing, .closed:
  508. return false
  509. }
  510. }
  511. enum OnClosed {
  512. case succeed(EventLoopPromise<Void>)
  513. case unexpectedClose((any Error)?, isIdle: Bool)
  514. case none
  515. }
  516. /// Marks the state as closed.
  517. mutating func closed() -> OnClosed {
  518. switch self.state {
  519. case .active(let state):
  520. self.state = .closed
  521. return .unexpectedClose(state.error, isIdle: state.openStreams.isEmpty)
  522. case .closing(let closing):
  523. self.state = .closed
  524. return closing.closePromise.map { .succeed($0) } ?? .none
  525. case .closed:
  526. self.state = .closed
  527. return .none
  528. }
  529. }
  530. }
  531. }