GRPCIdleHandlerStateMachine.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 Logging
  17. import NIOCore
  18. import NIOHTTP2
  19. /// Holds state for the 'GRPCIdleHandler', this isn't really just the idleness of the connection,
  20. /// it also holds state relevant to quiescing the connection as well as logging some HTTP/2 specific
  21. /// information (like stream creation/close events and changes to settings which can be useful when
  22. /// debugging live systems). Much of this information around the connection state is also used to
  23. /// inform the client connection manager since that's strongly tied to various channel and HTTP/2
  24. /// events.
  25. struct GRPCIdleHandlerStateMachine {
  26. /// Our role in the connection.
  27. enum Role {
  28. case server
  29. case client
  30. }
  31. /// The 'operating' state of the connection. This is the primary state we expect to be in: the
  32. /// connection is up and running and there are expected to be active RPCs, although this is by no
  33. /// means a requirement. Some of the situations in which there may be no active RPCs are:
  34. ///
  35. /// 1. Before the connection is 'ready' (that is, seen the first SETTINGS frame),
  36. /// 2. After the connection has dropped to zero active streams and before the idle timeout task
  37. /// has been scheduled.
  38. /// 3. When the connection has zero active streams and the connection was configured without an
  39. /// idle timeout.
  40. fileprivate struct Operating: CanOpenStreams, CanCloseStreams {
  41. /// Our role in the connection.
  42. var role: Role
  43. /// The number of open stream.
  44. var openStreams: Int
  45. /// The last stream ID initiated by the remote peer.
  46. var lastPeerInitiatedStreamID: HTTP2StreamID
  47. /// The maximum number of concurrent streams we are allowed to operate.
  48. var maxConcurrentStreams: Int
  49. /// We keep track of whether we've seen a SETTINGS frame. We expect to see one after the
  50. /// connection preface (RFC 7540 § 3.5). This is primarily for the benefit of the client which
  51. /// determines a connection to be 'ready' once it has seen the first SETTINGS frame. We also
  52. /// won't set an idle timeout until this becomes true.
  53. var hasSeenSettings: Bool
  54. fileprivate init(role: Role) {
  55. self.role = role
  56. self.openStreams = 0
  57. self.lastPeerInitiatedStreamID = .rootStream
  58. // Assumed until we know better.
  59. self.maxConcurrentStreams = 100
  60. self.hasSeenSettings = false
  61. }
  62. fileprivate init(fromWaitingToIdle state: WaitingToIdle) {
  63. self.role = state.role
  64. self.openStreams = 0
  65. self.lastPeerInitiatedStreamID = state.lastPeerInitiatedStreamID
  66. self.maxConcurrentStreams = state.maxConcurrentStreams
  67. // We won't transition to 'WaitingToIdle' unless we've seen a SETTINGS frame.
  68. self.hasSeenSettings = true
  69. }
  70. }
  71. /// The waiting-to-idle state is used when the connection has become 'ready', has no active
  72. /// RPCs and an idle timeout task has been scheduled. In this state, the connection will be closed
  73. /// once the idle is fired. The task will be cancelled on the creation of a stream.
  74. fileprivate struct WaitingToIdle {
  75. /// Our role in the connection.
  76. var role: Role
  77. /// The last stream ID initiated by the remote peer.
  78. var lastPeerInitiatedStreamID: HTTP2StreamID
  79. /// The maximum number of concurrent streams we are allowed to operate.
  80. var maxConcurrentStreams: Int
  81. /// A task which, when fired, will idle the connection.
  82. var idleTask: Scheduled<Void>
  83. fileprivate init(fromOperating state: Operating, idleTask: Scheduled<Void>) {
  84. // We won't transition to this state unless we've seen a SETTINGS frame.
  85. assert(state.hasSeenSettings)
  86. self.role = state.role
  87. self.lastPeerInitiatedStreamID = state.lastPeerInitiatedStreamID
  88. self.maxConcurrentStreams = state.maxConcurrentStreams
  89. self.idleTask = idleTask
  90. }
  91. }
  92. /// The quiescing state is entered only from the operating state. It may be entered if we receive
  93. /// a GOAWAY frame (the remote peer initiated the quiescing) or we initiate graceful shutdown
  94. /// locally.
  95. fileprivate struct Quiescing: TracksOpenStreams, CanCloseStreams {
  96. /// Our role in the connection.
  97. var role: Role
  98. /// The number of open stream.
  99. var openStreams: Int
  100. /// The last stream ID initiated by the remote peer.
  101. var lastPeerInitiatedStreamID: HTTP2StreamID
  102. /// The maximum number of concurrent streams we are allowed to operate.
  103. var maxConcurrentStreams: Int
  104. /// Whether this peer initiated shutting down.
  105. var initiatedByUs: Bool
  106. fileprivate init(fromOperating state: Operating, initiatedByUs: Bool) {
  107. // If we didn't initiate shutdown, the remote peer must have done so by sending a GOAWAY frame
  108. // in which case we must have seen a SETTINGS frame.
  109. assert(initiatedByUs || state.hasSeenSettings)
  110. self.role = state.role
  111. self.initiatedByUs = initiatedByUs
  112. self.openStreams = state.openStreams
  113. self.lastPeerInitiatedStreamID = state.lastPeerInitiatedStreamID
  114. self.maxConcurrentStreams = state.maxConcurrentStreams
  115. }
  116. }
  117. /// The closing state is entered when one of the previous states initiates a connection closure.
  118. /// From this state the only possible transition is to the closed state.
  119. fileprivate struct Closing {
  120. /// Our role in the connection.
  121. var role: Role
  122. /// Should the client connection manager receive an idle event when we close? (If not then it
  123. /// will attempt to establish a new connection immediately.)
  124. var shouldIdle: Bool
  125. fileprivate init(fromOperating state: Operating) {
  126. self.role = state.role
  127. // Idle if there are no open streams and we've seen the first SETTINGS frame.
  128. self.shouldIdle = !state.hasOpenStreams && state.hasSeenSettings
  129. }
  130. fileprivate init(fromQuiescing state: Quiescing) {
  131. self.role = state.role
  132. // If we initiated the quiescing then we shouldn't go idle (we want to shutdown instead).
  133. self.shouldIdle = !state.initiatedByUs
  134. }
  135. fileprivate init(fromWaitingToIdle state: WaitingToIdle, shouldIdle: Bool = true) {
  136. self.role = state.role
  137. self.shouldIdle = shouldIdle
  138. }
  139. }
  140. fileprivate enum State {
  141. case operating(Operating)
  142. case waitingToIdle(WaitingToIdle)
  143. case quiescing(Quiescing)
  144. case closing(Closing)
  145. case closed
  146. }
  147. /// The set of operations that should be performed as a result of interaction with the state
  148. /// machine.
  149. struct Operations {
  150. /// An event to notify the connection manager about.
  151. private(set) var connectionManagerEvent: ConnectionManagerEvent?
  152. /// The value of HTTP/2 SETTINGS_MAX_CONCURRENT_STREAMS changed.
  153. private(set) var maxConcurrentStreamsChange: Int?
  154. /// An idle task, either scheduling or cancelling an idle timeout.
  155. private(set) var idleTask: IdleTask?
  156. /// Send a GOAWAY frame with the last peer initiated stream ID set to this value.
  157. private(set) var sendGoAwayWithLastPeerInitiatedStreamID: HTTP2StreamID?
  158. /// Whether the channel should be closed.
  159. private(set) var shouldCloseChannel: Bool
  160. /// Whether a ping should be sent after a GOAWAY frame.
  161. private(set) var shouldPingAfterGoAway: Bool
  162. fileprivate static let none = Operations()
  163. fileprivate mutating func sendGoAwayFrame(
  164. lastPeerInitiatedStreamID streamID: HTTP2StreamID,
  165. followWithPing: Bool = false
  166. ) {
  167. self.sendGoAwayWithLastPeerInitiatedStreamID = streamID
  168. self.shouldPingAfterGoAway = followWithPing
  169. }
  170. fileprivate mutating func cancelIdleTask(_ task: Scheduled<Void>) {
  171. self.idleTask = .cancel(task)
  172. }
  173. fileprivate mutating func scheduleIdleTask() {
  174. self.idleTask = .schedule
  175. }
  176. fileprivate mutating func closeChannel() {
  177. self.shouldCloseChannel = true
  178. }
  179. fileprivate mutating func notifyConnectionManager(about event: ConnectionManagerEvent) {
  180. self.connectionManagerEvent = event
  181. }
  182. fileprivate mutating func maxConcurrentStreamsChanged(_ newValue: Int) {
  183. self.maxConcurrentStreamsChange = newValue
  184. }
  185. private init() {
  186. self.connectionManagerEvent = nil
  187. self.idleTask = nil
  188. self.sendGoAwayWithLastPeerInitiatedStreamID = nil
  189. self.shouldCloseChannel = false
  190. self.shouldPingAfterGoAway = false
  191. }
  192. }
  193. /// An event to notify the 'ConnectionManager' about.
  194. enum ConnectionManagerEvent {
  195. case inactive
  196. case idle
  197. case ready
  198. case quiescing
  199. }
  200. enum IdleTask {
  201. case schedule
  202. case cancel(Scheduled<Void>)
  203. }
  204. /// The current state.
  205. private var state: State
  206. /// A logger.
  207. internal var logger: Logger
  208. /// Create a new state machine.
  209. init(role: Role, logger: Logger) {
  210. self.state = .operating(.init(role: role))
  211. self.logger = logger
  212. }
  213. // MARK: Stream Events
  214. /// An HTTP/2 stream was created.
  215. mutating func streamCreated(withID streamID: HTTP2StreamID) -> Operations {
  216. var operations: Operations = .none
  217. switch self.state {
  218. case var .operating(state):
  219. // Create the stream.
  220. state.streamCreated(streamID, logger: self.logger)
  221. self.state = .operating(state)
  222. case let .waitingToIdle(state):
  223. var operating = Operating(fromWaitingToIdle: state)
  224. operating.streamCreated(streamID, logger: self.logger)
  225. self.state = .operating(operating)
  226. operations.cancelIdleTask(state.idleTask)
  227. case var .quiescing(state):
  228. state.lastPeerInitiatedStreamID = streamID
  229. state.openStreams += 1
  230. self.state = .quiescing(state)
  231. case .closing, .closed:
  232. ()
  233. }
  234. return operations
  235. }
  236. /// An HTTP/2 stream was closed.
  237. mutating func streamClosed(withID streamID: HTTP2StreamID) -> Operations {
  238. var operations: Operations = .none
  239. switch self.state {
  240. case var .operating(state):
  241. state.streamClosed(streamID, logger: self.logger)
  242. if state.hasSeenSettings, !state.hasOpenStreams {
  243. operations.scheduleIdleTask()
  244. }
  245. self.state = .operating(state)
  246. case .waitingToIdle:
  247. // If we're waiting to idle then there can't be any streams open which can be closed.
  248. preconditionFailure()
  249. case var .quiescing(state):
  250. state.streamClosed(streamID, logger: self.logger)
  251. if state.hasOpenStreams {
  252. self.state = .quiescing(state)
  253. } else {
  254. self.state = .closing(.init(fromQuiescing: state))
  255. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  256. operations.closeChannel()
  257. }
  258. case .closing, .closed:
  259. ()
  260. }
  261. return operations
  262. }
  263. // MARK: - Idle Events
  264. /// The given task was scheduled to idle the connection.
  265. mutating func scheduledIdleTimeoutTask(_ task: Scheduled<Void>) -> Operations {
  266. var operations: Operations = .none
  267. switch self.state {
  268. case let .operating(state):
  269. if state.hasOpenStreams {
  270. operations.cancelIdleTask(task)
  271. } else {
  272. self.state = .waitingToIdle(.init(fromOperating: state, idleTask: task))
  273. }
  274. case .waitingToIdle:
  275. // There's already an idle task.
  276. preconditionFailure()
  277. case .quiescing, .closing, .closed:
  278. operations.cancelIdleTask(task)
  279. }
  280. return operations
  281. }
  282. /// The idle timeout task fired, the connection should be idled.
  283. mutating func idleTimeoutTaskFired() -> Operations {
  284. var operations: Operations = .none
  285. switch self.state {
  286. case let .waitingToIdle(state):
  287. self.state = .closing(.init(fromWaitingToIdle: state))
  288. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  289. operations.closeChannel()
  290. // We're either operating on streams, streams are going away, or the connection is going away
  291. // so we don't need to idle the connection.
  292. case .operating, .quiescing, .closing, .closed:
  293. ()
  294. }
  295. return operations
  296. }
  297. // MARK: - Shutdown Events
  298. /// Close the connection, this can be caused as a result of a keepalive timeout (i.e. the server
  299. /// has become unresponsive), we'll bin this connection as a result.
  300. mutating func shutdownNow() -> Operations {
  301. var operations = Operations.none
  302. switch self.state {
  303. case let .operating(state):
  304. var closing = Closing(fromOperating: state)
  305. closing.shouldIdle = false
  306. self.state = .closing(closing)
  307. operations.closeChannel()
  308. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  309. case let .waitingToIdle(state):
  310. // Don't idle.
  311. self.state = .closing(Closing(fromWaitingToIdle: state, shouldIdle: false))
  312. operations.closeChannel()
  313. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  314. operations.cancelIdleTask(state.idleTask)
  315. case let .quiescing(state):
  316. self.state = .closing(Closing(fromQuiescing: state))
  317. // We've already sent a GOAWAY frame if we're in this state, just close.
  318. operations.closeChannel()
  319. case .closing, .closed:
  320. ()
  321. }
  322. return operations
  323. }
  324. /// Initiate a graceful shutdown of this connection, that is, begin quiescing.
  325. mutating func initiateGracefulShutdown() -> Operations {
  326. var operations: Operations = .none
  327. switch self.state {
  328. case let .operating(state):
  329. if state.hasOpenStreams {
  330. // There are open streams: send a GOAWAY frame and wait for the stream count to reach zero.
  331. //
  332. // It's okay if we haven't seen a SETTINGS frame at this point; we've initiated the shutdown
  333. // so making a connection is ready isn't necessary.
  334. operations.notifyConnectionManager(about: .quiescing)
  335. // TODO: we should ratchet down the last initiated stream after 1-RTT.
  336. //
  337. // As a client we will just stop initiating streams.
  338. if state.role == .server {
  339. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  340. }
  341. self.state = .quiescing(.init(fromOperating: state, initiatedByUs: true))
  342. } else {
  343. // No open streams: send a GOAWAY frame and close the channel.
  344. self.state = .closing(.init(fromOperating: state))
  345. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  346. operations.closeChannel()
  347. }
  348. case let .waitingToIdle(state):
  349. // There can't be any open streams, but we have a few loose ends to clear up: we need to
  350. // cancel the idle timeout, send a GOAWAY frame and then close. We don't want to idle from the
  351. // closing state: we want to shutdown instead.
  352. self.state = .closing(.init(fromWaitingToIdle: state, shouldIdle: false))
  353. operations.cancelIdleTask(state.idleTask)
  354. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  355. operations.closeChannel()
  356. case var .quiescing(state):
  357. // We're already quiescing: either the remote initiated it or we're initiating it more than
  358. // once. Set ourselves as the initiator to ensure we don't idle when we eventually close, this
  359. // is important for the client: if the server initiated this then we establish a new
  360. // connection when we close, unless we also initiated shutdown.
  361. state.initiatedByUs = true
  362. self.state = .quiescing(state)
  363. case var .closing(state):
  364. // We've already called 'close()', make sure we don't go idle.
  365. state.shouldIdle = false
  366. self.state = .closing(state)
  367. case .closed:
  368. ()
  369. }
  370. return operations
  371. }
  372. /// We've received a GOAWAY frame from the remote peer. Either the remote peer wants to close the
  373. /// connection or they're responding to us shutting down the connection.
  374. mutating func receiveGoAway() -> Operations {
  375. var operations: Operations = .none
  376. switch self.state {
  377. case let .operating(state):
  378. // A SETTINGS frame MUST follow the connection preface. (RFC 7540 § 3.5)
  379. assert(state.hasSeenSettings)
  380. if state.hasOpenStreams {
  381. operations.notifyConnectionManager(about: .quiescing)
  382. switch state.role {
  383. case .client:
  384. // The server sent us a GOAWAY we'll just stop opening new streams and will send a GOAWAY
  385. // frame before we close later.
  386. ()
  387. case .server:
  388. // Client sent us a GOAWAY frame; we'll let the streams drain and then close. We'll tell
  389. // the client that we're going away and send them a ping. When we receive the pong we will
  390. // send another GOAWAY frame with a lower stream ID. In this case, the pong acts as an ack
  391. // for the GOAWAY.
  392. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: .maxID, followWithPing: true)
  393. }
  394. self.state = .quiescing(.init(fromOperating: state, initiatedByUs: false))
  395. } else {
  396. // No open streams, we can close as well.
  397. self.state = .closing(.init(fromOperating: state))
  398. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  399. operations.closeChannel()
  400. }
  401. case let .waitingToIdle(state):
  402. // There can't be any open streams, but we have a few loose ends to clear up: we need to
  403. // cancel the idle timeout, send a GOAWAY frame and then close.
  404. self.state = .closing(.init(fromWaitingToIdle: state))
  405. operations.cancelIdleTask(state.idleTask)
  406. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: state.lastPeerInitiatedStreamID)
  407. operations.closeChannel()
  408. case .quiescing:
  409. // We're already quiescing, this changes nothing.
  410. ()
  411. case .closing, .closed:
  412. // We're already closing/closed (so must have emitted a GOAWAY frame already). Ignore this.
  413. ()
  414. }
  415. return operations
  416. }
  417. mutating func ratchetDownGoAwayStreamID() -> Operations {
  418. var operations: Operations = .none
  419. switch self.state {
  420. case let .quiescing(state):
  421. let streamID = state.lastPeerInitiatedStreamID
  422. operations.sendGoAwayFrame(lastPeerInitiatedStreamID: streamID)
  423. case .operating, .waitingToIdle, .closing, .closed:
  424. // We can only need to ratchet down the stream ID if we're already quiescing.
  425. ()
  426. }
  427. return operations
  428. }
  429. mutating func receiveSettings(_ settings: HTTP2Settings) -> Operations {
  430. // Log the change in settings.
  431. self.logger.debug(
  432. "HTTP2 settings update",
  433. metadata: Dictionary(
  434. settings.map {
  435. ("\($0.parameter.loggingMetadataKey)", "\($0.value)")
  436. },
  437. uniquingKeysWith: { a, _ in a }
  438. )
  439. )
  440. var operations: Operations = .none
  441. switch self.state {
  442. case var .operating(state):
  443. let hasSeenSettingsPreviously = state.hasSeenSettings
  444. // If we hadn't previously seen settings then we need to notify the client connection manager
  445. // that we're now ready.
  446. if !hasSeenSettingsPreviously {
  447. operations.notifyConnectionManager(about: .ready)
  448. state.hasSeenSettings = true
  449. // Now that we know the connection is ready, we may want to start an idle timeout as well.
  450. if !state.hasOpenStreams {
  451. operations.scheduleIdleTask()
  452. }
  453. }
  454. // Update max concurrent streams.
  455. if let maxStreams = settings.last(where: { $0.parameter == .maxConcurrentStreams })?.value {
  456. operations.maxConcurrentStreamsChanged(maxStreams)
  457. state.maxConcurrentStreams = maxStreams
  458. } else if !hasSeenSettingsPreviously {
  459. // We hadn't seen settings before now and max concurrent streams wasn't set we should assume
  460. // the default and emit an update.
  461. operations.maxConcurrentStreamsChanged(100)
  462. state.maxConcurrentStreams = 100
  463. }
  464. self.state = .operating(state)
  465. case var .waitingToIdle(state):
  466. // Update max concurrent streams.
  467. if let maxStreams = settings.last(where: { $0.parameter == .maxConcurrentStreams })?.value {
  468. operations.maxConcurrentStreamsChanged(maxStreams)
  469. state.maxConcurrentStreams = maxStreams
  470. }
  471. self.state = .waitingToIdle(state)
  472. case .quiescing, .closing, .closed:
  473. ()
  474. }
  475. return operations
  476. }
  477. // MARK: - Channel Events
  478. // (Other channel events aren't included here as they don't impact the state machine.)
  479. /// 'channelActive' was called in the idle handler holding this state machine.
  480. mutating func channelInactive() -> Operations {
  481. var operations: Operations = .none
  482. switch self.state {
  483. case let .operating(state):
  484. self.state = .closed
  485. // We unexpectedly became inactive.
  486. if !state.hasSeenSettings || state.hasOpenStreams {
  487. // Haven't seen settings, or we've seen settings and there are open streams.
  488. operations.notifyConnectionManager(about: .inactive)
  489. } else {
  490. // Have seen settings and there are no open streams.
  491. operations.notifyConnectionManager(about: .idle)
  492. }
  493. case let .waitingToIdle(state):
  494. self.state = .closed
  495. // We were going to idle anyway.
  496. operations.notifyConnectionManager(about: .idle)
  497. operations.cancelIdleTask(state.idleTask)
  498. case let .quiescing(state):
  499. self.state = .closed
  500. if state.initiatedByUs || state.hasOpenStreams {
  501. operations.notifyConnectionManager(about: .inactive)
  502. } else {
  503. operations.notifyConnectionManager(about: .idle)
  504. }
  505. case let .closing(state):
  506. self.state = .closed
  507. if state.shouldIdle {
  508. operations.notifyConnectionManager(about: .idle)
  509. } else {
  510. operations.notifyConnectionManager(about: .inactive)
  511. }
  512. case .closed:
  513. ()
  514. }
  515. return operations
  516. }
  517. }
  518. // MARK: - Helper Protocols
  519. private protocol TracksOpenStreams {
  520. /// The number of open streams.
  521. var openStreams: Int { get set }
  522. }
  523. extension TracksOpenStreams {
  524. /// Whether any streams are open.
  525. fileprivate var hasOpenStreams: Bool {
  526. return self.openStreams != 0
  527. }
  528. }
  529. private protocol CanOpenStreams: TracksOpenStreams {
  530. /// The role of this peer in the connection.
  531. var role: GRPCIdleHandlerStateMachine.Role { get }
  532. /// The ID of the stream most recently initiated by the remote peer.
  533. var lastPeerInitiatedStreamID: HTTP2StreamID { get set }
  534. /// The maximum number of concurrent streams.
  535. var maxConcurrentStreams: Int { get set }
  536. mutating func streamCreated(_ streamID: HTTP2StreamID, logger: Logger)
  537. }
  538. extension CanOpenStreams {
  539. fileprivate mutating func streamCreated(_ streamID: HTTP2StreamID, logger: Logger) {
  540. self.openStreams += 1
  541. switch self.role {
  542. case .client where streamID.isServerInitiated:
  543. self.lastPeerInitiatedStreamID = streamID
  544. case .server where streamID.isClientInitiated:
  545. self.lastPeerInitiatedStreamID = streamID
  546. default:
  547. ()
  548. }
  549. logger.debug(
  550. "HTTP2 stream created",
  551. metadata: [
  552. MetadataKey.h2StreamID: "\(streamID)",
  553. MetadataKey.h2ActiveStreams: "\(self.openStreams)",
  554. ]
  555. )
  556. if self.openStreams == self.maxConcurrentStreams {
  557. logger.warning(
  558. "HTTP2 max concurrent stream limit reached",
  559. metadata: [
  560. MetadataKey.h2ActiveStreams: "\(self.openStreams)"
  561. ]
  562. )
  563. }
  564. }
  565. }
  566. private protocol CanCloseStreams: TracksOpenStreams {
  567. /// Notes that a stream has closed.
  568. mutating func streamClosed(_ streamID: HTTP2StreamID, logger: Logger)
  569. }
  570. extension CanCloseStreams {
  571. fileprivate mutating func streamClosed(_ streamID: HTTP2StreamID, logger: Logger) {
  572. self.openStreams -= 1
  573. logger.debug(
  574. "HTTP2 stream closed",
  575. metadata: [
  576. MetadataKey.h2StreamID: "\(streamID)",
  577. MetadataKey.h2ActiveStreams: "\(self.openStreams)",
  578. ]
  579. )
  580. }
  581. }