ServerConnectionManagementHandler.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. internal import NIOCore
  17. internal import NIOHTTP2
  18. /// A `ChannelHandler` which manages the lifecycle of a gRPC connection over HTTP/2.
  19. ///
  20. /// This handler is responsible for managing several aspects of the connection. These include:
  21. /// 1. Handling the graceful close of connections. When gracefully closing a connection the server
  22. /// sends a GOAWAY frame with the last stream ID set to the maximum stream ID allowed followed by
  23. /// a PING frame. On receipt of the PING frame the server sends another GOAWAY frame with the
  24. /// highest ID of all streams which have been opened. After this, the handler closes the
  25. /// connection once all streams are closed.
  26. /// 2. Enforcing that graceful shutdown doesn't exceed a configured limit (if configured).
  27. /// 3. Gracefully closing the connection once it reaches the maximum configured age (if configured).
  28. /// 4. Gracefully closing the connection once it has been idle for a given period of time (if
  29. /// configured).
  30. /// 5. Periodically sending keep alive pings to the client (if configured) and closing the
  31. /// connection if necessary.
  32. /// 6. Policing pings sent by the client to ensure that the client isn't misconfigured to send
  33. /// too many pings.
  34. ///
  35. /// Some of the behaviours are described in:
  36. /// - [gRFC A8](https://github.com/grpc/proposal/blob/master/A8-client-side-keepalive.md), and
  37. /// - [gRFC A9](https://github.com/grpc/proposal/blob/master/A9-server-side-conn-mgt.md).
  38. final class ServerConnectionManagementHandler: ChannelDuplexHandler {
  39. typealias InboundIn = HTTP2Frame
  40. typealias InboundOut = HTTP2Frame
  41. typealias OutboundIn = HTTP2Frame
  42. typealias OutboundOut = HTTP2Frame
  43. /// The `EventLoop` of the `Channel` this handler exists in.
  44. private let eventLoop: any EventLoop
  45. /// The maximum amount of time a connection may be idle for. If the connection remains idle
  46. /// (i.e. has no open streams) for this period of time then the connection will be gracefully
  47. /// closed.
  48. private var maxIdleTimer: Timer?
  49. /// The maximum age of a connection. If the connection remains open after this amount of time
  50. /// then it will be gracefully closed.
  51. private var maxAgeTimer: Timer?
  52. /// The maximum amount of time a connection may spend closing gracefully, after which it is
  53. /// closed abruptly. The timer starts after the second GOAWAY frame has been sent.
  54. private var maxGraceTimer: Timer?
  55. /// The amount of time to wait before sending a keep alive ping.
  56. private var keepaliveTimer: Timer?
  57. /// The amount of time the client has to reply after sending a keep alive ping. Only used if
  58. /// `keepaliveTimer` is set.
  59. private var keepaliveTimeoutTimer: Timer
  60. /// Opaque data sent in keep alive pings.
  61. private let keepalivePingData: HTTP2PingData
  62. /// Whether a flush is pending.
  63. private var flushPending: Bool
  64. /// Whether `channelRead` has been called and `channelReadComplete` hasn't yet been called.
  65. /// Resets once `channelReadComplete` returns.
  66. private var inReadLoop: Bool
  67. /// The context of the channel this handler is in.
  68. private var context: ChannelHandlerContext?
  69. /// The current state of the connection.
  70. private var state: StateMachine
  71. /// The clock.
  72. private let clock: Clock
  73. /// A clock providing the current time.
  74. ///
  75. /// This is necessary for testing where a manual clock can be used and advanced from the test.
  76. /// While NIO's `EmbeddedEventLoop` provides control over its view of time (and therefore any
  77. /// events scheduled on it) it doesn't offer a way to get the current time. This is usually done
  78. /// via `NIODeadline`.
  79. enum Clock {
  80. case nio
  81. case manual(Manual)
  82. func now() -> NIODeadline {
  83. switch self {
  84. case .nio:
  85. return .now()
  86. case .manual(let clock):
  87. return clock.time
  88. }
  89. }
  90. final class Manual {
  91. private(set) var time: NIODeadline
  92. init() {
  93. self.time = .uptimeNanoseconds(0)
  94. }
  95. func advance(by amount: TimeAmount) {
  96. self.time = self.time + amount
  97. }
  98. }
  99. }
  100. /// Stats about recently written frames. Used to determine whether to reset keep-alive state.
  101. private var frameStats: FrameStats
  102. struct FrameStats {
  103. private(set) var didWriteHeadersOrData = false
  104. /// Mark that a HEADERS frame has been written.
  105. mutating func wroteHeaders() {
  106. self.didWriteHeadersOrData = true
  107. }
  108. /// Mark that DATA frame has been written.
  109. mutating func wroteData() {
  110. self.didWriteHeadersOrData = true
  111. }
  112. /// Resets the state such that no HEADERS or DATA frames have been written.
  113. mutating func reset() {
  114. self.didWriteHeadersOrData = false
  115. }
  116. }
  117. /// A synchronous view over this handler.
  118. var syncView: SyncView {
  119. return SyncView(self)
  120. }
  121. /// A synchronous view over this handler.
  122. ///
  123. /// Methods on this view *must* be called from the same `EventLoop` as the `Channel` in which
  124. /// this handler exists.
  125. struct SyncView {
  126. private let handler: ServerConnectionManagementHandler
  127. fileprivate init(_ handler: ServerConnectionManagementHandler) {
  128. self.handler = handler
  129. }
  130. /// Notify the handler that the connection has received a flush event.
  131. func connectionWillFlush() {
  132. // The handler can't rely on `flush(context:)` due to its expected position in the pipeline.
  133. // It's expected to be placed after the HTTP/2 handler (i.e. closer to the application) as
  134. // it needs to receive HTTP/2 frames. However, flushes from stream channels aren't sent down
  135. // the entire connection channel, instead they are sent from the point in the channel they
  136. // are multiplexed from (either the HTTP/2 handler or the HTTP/2 multiplexing handler,
  137. // depending on how multiplexing is configured).
  138. self.handler.eventLoop.assertInEventLoop()
  139. if self.handler.frameStats.didWriteHeadersOrData {
  140. self.handler.frameStats.reset()
  141. self.handler.state.resetKeepaliveState()
  142. }
  143. }
  144. /// Notify the handler that a HEADERS frame was written in the last write loop.
  145. func wroteHeadersFrame() {
  146. self.handler.eventLoop.assertInEventLoop()
  147. self.handler.frameStats.wroteHeaders()
  148. }
  149. /// Notify the handler that a DATA frame was written in the last write loop.
  150. func wroteDataFrame() {
  151. self.handler.eventLoop.assertInEventLoop()
  152. self.handler.frameStats.wroteData()
  153. }
  154. }
  155. /// Creates a new handler which manages the lifecycle of a connection.
  156. ///
  157. /// - Parameters:
  158. /// - eventLoop: The `EventLoop` of the `Channel` this handler is placed in.
  159. /// - maxIdleTime: The maximum amount time a connection may be idle for before being closed.
  160. /// - maxAge: The maximum amount of time a connection may exist before being gracefully closed.
  161. /// - maxGraceTime: The maximum amount of time that the connection has to close gracefully.
  162. /// - keepaliveTime: The amount of time to wait after reading data before sending a keep-alive
  163. /// ping.
  164. /// - keepaliveTimeout: The amount of time the client has to reply after the server sends a
  165. /// keep-alive ping to keep the connection open. The connection is closed if no reply
  166. /// is received.
  167. /// - allowKeepaliveWithoutCalls: Whether the server allows the client to send keep-alive pings
  168. /// when there are no calls in progress.
  169. /// - minPingIntervalWithoutCalls: The minimum allowed interval the client is allowed to send
  170. /// keep-alive pings. Pings more frequent than this interval count as 'strikes' and the
  171. /// connection is closed if there are too many strikes.
  172. /// - clock: A clock providing the current time.
  173. init(
  174. eventLoop: any EventLoop,
  175. maxIdleTime: TimeAmount?,
  176. maxAge: TimeAmount?,
  177. maxGraceTime: TimeAmount?,
  178. keepaliveTime: TimeAmount?,
  179. keepaliveTimeout: TimeAmount?,
  180. allowKeepaliveWithoutCalls: Bool,
  181. minPingIntervalWithoutCalls: TimeAmount,
  182. clock: Clock = .nio
  183. ) {
  184. self.eventLoop = eventLoop
  185. self.maxIdleTimer = maxIdleTime.map { Timer(delay: $0) }
  186. self.maxAgeTimer = maxAge.map { Timer(delay: $0) }
  187. self.maxGraceTimer = maxGraceTime.map { Timer(delay: $0) }
  188. self.keepaliveTimer = keepaliveTime.map { Timer(delay: $0) }
  189. // Always create a keep alive timeout timer, it's only used if there is a keep alive timer.
  190. self.keepaliveTimeoutTimer = Timer(delay: keepaliveTimeout ?? .seconds(20))
  191. // Generate a random value to be used as keep alive ping data.
  192. let pingData = UInt64.random(in: .min ... .max)
  193. self.keepalivePingData = HTTP2PingData(withInteger: pingData)
  194. self.state = StateMachine(
  195. allowKeepaliveWithoutCalls: allowKeepaliveWithoutCalls,
  196. minPingReceiveIntervalWithoutCalls: minPingIntervalWithoutCalls,
  197. goAwayPingData: HTTP2PingData(withInteger: ~pingData)
  198. )
  199. self.flushPending = false
  200. self.inReadLoop = false
  201. self.clock = clock
  202. self.frameStats = FrameStats()
  203. }
  204. func handlerAdded(context: ChannelHandlerContext) {
  205. assert(context.eventLoop === self.eventLoop)
  206. self.context = context
  207. }
  208. func handlerRemoved(context: ChannelHandlerContext) {
  209. self.context = nil
  210. }
  211. func channelActive(context: ChannelHandlerContext) {
  212. let view = LoopBoundView(handler: self, context: context)
  213. self.maxAgeTimer?.schedule(on: context.eventLoop) {
  214. view.initiateGracefulShutdown()
  215. }
  216. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  217. view.initiateGracefulShutdown()
  218. }
  219. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  220. view.keepaliveTimerFired()
  221. }
  222. context.fireChannelActive()
  223. }
  224. func channelInactive(context: ChannelHandlerContext) {
  225. self.maxIdleTimer?.cancel()
  226. self.maxAgeTimer?.cancel()
  227. self.maxGraceTimer?.cancel()
  228. self.keepaliveTimer?.cancel()
  229. self.keepaliveTimeoutTimer.cancel()
  230. context.fireChannelInactive()
  231. }
  232. func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  233. switch event {
  234. case let event as NIOHTTP2StreamCreatedEvent:
  235. self._streamCreated(event.streamID, channel: context.channel)
  236. case let event as StreamClosedEvent:
  237. self._streamClosed(event.streamID, channel: context.channel)
  238. case is ChannelShouldQuiesceEvent:
  239. self.initiateGracefulShutdown(context: context)
  240. default:
  241. ()
  242. }
  243. context.fireUserInboundEventTriggered(event)
  244. }
  245. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  246. self.inReadLoop = true
  247. // Any read data indicates that the connection is alive so cancel the keep-alive timers.
  248. self.keepaliveTimer?.cancel()
  249. self.keepaliveTimeoutTimer.cancel()
  250. let frame = self.unwrapInboundIn(data)
  251. switch frame.payload {
  252. case .ping(let data, let ack):
  253. if ack {
  254. self.handlePingAck(context: context, data: data)
  255. } else {
  256. self.handlePing(context: context, data: data)
  257. }
  258. default:
  259. () // Only interested in PING frames, ignore the rest.
  260. }
  261. context.fireChannelRead(data)
  262. }
  263. func channelReadComplete(context: ChannelHandlerContext) {
  264. while self.flushPending {
  265. self.flushPending = false
  266. context.flush()
  267. }
  268. self.inReadLoop = false
  269. // Done reading: schedule the keep-alive timer.
  270. let view = LoopBoundView(handler: self, context: context)
  271. self.keepaliveTimer?.schedule(on: context.eventLoop) {
  272. view.keepaliveTimerFired()
  273. }
  274. context.fireChannelReadComplete()
  275. }
  276. func flush(context: ChannelHandlerContext) {
  277. self.maybeFlush(context: context)
  278. }
  279. }
  280. extension ServerConnectionManagementHandler {
  281. struct LoopBoundView: @unchecked Sendable {
  282. private let handler: ServerConnectionManagementHandler
  283. private let context: ChannelHandlerContext
  284. init(handler: ServerConnectionManagementHandler, context: ChannelHandlerContext) {
  285. self.handler = handler
  286. self.context = context
  287. }
  288. func initiateGracefulShutdown() {
  289. self.context.eventLoop.assertInEventLoop()
  290. self.handler.initiateGracefulShutdown(context: self.context)
  291. }
  292. func keepaliveTimerFired() {
  293. self.context.eventLoop.assertInEventLoop()
  294. self.handler.keepaliveTimerFired(context: self.context)
  295. }
  296. }
  297. }
  298. extension ServerConnectionManagementHandler {
  299. struct HTTP2StreamDelegate: @unchecked Sendable, NIOHTTP2StreamDelegate {
  300. // @unchecked is okay: the only methods do the appropriate event-loop dance.
  301. private let handler: ServerConnectionManagementHandler
  302. init(_ handler: ServerConnectionManagementHandler) {
  303. self.handler = handler
  304. }
  305. func streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  306. if self.handler.eventLoop.inEventLoop {
  307. self.handler._streamCreated(id, channel: channel)
  308. } else {
  309. self.handler.eventLoop.execute {
  310. self.handler._streamCreated(id, channel: channel)
  311. }
  312. }
  313. }
  314. func streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  315. if self.handler.eventLoop.inEventLoop {
  316. self.handler._streamClosed(id, channel: channel)
  317. } else {
  318. self.handler.eventLoop.execute {
  319. self.handler._streamClosed(id, channel: channel)
  320. }
  321. }
  322. }
  323. }
  324. var http2StreamDelegate: HTTP2StreamDelegate {
  325. return HTTP2StreamDelegate(self)
  326. }
  327. private func _streamCreated(_ id: HTTP2StreamID, channel: any Channel) {
  328. // The connection isn't idle if a stream is open.
  329. self.maxIdleTimer?.cancel()
  330. self.state.streamOpened(id)
  331. }
  332. private func _streamClosed(_ id: HTTP2StreamID, channel: any Channel) {
  333. guard let context = self.context else { return }
  334. switch self.state.streamClosed(id) {
  335. case .startIdleTimer:
  336. let loopBound = LoopBoundView(handler: self, context: context)
  337. self.maxIdleTimer?.schedule(on: context.eventLoop) {
  338. loopBound.initiateGracefulShutdown()
  339. }
  340. case .close:
  341. context.close(mode: .all, promise: nil)
  342. case .none:
  343. ()
  344. }
  345. }
  346. }
  347. extension ServerConnectionManagementHandler {
  348. private func maybeFlush(context: ChannelHandlerContext) {
  349. if self.inReadLoop {
  350. self.flushPending = true
  351. } else {
  352. context.flush()
  353. }
  354. }
  355. private func initiateGracefulShutdown(context: ChannelHandlerContext) {
  356. context.eventLoop.assertInEventLoop()
  357. // Cancel any timers if initiating shutdown.
  358. self.maxIdleTimer?.cancel()
  359. self.maxAgeTimer?.cancel()
  360. self.keepaliveTimer?.cancel()
  361. self.keepaliveTimeoutTimer.cancel()
  362. switch self.state.startGracefulShutdown() {
  363. case .sendGoAwayAndPing(let pingData):
  364. // There's a time window between the server sending a GOAWAY frame and the client receiving
  365. // it. During this time the client may open new streams as it doesn't yet know about the
  366. // GOAWAY frame.
  367. //
  368. // The server therefore sends a GOAWAY with the last stream ID set to the maximum stream ID
  369. // and follows it with a PING frame. When the server receives the ack for the PING frame it
  370. // knows that the client has received the initial GOAWAY frame and that no more streams may
  371. // be opened. The server can then send an additional GOAWAY frame with a more representative
  372. // last stream ID.
  373. let goAway = HTTP2Frame(
  374. streamID: .rootStream,
  375. payload: .goAway(
  376. lastStreamID: .maxID,
  377. errorCode: .noError,
  378. opaqueData: nil
  379. )
  380. )
  381. let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(pingData, ack: false))
  382. context.write(self.wrapOutboundOut(goAway), promise: nil)
  383. context.write(self.wrapOutboundOut(ping), promise: nil)
  384. self.maybeFlush(context: context)
  385. case .none:
  386. () // Already shutting down.
  387. }
  388. }
  389. private func handlePing(context: ChannelHandlerContext, data: HTTP2PingData) {
  390. switch self.state.receivedPing(atTime: self.clock.now(), data: data) {
  391. case .enhanceYourCalmThenClose(let streamID):
  392. let goAway = HTTP2Frame(
  393. streamID: .rootStream,
  394. payload: .goAway(
  395. lastStreamID: streamID,
  396. errorCode: .enhanceYourCalm,
  397. opaqueData: context.channel.allocator.buffer(string: "too_many_pings")
  398. )
  399. )
  400. context.write(self.wrapOutboundOut(goAway), promise: nil)
  401. self.maybeFlush(context: context)
  402. context.close(promise: nil)
  403. case .sendAck:
  404. () // ACKs are sent by NIO's HTTP/2 handler, don't double ack.
  405. case .none:
  406. ()
  407. }
  408. }
  409. private func handlePingAck(context: ChannelHandlerContext, data: HTTP2PingData) {
  410. switch self.state.receivedPingAck(data: data) {
  411. case .sendGoAway(let streamID, let close):
  412. let goAway = HTTP2Frame(
  413. streamID: .rootStream,
  414. payload: .goAway(lastStreamID: streamID, errorCode: .noError, opaqueData: nil)
  415. )
  416. context.write(self.wrapOutboundOut(goAway), promise: nil)
  417. self.maybeFlush(context: context)
  418. if close {
  419. context.close(promise: nil)
  420. } else {
  421. // RPCs may have a grace period for finishing once the second GOAWAY frame has finished.
  422. // If this is set close the connection abruptly once the grace period passes.
  423. let loopBound = NIOLoopBound(context, eventLoop: context.eventLoop)
  424. self.maxGraceTimer?.schedule(on: context.eventLoop) {
  425. loopBound.value.close(promise: nil)
  426. }
  427. }
  428. case .none:
  429. ()
  430. }
  431. }
  432. private func keepaliveTimerFired(context: ChannelHandlerContext) {
  433. let ping = HTTP2Frame(streamID: .rootStream, payload: .ping(self.keepalivePingData, ack: false))
  434. context.write(self.wrapInboundOut(ping), promise: nil)
  435. self.maybeFlush(context: context)
  436. // Schedule a timeout on waiting for the response.
  437. let loopBound = LoopBoundView(handler: self, context: context)
  438. self.keepaliveTimeoutTimer.schedule(on: context.eventLoop) {
  439. loopBound.initiateGracefulShutdown()
  440. }
  441. }
  442. }