ClientConnectionHandler.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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(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: 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: 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 channelActive(context: ChannelHandlerContext) {
  114. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  115. self.keepaliveTimerFired(context: context)
  116. }
  117. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  118. self.maxIdleTimerFired(context: context)
  119. }
  120. }
  121. func channelInactive(context: ChannelHandlerContext) {
  122. switch self.state.closed() {
  123. case .none:
  124. ()
  125. case .unexpectedClose(let error, let isIdle):
  126. let event = self.wrapInboundOut(.closing(.unexpected(error, isIdle: isIdle)))
  127. context.fireChannelRead(event)
  128. case .succeed(let promise):
  129. promise.succeed()
  130. }
  131. self.keepaliveTimer?.cancel()
  132. self.keepaliveTimeoutTimer.cancel()
  133. }
  134. func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  135. switch event {
  136. case let event as NIOHTTP2StreamCreatedEvent:
  137. self._streamCreated(event.streamID, channel: context.channel)
  138. case let event as StreamClosedEvent:
  139. self._streamClosed(event.streamID, channel: context.channel)
  140. default:
  141. ()
  142. }
  143. context.fireUserInboundEventTriggered(event)
  144. }
  145. func errorCaught(context: ChannelHandlerContext, error: any Error) {
  146. // Store the error and close, this will result in the final close event being fired down
  147. // the pipeline with an appropriate close reason and appropriate error. (This avoids
  148. // the async channel just throwing the error.)
  149. self.state.receivedError(error)
  150. context.close(mode: .all, promise: nil)
  151. }
  152. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  153. let frame = self.unwrapInboundIn(data)
  154. self.inReadLoop = true
  155. switch frame.payload {
  156. case .goAway(_, let errorCode, let data):
  157. // Receiving a GOAWAY frame means we need to stop creating streams immediately and start
  158. // closing the connection.
  159. switch self.state.beginGracefulShutdown(promise: nil) {
  160. case .sendGoAway(let close):
  161. // gRPC servers may indicate why the GOAWAY was sent in the opaque data.
  162. let message = data.map { String(buffer: $0) } ?? ""
  163. context.fireChannelRead(self.wrapInboundOut(.closing(.goAway(errorCode, message))))
  164. // Clients should send GOAWAYs when closing a connection.
  165. self.writeAndFlushGoAway(context: context, errorCode: .noError)
  166. if close {
  167. context.close(promise: nil)
  168. }
  169. case .none:
  170. ()
  171. }
  172. case .ping(let data, let ack):
  173. // Pings are ack'd by the HTTP/2 handler so we only pay attention to acks here, and in
  174. // particular only those carrying the keep-alive data.
  175. if ack, data == self.keepalivePingData {
  176. self.keepaliveTimeoutTimer.cancel()
  177. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  178. self.keepaliveTimerFired(context: context)
  179. }
  180. }
  181. case .settings(.settings(_)):
  182. let isInitialSettings = self.state.receivedSettings()
  183. // The first settings frame indicates that the connection is now ready to use. The channel
  184. // becoming active is insufficient as, for example, a TLS handshake may fail after
  185. // establishing the TCP connection, or the server isn't configured for gRPC (or HTTP/2).
  186. if isInitialSettings {
  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 HTTP2StreamDelegate: @unchecked Sendable, NIOHTTP2StreamDelegate {
  228. // @unchecked is okay: the only methods do the appropriate event-loop dance.
  229. private let handler: ClientConnectionHandler
  230. init(_ handler: ClientConnectionHandler) {
  231. self.handler = handler
  232. }
  233. func streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  234. if self.handler.eventLoop.inEventLoop {
  235. self.handler._streamCreated(id, channel: channel)
  236. } else {
  237. self.handler.eventLoop.execute {
  238. self.handler._streamCreated(id, channel: channel)
  239. }
  240. }
  241. }
  242. func streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  243. if self.handler.eventLoop.inEventLoop {
  244. self.handler._streamClosed(id, channel: channel)
  245. } else {
  246. self.handler.eventLoop.execute {
  247. self.handler._streamClosed(id, channel: channel)
  248. }
  249. }
  250. }
  251. }
  252. var http2StreamDelegate: HTTP2StreamDelegate {
  253. return HTTP2StreamDelegate(self)
  254. }
  255. private func _streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  256. self.eventLoop.assertInEventLoop()
  257. // Stream created, so the connection isn't idle.
  258. self.maxIdleTimer?.cancel()
  259. self.state.streamOpened(id)
  260. }
  261. private func _streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  262. guard let context = self.context else { return }
  263. self.eventLoop.assertInEventLoop()
  264. switch self.state.streamClosed(id) {
  265. case .startIdleTimer(let cancelKeepalive):
  266. // All streams are closed, restart the idle timer, and stop the keep-alive timer (it may
  267. // not stop if keep-alive is allowed when there are no active calls).
  268. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  269. self.maxIdleTimerFired(context: context)
  270. }
  271. if cancelKeepalive {
  272. self.keepaliveTimer?.cancel()
  273. }
  274. case .close:
  275. // Connection was closing but waiting for all streams to close. They must all be closed
  276. // now so close the connection.
  277. context.close(promise: nil)
  278. case .none:
  279. ()
  280. }
  281. }
  282. }
  283. extension ClientConnectionHandler {
  284. private func maybeFlush(context: ChannelHandlerContext) {
  285. if self.inReadLoop {
  286. self.flushPending = true
  287. } else {
  288. context.flush()
  289. }
  290. }
  291. private func keepaliveTimerFired(context: ChannelHandlerContext) {
  292. guard self.state.sendKeepalivePing() else { return }
  293. // Cancel the keep alive timer when the client sends a ping. The timer is resumed when the ping
  294. // is acknowledged.
  295. self.keepaliveTimer?.cancel()
  296. let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(self.keepalivePingData, ack: false))
  297. context.write(self.wrapOutboundOut(ping), promise: nil)
  298. self.maybeFlush(context: context)
  299. // Schedule a timeout on waiting for the response.
  300. self.keepaliveTimeoutTimer.schedule(on: context.eventLoop) {
  301. self.keepaliveTimeoutExpired(context: context)
  302. }
  303. }
  304. private func keepaliveTimeoutExpired(context: ChannelHandlerContext) {
  305. guard self.state.beginClosing() else { return }
  306. context.fireChannelRead(self.wrapInboundOut(.closing(.keepaliveExpired)))
  307. self.writeAndFlushGoAway(context: context, message: "keepalive_expired")
  308. context.close(promise: nil)
  309. }
  310. private func maxIdleTimerFired(context: ChannelHandlerContext) {
  311. guard self.state.beginClosing() else { return }
  312. context.fireChannelRead(self.wrapInboundOut(.closing(.idle)))
  313. self.writeAndFlushGoAway(context: context, message: "idle")
  314. context.close(promise: nil)
  315. }
  316. private func writeAndFlushGoAway(
  317. context: ChannelHandlerContext,
  318. errorCode: HTTP2ErrorCode = .noError,
  319. message: String? = nil
  320. ) {
  321. let goAway = HTTP2Frame(
  322. streamID: .rootStream,
  323. payload: .goAway(
  324. lastStreamID: 0,
  325. errorCode: errorCode,
  326. opaqueData: message.map { context.channel.allocator.buffer(string: $0) }
  327. )
  328. )
  329. context.write(self.wrapOutboundOut(goAway), promise: nil)
  330. self.maybeFlush(context: context)
  331. }
  332. }
  333. extension ClientConnectionHandler {
  334. struct StateMachine {
  335. private var state: State
  336. private enum State {
  337. case active(Active)
  338. case closing(Closing)
  339. case closed
  340. struct Active {
  341. var openStreams: Set<HTTP2StreamID>
  342. var allowKeepaliveWithoutCalls: Bool
  343. var receivedConnectionPreface: Bool
  344. var error: (any Error)?
  345. init(allowKeepaliveWithoutCalls: Bool) {
  346. self.openStreams = []
  347. self.allowKeepaliveWithoutCalls = allowKeepaliveWithoutCalls
  348. self.receivedConnectionPreface = false
  349. self.error = nil
  350. }
  351. mutating func receivedSettings() -> Bool {
  352. let isFirstSettingsFrame = !self.receivedConnectionPreface
  353. self.receivedConnectionPreface = true
  354. return isFirstSettingsFrame
  355. }
  356. }
  357. struct Closing {
  358. var allowKeepaliveWithoutCalls: Bool
  359. var openStreams: Set<HTTP2StreamID>
  360. var closePromise: Optional<EventLoopPromise<Void>>
  361. init(from state: Active, closePromise: EventLoopPromise<Void>?) {
  362. self.openStreams = state.openStreams
  363. self.allowKeepaliveWithoutCalls = state.allowKeepaliveWithoutCalls
  364. self.closePromise = closePromise
  365. }
  366. }
  367. }
  368. init(allowKeepaliveWithoutCalls: Bool) {
  369. self.state = .active(State.Active(allowKeepaliveWithoutCalls: allowKeepaliveWithoutCalls))
  370. }
  371. /// Record that a SETTINGS frame was received from the remote peer.
  372. ///
  373. /// - Returns: `true` if this was the first SETTINGS frame received.
  374. mutating func receivedSettings() -> Bool {
  375. switch self.state {
  376. case .active(var active):
  377. let isFirstSettingsFrame = active.receivedSettings()
  378. self.state = .active(active)
  379. return isFirstSettingsFrame
  380. case .closing, .closed:
  381. return false
  382. }
  383. }
  384. /// Record that an error was received.
  385. mutating func receivedError(_ error: any Error) {
  386. switch self.state {
  387. case .active(var active):
  388. active.error = error
  389. self.state = .active(active)
  390. case .closing, .closed:
  391. ()
  392. }
  393. }
  394. /// Record that the stream with the given ID has been opened.
  395. mutating func streamOpened(_ id: HTTP2StreamID) {
  396. switch self.state {
  397. case .active(var state):
  398. let (inserted, _) = state.openStreams.insert(id)
  399. assert(inserted, "Can't open stream \(Int(id)), it's already open")
  400. self.state = .active(state)
  401. case .closing(var state):
  402. let (inserted, _) = state.openStreams.insert(id)
  403. assert(inserted, "Can't open stream \(Int(id)), it's already open")
  404. self.state = .closing(state)
  405. case .closed:
  406. ()
  407. }
  408. }
  409. enum OnStreamClosed: Equatable {
  410. /// Start the idle timer, after which the connection should be closed gracefully.
  411. case startIdleTimer(cancelKeepalive: Bool)
  412. /// Close the connection.
  413. case close
  414. /// Do nothing.
  415. case none
  416. }
  417. /// Record that the stream with the given ID has been closed.
  418. mutating func streamClosed(_ id: HTTP2StreamID) -> OnStreamClosed {
  419. let onStreamClosed: OnStreamClosed
  420. switch self.state {
  421. case .active(var state):
  422. let removedID = state.openStreams.remove(id)
  423. assert(removedID != nil, "Can't close stream \(Int(id)), it wasn't open")
  424. if state.openStreams.isEmpty {
  425. onStreamClosed = .startIdleTimer(cancelKeepalive: !state.allowKeepaliveWithoutCalls)
  426. } else {
  427. onStreamClosed = .none
  428. }
  429. self.state = .active(state)
  430. case .closing(var state):
  431. let removedID = state.openStreams.remove(id)
  432. assert(removedID != nil, "Can't close stream \(Int(id)), it wasn't open")
  433. onStreamClosed = state.openStreams.isEmpty ? .close : .none
  434. self.state = .closing(state)
  435. case .closed:
  436. onStreamClosed = .none
  437. }
  438. return onStreamClosed
  439. }
  440. /// Returns whether a keep alive ping should be sent to the server.
  441. mutating func sendKeepalivePing() -> Bool {
  442. let sendKeepalivePing: Bool
  443. // Only send a ping if there are open streams or there are no open streams and keep alive
  444. // is permitted when there are no active calls.
  445. switch self.state {
  446. case .active(let state):
  447. sendKeepalivePing = !state.openStreams.isEmpty || state.allowKeepaliveWithoutCalls
  448. case .closing(let state):
  449. sendKeepalivePing = !state.openStreams.isEmpty || state.allowKeepaliveWithoutCalls
  450. case .closed:
  451. sendKeepalivePing = false
  452. }
  453. return sendKeepalivePing
  454. }
  455. enum OnGracefulShutDown: Equatable {
  456. case sendGoAway(Bool)
  457. case none
  458. }
  459. mutating func beginGracefulShutdown(promise: EventLoopPromise<Void>?) -> OnGracefulShutDown {
  460. let onGracefulShutdown: OnGracefulShutDown
  461. switch self.state {
  462. case .active(let state):
  463. // Only close immediately if there are no open streams. The client doesn't need to
  464. // ratchet down the last stream ID as only the client creates streams in gRPC.
  465. let close = state.openStreams.isEmpty
  466. onGracefulShutdown = .sendGoAway(close)
  467. self.state = .closing(State.Closing(from: state, closePromise: promise))
  468. case .closing(var state):
  469. state.closePromise.setOrCascade(to: promise)
  470. self.state = .closing(state)
  471. onGracefulShutdown = .none
  472. case .closed:
  473. onGracefulShutdown = .none
  474. }
  475. return onGracefulShutdown
  476. }
  477. /// Returns whether the connection should be closed.
  478. mutating func beginClosing() -> Bool {
  479. switch self.state {
  480. case .active(let active):
  481. self.state = .closing(State.Closing(from: active, closePromise: nil))
  482. return true
  483. case .closing, .closed:
  484. return false
  485. }
  486. }
  487. enum OnClosed {
  488. case succeed(EventLoopPromise<Void>)
  489. case unexpectedClose(Error?, isIdle: Bool)
  490. case none
  491. }
  492. /// Marks the state as closed.
  493. mutating func closed() -> OnClosed {
  494. switch self.state {
  495. case .active(let state):
  496. self.state = .closed
  497. return .unexpectedClose(state.error, isIdle: state.openStreams.isEmpty)
  498. case .closing(let closing):
  499. self.state = .closed
  500. return closing.closePromise.map { .succeed($0) } ?? .none
  501. case .closed:
  502. self.state = .closed
  503. return .none
  504. }
  505. }
  506. }
  507. }