ClientConnectionHandler.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 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. self.keepaliveTimeoutTimer.cancel()
  169. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  170. self.keepaliveTimerFired(context: context)
  171. }
  172. }
  173. case .settings(.settings(_)):
  174. let isInitialSettings = self.state.receivedSettings()
  175. // The first settings frame indicates that the connection is now ready to use. The channel
  176. // becoming active is insufficient as, for example, a TLS handshake may fail after
  177. // establishing the TCP connection, or the server isn't configured for gRPC (or HTTP/2).
  178. if isInitialSettings {
  179. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  180. self.keepaliveTimerFired(context: context)
  181. }
  182. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  183. self.maxIdleTimerFired(context: context)
  184. }
  185. context.fireChannelRead(self.wrapInboundOut(.ready))
  186. }
  187. default:
  188. ()
  189. }
  190. }
  191. func channelReadComplete(context: ChannelHandlerContext) {
  192. while self.flushPending {
  193. self.flushPending = false
  194. context.flush()
  195. }
  196. self.inReadLoop = false
  197. context.fireChannelReadComplete()
  198. }
  199. func triggerUserOutboundEvent(
  200. context: ChannelHandlerContext,
  201. event: Any,
  202. promise: EventLoopPromise<Void>?
  203. ) {
  204. if let event = event as? OutboundEvent {
  205. switch event {
  206. case .closeGracefully:
  207. switch self.state.beginGracefulShutdown(promise: promise) {
  208. case .sendGoAway(let close):
  209. context.fireChannelRead(self.wrapInboundOut(.closing(.initiatedLocally)))
  210. // The client could send a GOAWAY at this point but it's not really necessary, the server
  211. // can't open streams anyway, the client will just close the connection when it's done.
  212. if close {
  213. context.close(promise: nil)
  214. }
  215. case .none:
  216. ()
  217. }
  218. }
  219. } else {
  220. context.triggerUserOutboundEvent(event, promise: promise)
  221. }
  222. }
  223. }
  224. extension ClientConnectionHandler {
  225. struct HTTP2StreamDelegate: @unchecked Sendable, NIOHTTP2StreamDelegate {
  226. // @unchecked is okay: the only methods do the appropriate event-loop dance.
  227. private let handler: ClientConnectionHandler
  228. init(_ handler: ClientConnectionHandler) {
  229. self.handler = handler
  230. }
  231. func streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  232. if self.handler.eventLoop.inEventLoop {
  233. self.handler._streamCreated(id, channel: channel)
  234. } else {
  235. self.handler.eventLoop.execute {
  236. self.handler._streamCreated(id, channel: channel)
  237. }
  238. }
  239. }
  240. func streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  241. if self.handler.eventLoop.inEventLoop {
  242. self.handler._streamClosed(id, channel: channel)
  243. } else {
  244. self.handler.eventLoop.execute {
  245. self.handler._streamClosed(id, channel: channel)
  246. }
  247. }
  248. }
  249. }
  250. var http2StreamDelegate: HTTP2StreamDelegate {
  251. return HTTP2StreamDelegate(self)
  252. }
  253. private func _streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  254. self.eventLoop.assertInEventLoop()
  255. // Stream created, so the connection isn't idle.
  256. self.maxIdleTimer?.cancel()
  257. self.state.streamOpened(id)
  258. }
  259. private func _streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  260. guard let context = self.context else { return }
  261. self.eventLoop.assertInEventLoop()
  262. switch self.state.streamClosed(id) {
  263. case .startIdleTimer(let cancelKeepalive):
  264. // All streams are closed, restart the idle timer, and stop the keep-alive timer (it may
  265. // not stop if keep-alive is allowed when there are no active calls).
  266. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  267. self.maxIdleTimerFired(context: context)
  268. }
  269. if cancelKeepalive {
  270. self.keepaliveTimer?.cancel()
  271. }
  272. case .close:
  273. // Connection was closing but waiting for all streams to close. They must all be closed
  274. // now so close the connection.
  275. context.close(promise: nil)
  276. case .none:
  277. ()
  278. }
  279. }
  280. }
  281. extension ClientConnectionHandler {
  282. private func maybeFlush(context: ChannelHandlerContext) {
  283. if self.inReadLoop {
  284. self.flushPending = true
  285. } else {
  286. context.flush()
  287. }
  288. }
  289. private func keepaliveTimerFired(context: ChannelHandlerContext) {
  290. guard self.state.sendKeepalivePing() else { return }
  291. // Cancel the keep alive timer when the client sends a ping. The timer is resumed when the ping
  292. // is acknowledged.
  293. self.keepaliveTimer?.cancel()
  294. let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(self.keepalivePingData, ack: false))
  295. context.write(self.wrapOutboundOut(ping), promise: nil)
  296. self.maybeFlush(context: context)
  297. // Schedule a timeout on waiting for the response.
  298. self.keepaliveTimeoutTimer.schedule(on: context.eventLoop) {
  299. self.keepaliveTimeoutExpired(context: context)
  300. }
  301. }
  302. private func keepaliveTimeoutExpired(context: ChannelHandlerContext) {
  303. guard self.state.beginClosing() else { return }
  304. context.fireChannelRead(self.wrapInboundOut(.closing(.keepaliveExpired)))
  305. self.writeAndFlushGoAway(context: context, message: "keepalive_expired")
  306. context.close(promise: nil)
  307. }
  308. private func maxIdleTimerFired(context: ChannelHandlerContext) {
  309. guard self.state.beginClosing() else { return }
  310. context.fireChannelRead(self.wrapInboundOut(.closing(.idle)))
  311. self.writeAndFlushGoAway(context: context, message: "idle")
  312. context.close(promise: nil)
  313. }
  314. private func writeAndFlushGoAway(
  315. context: ChannelHandlerContext,
  316. errorCode: HTTP2ErrorCode = .noError,
  317. message: String? = nil
  318. ) {
  319. let goAway = HTTP2Frame(
  320. streamID: .rootStream,
  321. payload: .goAway(
  322. lastStreamID: 0,
  323. errorCode: errorCode,
  324. opaqueData: message.map { context.channel.allocator.buffer(string: $0) }
  325. )
  326. )
  327. context.write(self.wrapOutboundOut(goAway), promise: nil)
  328. self.maybeFlush(context: context)
  329. }
  330. }
  331. extension ClientConnectionHandler {
  332. struct StateMachine {
  333. private var state: State
  334. private enum State {
  335. case active(Active)
  336. case closing(Closing)
  337. case closed
  338. struct Active {
  339. var openStreams: Set<HTTP2StreamID>
  340. var allowKeepaliveWithoutCalls: Bool
  341. var receivedConnectionPreface: Bool
  342. var error: (any Error)?
  343. init(allowKeepaliveWithoutCalls: Bool) {
  344. self.openStreams = []
  345. self.allowKeepaliveWithoutCalls = allowKeepaliveWithoutCalls
  346. self.receivedConnectionPreface = false
  347. self.error = nil
  348. }
  349. mutating func receivedSettings() -> Bool {
  350. let isFirstSettingsFrame = !self.receivedConnectionPreface
  351. self.receivedConnectionPreface = true
  352. return isFirstSettingsFrame
  353. }
  354. }
  355. struct Closing {
  356. var allowKeepaliveWithoutCalls: Bool
  357. var openStreams: Set<HTTP2StreamID>
  358. var closePromise: Optional<EventLoopPromise<Void>>
  359. init(from state: Active, closePromise: EventLoopPromise<Void>?) {
  360. self.openStreams = state.openStreams
  361. self.allowKeepaliveWithoutCalls = state.allowKeepaliveWithoutCalls
  362. self.closePromise = closePromise
  363. }
  364. }
  365. }
  366. init(allowKeepaliveWithoutCalls: Bool) {
  367. self.state = .active(State.Active(allowKeepaliveWithoutCalls: allowKeepaliveWithoutCalls))
  368. }
  369. /// Record that a SETTINGS frame was received from the remote peer.
  370. ///
  371. /// - Returns: `true` if this was the first SETTINGS frame received.
  372. mutating func receivedSettings() -> Bool {
  373. switch self.state {
  374. case .active(var active):
  375. let isFirstSettingsFrame = active.receivedSettings()
  376. self.state = .active(active)
  377. return isFirstSettingsFrame
  378. case .closing, .closed:
  379. return false
  380. }
  381. }
  382. /// Record that an error was received.
  383. mutating func receivedError(_ error: any Error) {
  384. switch self.state {
  385. case .active(var active):
  386. active.error = error
  387. self.state = .active(active)
  388. case .closing, .closed:
  389. ()
  390. }
  391. }
  392. /// Record that the stream with the given ID has been opened.
  393. mutating func streamOpened(_ id: HTTP2StreamID) {
  394. switch self.state {
  395. case .active(var state):
  396. let (inserted, _) = state.openStreams.insert(id)
  397. assert(inserted, "Can't open stream \(Int(id)), it's already open")
  398. self.state = .active(state)
  399. case .closing(var state):
  400. let (inserted, _) = state.openStreams.insert(id)
  401. assert(inserted, "Can't open stream \(Int(id)), it's already open")
  402. self.state = .closing(state)
  403. case .closed:
  404. ()
  405. }
  406. }
  407. enum OnStreamClosed: Equatable {
  408. /// Start the idle timer, after which the connection should be closed gracefully.
  409. case startIdleTimer(cancelKeepalive: Bool)
  410. /// Close the connection.
  411. case close
  412. /// Do nothing.
  413. case none
  414. }
  415. /// Record that the stream with the given ID has been closed.
  416. mutating func streamClosed(_ id: HTTP2StreamID) -> OnStreamClosed {
  417. let onStreamClosed: OnStreamClosed
  418. switch self.state {
  419. case .active(var state):
  420. let removedID = state.openStreams.remove(id)
  421. assert(removedID != nil, "Can't close stream \(Int(id)), it wasn't open")
  422. if state.openStreams.isEmpty {
  423. onStreamClosed = .startIdleTimer(cancelKeepalive: !state.allowKeepaliveWithoutCalls)
  424. } else {
  425. onStreamClosed = .none
  426. }
  427. self.state = .active(state)
  428. case .closing(var state):
  429. let removedID = state.openStreams.remove(id)
  430. assert(removedID != nil, "Can't close stream \(Int(id)), it wasn't open")
  431. onStreamClosed = state.openStreams.isEmpty ? .close : .none
  432. self.state = .closing(state)
  433. case .closed:
  434. onStreamClosed = .none
  435. }
  436. return onStreamClosed
  437. }
  438. /// Returns whether a keep alive ping should be sent to the server.
  439. mutating func sendKeepalivePing() -> Bool {
  440. let sendKeepalivePing: Bool
  441. // Only send a ping if there are open streams or there are no open streams and keep alive
  442. // is permitted when there are no active calls.
  443. switch self.state {
  444. case .active(let state):
  445. sendKeepalivePing = !state.openStreams.isEmpty || state.allowKeepaliveWithoutCalls
  446. case .closing(let state):
  447. sendKeepalivePing = !state.openStreams.isEmpty || state.allowKeepaliveWithoutCalls
  448. case .closed:
  449. sendKeepalivePing = false
  450. }
  451. return sendKeepalivePing
  452. }
  453. enum OnGracefulShutDown: Equatable {
  454. case sendGoAway(Bool)
  455. case none
  456. }
  457. mutating func beginGracefulShutdown(promise: EventLoopPromise<Void>?) -> OnGracefulShutDown {
  458. let onGracefulShutdown: OnGracefulShutDown
  459. switch self.state {
  460. case .active(let state):
  461. // Only close immediately if there are no open streams. The client doesn't need to
  462. // ratchet down the last stream ID as only the client creates streams in gRPC.
  463. let close = state.openStreams.isEmpty
  464. onGracefulShutdown = .sendGoAway(close)
  465. self.state = .closing(State.Closing(from: state, closePromise: promise))
  466. case .closing(var state):
  467. state.closePromise.setOrCascade(to: promise)
  468. self.state = .closing(state)
  469. onGracefulShutdown = .none
  470. case .closed:
  471. onGracefulShutdown = .none
  472. }
  473. return onGracefulShutdown
  474. }
  475. /// Returns whether the connection should be closed.
  476. mutating func beginClosing() -> Bool {
  477. switch self.state {
  478. case .active(let active):
  479. self.state = .closing(State.Closing(from: active, closePromise: nil))
  480. return true
  481. case .closing, .closed:
  482. return false
  483. }
  484. }
  485. enum OnClosed {
  486. case succeed(EventLoopPromise<Void>)
  487. case unexpectedClose(Error?, isIdle: Bool)
  488. case none
  489. }
  490. /// Marks the state as closed.
  491. mutating func closed() -> OnClosed {
  492. switch self.state {
  493. case .active(let state):
  494. self.state = .closed
  495. return .unexpectedClose(state.error, isIdle: state.openStreams.isEmpty)
  496. case .closing(let closing):
  497. self.state = .closed
  498. return closing.closePromise.map { .succeed($0) } ?? .none
  499. case .closed:
  500. self.state = .closed
  501. return .none
  502. }
  503. }
  504. }
  505. }