ConnectionPool.swift 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*
  2. * Copyright 2021, 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 Atomics
  17. import Logging
  18. import NIOConcurrencyHelpers
  19. import NIOCore
  20. import NIOHTTP2
  21. @usableFromInline
  22. internal final class ConnectionPool {
  23. /// The event loop all connections in this pool are running on.
  24. @usableFromInline
  25. internal let eventLoop: EventLoop
  26. @usableFromInline
  27. internal enum State {
  28. case active
  29. case shuttingDown(EventLoopFuture<Void>)
  30. case shutdown
  31. }
  32. /// The state of the connection pool.
  33. @usableFromInline
  34. internal var _state: State = .active
  35. /// The most recent connection error we have observed.
  36. ///
  37. /// This error is used to provide additional context to failed waiters. A waiter may, for example,
  38. /// timeout because the pool is busy, or because no connection can be established because of an
  39. /// underlying connection error. In the latter case it's useful for the caller to know why the
  40. /// connection is failing at the RPC layer.
  41. ///
  42. /// This value is cleared when a connection becomes 'available'. That is, when we receive an
  43. /// http/2 SETTINGS frame.
  44. ///
  45. /// This value is set whenever an underlying connection transitions to the transient failure state
  46. /// or to the idle state and has an associated error.
  47. @usableFromInline
  48. internal var _mostRecentError: Error? = nil
  49. /// Connection managers and their stream availability state keyed by the ID of the connection
  50. /// manager.
  51. ///
  52. /// Connections are accessed by their ID for connection state changes (infrequent) and when
  53. /// streams are closed (frequent). However when choosing which connection to succeed a waiter
  54. /// with (frequent) requires the connections to be ordered by their availability. A dictionary
  55. /// might not be the most efficient data structure (a queue prioritised by stream availability may
  56. /// be a better choice given the number of connections is likely to be very low in practice).
  57. @usableFromInline
  58. internal var _connections: [ConnectionManagerID: PerConnectionState]
  59. /// The threshold which if exceeded when creating a stream determines whether the pool will
  60. /// start connecting an idle connection (if one exists).
  61. ///
  62. /// The 'load' is calculated as the ratio of demand for streams (the sum of the number of waiters
  63. /// and the number of reserved streams) and the total number of streams which non-idle connections
  64. /// could support (this includes the streams that a connection in the connecting state could
  65. /// support).
  66. @usableFromInline
  67. internal let reservationLoadThreshold: Double
  68. /// The assumed value for the maximum number of concurrent streams a connection can support. We
  69. /// assume a connection will support this many streams until we know better.
  70. @usableFromInline
  71. internal let assumedMaxConcurrentStreams: Int
  72. /// A queue of waiters which may or may not get a stream in the future.
  73. @usableFromInline
  74. internal var waiters: CircularBuffer<Waiter>
  75. /// The maximum number of waiters allowed, the size of `waiters` must not exceed this value. If
  76. /// there are this many waiters in the queue then the next waiter will be failed immediately.
  77. @usableFromInline
  78. internal let maxWaiters: Int
  79. /// The number of connections in the pool that should always be kept open (i.e. they won't go idle).
  80. /// In other words, it's the number of connections for which we should ignore idle timers.
  81. @usableFromInline
  82. internal let minConnections: Int
  83. /// Configuration for backoff between subsequence connection attempts.
  84. @usableFromInline
  85. internal let connectionBackoff: ConnectionBackoff
  86. /// Provides a channel factory to the `ConnectionManager`.
  87. @usableFromInline
  88. internal let channelProvider: ConnectionManagerChannelProvider
  89. /// The object to notify about changes to stream reservations; in practice this is usually
  90. /// the `PoolManager`.
  91. @usableFromInline
  92. internal let streamLender: StreamLender
  93. @usableFromInline
  94. internal var delegate: GRPCConnectionPoolDelegate?
  95. /// A logger.
  96. @usableFromInline
  97. internal let logger: Logger
  98. /// Returns `NIODeadline` representing 'now'. This is useful for testing.
  99. @usableFromInline
  100. internal let now: () -> NIODeadline
  101. /// The ID of this sub-pool.
  102. @usableFromInline
  103. internal let id: GRPCSubPoolID
  104. /// Logging metadata keys.
  105. @usableFromInline
  106. internal enum Metadata {
  107. /// The ID of this pool.
  108. @usableFromInline
  109. static let id = "pool.id"
  110. /// The number of stream reservations (i.e. number of open streams + number of waiters).
  111. @usableFromInline
  112. static let reservationsCount = "pool.reservations.count"
  113. /// The number of streams this pool can support with non-idle connections at this time.
  114. @usableFromInline
  115. static let reservationsCapacity = "pool.reservations.capacity"
  116. /// The current reservation load (i.e. reservation count / reservation capacity)
  117. @usableFromInline
  118. static let reservationsLoad = "pool.reservations.load"
  119. /// The reservation load threshold, above which a new connection will be created (if possible).
  120. @usableFromInline
  121. static let reservationsLoadThreshold = "pool.reservations.loadThreshold"
  122. /// The current number of waiters in the pool.
  123. @usableFromInline
  124. static let waitersCount = "pool.waiters.count"
  125. /// The maximum number of waiters the pool is configured to allow.
  126. @usableFromInline
  127. static let waitersMax = "pool.waiters.max"
  128. /// The number of waiters which were successfully serviced.
  129. @usableFromInline
  130. static let waitersServiced = "pool.waiters.serviced"
  131. /// The ID of waiter.
  132. @usableFromInline
  133. static let waiterID = "pool.waiter.id"
  134. /// The maximum number of connections allowed in the pool.
  135. @usableFromInline
  136. static let connectionsMax = "pool.connections.max"
  137. /// The number of connections in the ready state.
  138. @usableFromInline
  139. static let connectionsReady = "pool.connections.ready"
  140. /// The number of connections in the connecting state.
  141. @usableFromInline
  142. static let connectionsConnecting = "pool.connections.connecting"
  143. /// The number of connections in the transient failure state.
  144. @usableFromInline
  145. static let connectionsTransientFailure = "pool.connections.transientFailure"
  146. }
  147. @usableFromInline
  148. init(
  149. eventLoop: EventLoop,
  150. maxWaiters: Int,
  151. minConnections: Int,
  152. reservationLoadThreshold: Double,
  153. assumedMaxConcurrentStreams: Int,
  154. connectionBackoff: ConnectionBackoff,
  155. channelProvider: ConnectionManagerChannelProvider,
  156. streamLender: StreamLender,
  157. delegate: GRPCConnectionPoolDelegate?,
  158. logger: Logger,
  159. now: @escaping () -> NIODeadline = NIODeadline.now
  160. ) {
  161. precondition(
  162. (0.0 ... 1.0).contains(reservationLoadThreshold),
  163. "reservationLoadThreshold must be within the range 0.0 ... 1.0"
  164. )
  165. self.reservationLoadThreshold = reservationLoadThreshold
  166. self.assumedMaxConcurrentStreams = assumedMaxConcurrentStreams
  167. self._connections = [:]
  168. self.maxWaiters = maxWaiters
  169. self.minConnections = minConnections
  170. self.waiters = CircularBuffer(initialCapacity: 16)
  171. self.eventLoop = eventLoop
  172. self.connectionBackoff = connectionBackoff
  173. self.channelProvider = channelProvider
  174. self.streamLender = streamLender
  175. self.delegate = delegate
  176. self.now = now
  177. let id = GRPCSubPoolID.next()
  178. var logger = logger
  179. logger[metadataKey: Metadata.id] = "\(id)"
  180. self.id = id
  181. self.logger = logger
  182. }
  183. /// Initialize the connection pool.
  184. ///
  185. /// - Parameter connections: The number of connections to add to the pool.
  186. internal func initialize(connections: Int) {
  187. assert(self._connections.isEmpty)
  188. self.logger.debug(
  189. "initializing new sub-pool",
  190. metadata: [
  191. Metadata.waitersMax: .stringConvertible(self.maxWaiters),
  192. Metadata.connectionsMax: .stringConvertible(connections),
  193. ]
  194. )
  195. self._connections.reserveCapacity(connections)
  196. var numberOfKeepOpenConnections = self.minConnections
  197. while self._connections.count < connections {
  198. // If we have less than the minimum number of connections, don't let
  199. // the new connection close when idle.
  200. let idleBehavior =
  201. numberOfKeepOpenConnections > 0
  202. ? ConnectionManager.IdleBehavior.neverGoIdle : .closeWhenIdleTimeout
  203. numberOfKeepOpenConnections -= 1
  204. self.addConnectionToPool(idleBehavior: idleBehavior)
  205. }
  206. }
  207. /// Make and add a new connection to the pool.
  208. private func addConnectionToPool(idleBehavior: ConnectionManager.IdleBehavior) {
  209. let manager = ConnectionManager(
  210. eventLoop: self.eventLoop,
  211. channelProvider: self.channelProvider,
  212. callStartBehavior: .waitsForConnectivity,
  213. idleBehavior: idleBehavior,
  214. connectionBackoff: self.connectionBackoff,
  215. connectivityDelegate: self,
  216. http2Delegate: self,
  217. logger: self.logger
  218. )
  219. let id = manager.id
  220. self._connections[id] = PerConnectionState(manager: manager)
  221. self.delegate?.connectionAdded(id: .init(id))
  222. // If it's one of the connections that should be kept open, then connect
  223. // straight away.
  224. switch idleBehavior {
  225. case .neverGoIdle:
  226. self.eventLoop.execute {
  227. if manager.sync.isIdle {
  228. manager.sync.startConnecting()
  229. }
  230. }
  231. case .closeWhenIdleTimeout:
  232. ()
  233. }
  234. }
  235. // MARK: - Called from the pool manager
  236. /// Make and initialize an HTTP/2 stream `Channel`.
  237. ///
  238. /// - Parameters:
  239. /// - deadline: The point in time by which the `promise` must have been resolved.
  240. /// - promise: A promise for a `Channel`.
  241. /// - logger: A request logger.
  242. /// - initializer: A closure to initialize the `Channel` with.
  243. @inlinable
  244. internal func makeStream(
  245. deadline: NIODeadline,
  246. promise: EventLoopPromise<Channel>,
  247. logger: Logger,
  248. initializer: @escaping @Sendable (Channel) -> EventLoopFuture<Void>
  249. ) {
  250. if self.eventLoop.inEventLoop {
  251. self._makeStream(
  252. deadline: deadline,
  253. promise: promise,
  254. logger: logger,
  255. initializer: initializer
  256. )
  257. } else {
  258. self.eventLoop.execute {
  259. self._makeStream(
  260. deadline: deadline,
  261. promise: promise,
  262. logger: logger,
  263. initializer: initializer
  264. )
  265. }
  266. }
  267. }
  268. /// See `makeStream(deadline:promise:logger:initializer:)`.
  269. @inlinable
  270. internal func makeStream(
  271. deadline: NIODeadline,
  272. logger: Logger,
  273. initializer: @escaping @Sendable (Channel) -> EventLoopFuture<Void>
  274. ) -> EventLoopFuture<Channel> {
  275. let promise = self.eventLoop.makePromise(of: Channel.self)
  276. self.makeStream(deadline: deadline, promise: promise, logger: logger, initializer: initializer)
  277. return promise.futureResult
  278. }
  279. /// Shutdown the connection pool.
  280. ///
  281. /// Existing waiters will be failed and all underlying connections will be shutdown. Subsequent
  282. /// calls to `makeStream` will be failed immediately.
  283. ///
  284. /// - Parameter mode: The mode to use when shutting down.
  285. /// - Returns: A future indicated when shutdown has been completed.
  286. internal func shutdown(mode: ConnectionManager.ShutdownMode) -> EventLoopFuture<Void> {
  287. let promise = self.eventLoop.makePromise(of: Void.self)
  288. if self.eventLoop.inEventLoop {
  289. self._shutdown(mode: mode, promise: promise)
  290. } else {
  291. self.eventLoop.execute {
  292. self._shutdown(mode: mode, promise: promise)
  293. }
  294. }
  295. return promise.futureResult
  296. }
  297. /// See `makeStream(deadline:promise:logger:initializer:)`.
  298. ///
  299. /// - Important: Must be called on the pool's `EventLoop`.
  300. @inlinable
  301. internal func _makeStream(
  302. deadline: NIODeadline,
  303. promise: EventLoopPromise<Channel>,
  304. logger: Logger,
  305. initializer: @escaping @Sendable (Channel) -> EventLoopFuture<Void>
  306. ) {
  307. self.eventLoop.assertInEventLoop()
  308. guard case .active = self._state else {
  309. // Fail the promise right away if we're shutting down or already shut down.
  310. promise.fail(GRPCConnectionPoolError.shutdown)
  311. return
  312. }
  313. // Try to make a stream on an existing connection.
  314. let streamCreated = self._tryMakeStream(promise: promise, initializer: initializer)
  315. if !streamCreated {
  316. // No stream was created, wait for one.
  317. self._enqueueWaiter(
  318. deadline: deadline,
  319. promise: promise,
  320. logger: logger,
  321. initializer: initializer
  322. )
  323. }
  324. }
  325. /// Try to find an existing connection on which we can make a stream.
  326. ///
  327. /// - Parameters:
  328. /// - promise: A promise to succeed if we can make a stream.
  329. /// - initializer: A closure to initialize the stream with.
  330. /// - Returns: A boolean value indicating whether the stream was created or not.
  331. @inlinable
  332. internal func _tryMakeStream(
  333. promise: EventLoopPromise<Channel>,
  334. initializer: @escaping @Sendable (Channel) -> EventLoopFuture<Void>
  335. ) -> Bool {
  336. // We shouldn't jump the queue.
  337. guard self.waiters.isEmpty else {
  338. return false
  339. }
  340. // Reserve a stream, if we can.
  341. guard let multiplexer = self._reserveStreamFromMostAvailableConnection() else {
  342. return false
  343. }
  344. multiplexer.createStreamChannel(promise: promise, initializer)
  345. // Has reserving another stream tipped us over the limit for needing another connection?
  346. if self._shouldBringUpAnotherConnection() {
  347. self._startConnectingIdleConnection()
  348. }
  349. return true
  350. }
  351. /// Enqueue a waiter to be provided with a stream at some point in the future.
  352. ///
  353. /// - Parameters:
  354. /// - deadline: The point in time by which the promise should have been completed.
  355. /// - promise: The promise to complete with the `Channel`.
  356. /// - logger: A logger.
  357. /// - initializer: A closure to initialize the `Channel` with.
  358. @inlinable
  359. internal func _enqueueWaiter(
  360. deadline: NIODeadline,
  361. promise: EventLoopPromise<Channel>,
  362. logger: Logger,
  363. initializer: @escaping @Sendable (Channel) -> EventLoopFuture<Void>
  364. ) {
  365. // Don't overwhelm the pool with too many waiters.
  366. guard self.waiters.count < self.maxWaiters else {
  367. logger.trace(
  368. "connection pool has too many waiters",
  369. metadata: [
  370. Metadata.waitersMax: .stringConvertible(self.maxWaiters)
  371. ]
  372. )
  373. promise.fail(GRPCConnectionPoolError.tooManyWaiters(connectionError: self._mostRecentError))
  374. return
  375. }
  376. let waiter = Waiter(deadline: deadline, promise: promise, channelInitializer: initializer)
  377. // Fail the waiter and punt it from the queue when it times out. It's okay that we schedule the
  378. // timeout before appending it to the waiters, it wont run until the next event loop tick at the
  379. // earliest (even if the deadline has already passed).
  380. waiter.scheduleTimeout(on: self.eventLoop) {
  381. waiter.fail(GRPCConnectionPoolError.deadlineExceeded(connectionError: self._mostRecentError))
  382. if let index = self.waiters.firstIndex(where: { $0.id == waiter.id }) {
  383. self.waiters.remove(at: index)
  384. logger.trace(
  385. "timed out waiting for a connection",
  386. metadata: [
  387. Metadata.waiterID: "\(waiter.id)",
  388. Metadata.waitersCount: .stringConvertible(self.waiters.count),
  389. ]
  390. )
  391. }
  392. }
  393. // request logger
  394. logger.debug(
  395. "waiting for a connection to become available",
  396. metadata: [
  397. Metadata.waiterID: "\(waiter.id)",
  398. Metadata.waitersCount: .stringConvertible(self.waiters.count),
  399. ]
  400. )
  401. self.waiters.append(waiter)
  402. // pool logger
  403. self.logger.trace(
  404. "enqueued connection waiter",
  405. metadata: [
  406. Metadata.waitersCount: .stringConvertible(self.waiters.count)
  407. ]
  408. )
  409. if self._shouldBringUpAnotherConnection() {
  410. self._startConnectingIdleConnection()
  411. }
  412. }
  413. /// Compute the current demand and capacity for streams.
  414. ///
  415. /// The 'demand' for streams is the number of reserved streams and the number of waiters. The
  416. /// capacity for streams is the product of max concurrent streams and the number of non-idle
  417. /// connections.
  418. ///
  419. /// - Returns: A tuple of the demand and capacity for streams.
  420. @usableFromInline
  421. internal func _computeStreamDemandAndCapacity() -> (demand: Int, capacity: Int) {
  422. let demand = self.sync.reservedStreams + self.sync.waiters
  423. // TODO: make this cheaper by storing and incrementally updating the number of idle connections
  424. let capacity = self._connections.values.reduce(0) { sum, state in
  425. if state.manager.sync.isIdle || state.isQuiescing {
  426. // Idle connection or quiescing (so the capacity should be ignored).
  427. return sum
  428. } else if let knownMaxAvailableStreams = state.maxAvailableStreams {
  429. // A known value of max concurrent streams, i.e. the connection is active.
  430. return sum + knownMaxAvailableStreams
  431. } else {
  432. // Not idle and no known value, the connection must be connecting so use our assumed value.
  433. return sum + self.assumedMaxConcurrentStreams
  434. }
  435. }
  436. return (demand, capacity)
  437. }
  438. /// Returns whether the pool should start connecting an idle connection (if one exists).
  439. @usableFromInline
  440. internal func _shouldBringUpAnotherConnection() -> Bool {
  441. let (demand, capacity) = self._computeStreamDemandAndCapacity()
  442. // Infinite -- i.e. all connections are idle or no connections exist -- is okay here as it
  443. // will always be greater than the threshold and a new connection will be spun up.
  444. let load = Double(demand) / Double(capacity)
  445. let loadExceedsThreshold = load >= self.reservationLoadThreshold
  446. if loadExceedsThreshold {
  447. self.logger.debug(
  448. "stream reservation load factor greater than or equal to threshold, bringing up additional connection if available",
  449. metadata: [
  450. Metadata.reservationsCount: .stringConvertible(demand),
  451. Metadata.reservationsCapacity: .stringConvertible(capacity),
  452. Metadata.reservationsLoad: .stringConvertible(load),
  453. Metadata.reservationsLoadThreshold: .stringConvertible(self.reservationLoadThreshold),
  454. ]
  455. )
  456. }
  457. return loadExceedsThreshold
  458. }
  459. /// Starts connecting an idle connection, if one exists.
  460. @usableFromInline
  461. internal func _startConnectingIdleConnection() {
  462. if let index = self._connections.values.firstIndex(where: { $0.manager.sync.isIdle }) {
  463. self._connections.values[index].manager.sync.startConnecting()
  464. } else {
  465. let connecting = self._connections.values.count { $0.manager.sync.isConnecting }
  466. let ready = self._connections.values.count { $0.manager.sync.isReady }
  467. let transientFailure = self._connections.values.count { $0.manager.sync.isTransientFailure }
  468. self.logger.debug(
  469. "no idle connections in pool",
  470. metadata: [
  471. Metadata.connectionsConnecting: .stringConvertible(connecting),
  472. Metadata.connectionsReady: .stringConvertible(ready),
  473. Metadata.connectionsTransientFailure: .stringConvertible(transientFailure),
  474. Metadata.waitersCount: .stringConvertible(self.waiters.count),
  475. ]
  476. )
  477. }
  478. }
  479. /// Returns the index in `self.connections.values` of the connection with the most available
  480. /// streams. Returns `self.connections.endIndex` if no connection has at least one stream
  481. /// available.
  482. ///
  483. /// - Note: this is linear in the number of connections.
  484. @usableFromInline
  485. internal func _mostAvailableConnectionIndex()
  486. -> Dictionary<ConnectionManagerID, PerConnectionState>.Index
  487. {
  488. var index = self._connections.values.startIndex
  489. var selectedIndex = self._connections.values.endIndex
  490. var mostAvailableStreams = 0
  491. while index != self._connections.values.endIndex {
  492. let availableStreams = self._connections.values[index].availableStreams
  493. if availableStreams > mostAvailableStreams {
  494. mostAvailableStreams = availableStreams
  495. selectedIndex = index
  496. }
  497. self._connections.values.formIndex(after: &index)
  498. }
  499. return selectedIndex
  500. }
  501. /// Reserves a stream from the connection with the most available streams, if one exists.
  502. ///
  503. /// - Returns: The `HTTP2StreamMultiplexer` from the connection the stream was reserved from,
  504. /// or `nil` if no stream could be reserved.
  505. @usableFromInline
  506. internal func _reserveStreamFromMostAvailableConnection() -> HTTP2StreamMultiplexer? {
  507. let index = self._mostAvailableConnectionIndex()
  508. if index != self._connections.endIndex {
  509. // '!' is okay here; the most available connection must have at least one stream available
  510. // to reserve.
  511. return self._connections.values[index].reserveStream()!
  512. } else {
  513. return nil
  514. }
  515. }
  516. /// See `shutdown(mode:)`.
  517. ///
  518. /// - Parameter promise: A `promise` to complete when the pool has been shutdown.
  519. @usableFromInline
  520. internal func _shutdown(mode: ConnectionManager.ShutdownMode, promise: EventLoopPromise<Void>) {
  521. self.eventLoop.assertInEventLoop()
  522. switch self._state {
  523. case .active:
  524. self.logger.debug("shutting down connection pool")
  525. // We're shutting down now and when that's done we'll be fully shutdown.
  526. self._state = .shuttingDown(promise.futureResult)
  527. promise.futureResult.whenComplete { _ in
  528. self._state = .shutdown
  529. self.delegate = nil
  530. self.logger.trace("finished shutting down connection pool")
  531. }
  532. // Shutdown all the connections and remove them from the pool.
  533. let connections = self._connections
  534. self._connections.removeAll()
  535. let allShutdown: [EventLoopFuture<Void>] = connections.values.map {
  536. let id = $0.manager.id
  537. let manager = $0.manager
  538. return manager.eventLoop.flatSubmit {
  539. // If the connection was idle/shutdown before calling shutdown then we shouldn't tell
  540. // the delegate the connection closed (because it either never connected or was already
  541. // informed about this).
  542. let connectionIsInactive = manager.sync.isIdle || manager.sync.isShutdown
  543. return manager.shutdown(mode: mode).always { _ in
  544. if !connectionIsInactive {
  545. self.delegate?.connectionClosed(id: .init(id), error: nil)
  546. }
  547. self.delegate?.connectionRemoved(id: .init(id))
  548. }
  549. }
  550. }
  551. // Fail the outstanding waiters.
  552. while let waiter = self.waiters.popFirst() {
  553. waiter.fail(GRPCConnectionPoolError.shutdown)
  554. }
  555. // Cascade the result of the shutdown into the promise.
  556. EventLoopFuture.andAllSucceed(allShutdown, promise: promise)
  557. case let .shuttingDown(future):
  558. // We're already shutting down, cascade the result.
  559. promise.completeWith(future)
  560. case .shutdown:
  561. // Already shutdown, fine.
  562. promise.succeed(())
  563. }
  564. }
  565. internal func stats() -> EventLoopFuture<GRPCSubPoolStats> {
  566. let promise = self.eventLoop.makePromise(of: GRPCSubPoolStats.self)
  567. if self.eventLoop.inEventLoop {
  568. self._stats(promise: promise)
  569. } else {
  570. self.eventLoop.execute {
  571. self._stats(promise: promise)
  572. }
  573. }
  574. return promise.futureResult
  575. }
  576. private func _stats(promise: EventLoopPromise<GRPCSubPoolStats>) {
  577. self.eventLoop.assertInEventLoop()
  578. var stats = GRPCSubPoolStats(id: self.id)
  579. for connection in self._connections.values {
  580. let sync = connection.manager.sync
  581. if sync.isIdle {
  582. stats.connectionStates.idle += 1
  583. } else if sync.isConnecting {
  584. stats.connectionStates.connecting += 1
  585. } else if sync.isReady {
  586. stats.connectionStates.ready += 1
  587. } else if sync.isTransientFailure {
  588. stats.connectionStates.transientFailure += 1
  589. }
  590. stats.streamsInUse += connection.reservedStreams
  591. stats.streamsFreeToUse += connection.availableStreams
  592. }
  593. stats.rpcsWaiting += self.waiters.count
  594. promise.succeed(stats)
  595. }
  596. }
  597. extension ConnectionPool: ConnectionManagerConnectivityDelegate {
  598. // We're interested in a few different situations here:
  599. //
  600. // 1. The connection was usable ('ready') and is no longer usable (either it became idle or
  601. // encountered an error. If this happens we need to notify any connections of the change as
  602. // they may no longer be used for new RPCs.
  603. // 2. The connection was not usable but moved to a different unusable state. If this happens and
  604. // we know the cause of the state transition (i.e. the error) then we need to update our most
  605. // recent error with the error. This information is used when failing waiters to provide some
  606. // context as to why they may be failing.
  607. func connectionStateDidChange(
  608. _ manager: ConnectionManager,
  609. from oldState: _ConnectivityState,
  610. to newState: _ConnectivityState
  611. ) {
  612. switch (oldState, newState) {
  613. case let (.ready, .transientFailure(error)),
  614. let (.ready, .idle(.some(error))):
  615. self.updateMostRecentError(error)
  616. self.connectionUnavailable(manager.id)
  617. case (.ready, .idle(.none)),
  618. (.ready, .shutdown):
  619. self.connectionUnavailable(manager.id)
  620. case let (_, .transientFailure(error)),
  621. let (_, .idle(.some(error))):
  622. self.updateMostRecentError(error)
  623. default:
  624. ()
  625. }
  626. guard let delegate = self.delegate else { return }
  627. switch (oldState, newState) {
  628. case (.idle, .connecting),
  629. (.transientFailure, .connecting):
  630. delegate.startedConnecting(id: .init(manager.id))
  631. case (.connecting, .ready):
  632. // The connection becoming ready is handled by 'receivedSettingsMaxConcurrentStreams'.
  633. ()
  634. case (.ready, .idle):
  635. delegate.connectionClosed(id: .init(manager.id), error: nil)
  636. case let (.ready, .transientFailure(error)):
  637. delegate.connectionClosed(id: .init(manager.id), error: error)
  638. case let (.connecting, .transientFailure(error)):
  639. delegate.connectFailed(id: .init(manager.id), error: error)
  640. default:
  641. ()
  642. }
  643. }
  644. func connectionIsQuiescing(_ manager: ConnectionManager) {
  645. self.eventLoop.assertInEventLoop()
  646. // Find the relevant connection.
  647. guard let index = self._connections.index(forKey: manager.id) else {
  648. return
  649. }
  650. // Drop the connectivity delegate, we're no longer interested in its events now.
  651. manager.sync.connectivityDelegate = nil
  652. // Started quiescing; update our state and notify the pool delegate.
  653. self._connections.values[index].isQuiescing = true
  654. self.delegate?.connectionQuiescing(id: .init(manager.id))
  655. // As the connection is quescing, we need to know when the current connection its managing has
  656. // closed. When that happens drop the H2 delegate and update the pool delegate.
  657. manager.onCurrentConnectionClose { hadActiveConnection in
  658. assert(hadActiveConnection)
  659. if let removed = self._connections.removeValue(forKey: manager.id) {
  660. removed.manager.sync.http2Delegate = nil
  661. self.delegate?.connectionClosed(id: .init(removed.manager.id), error: nil)
  662. self.delegate?.connectionRemoved(id: .init(removed.manager.id))
  663. }
  664. }
  665. // Grab the number of reserved streams (before invalidating the index by adding a connection).
  666. let reservedStreams = self._connections.values[index].reservedStreams
  667. // Replace the connection with a new idle one. Keep the idle behavior, so that
  668. // if it's a connection that should be kept alive, we maintain it.
  669. self.addConnectionToPool(idleBehavior: manager.idleBehavior)
  670. // Since we're removing this connection from the pool (and no new streams can be created on
  671. // the connection), the pool manager can ignore any streams reserved against this connection.
  672. // We do still care about the number of reserved streams for the connection though
  673. //
  674. // Note: we don't need to adjust the number of available streams as the effective number of
  675. // connections hasn't changed.
  676. self.streamLender.returnStreams(reservedStreams, to: self)
  677. }
  678. private func updateMostRecentError(_ error: Error) {
  679. self.eventLoop.assertInEventLoop()
  680. // Update the last known error if there is one. We will use it to provide some context to
  681. // waiters which may fail.
  682. self._mostRecentError = error
  683. }
  684. /// A connection has become unavailable.
  685. private func connectionUnavailable(_ id: ConnectionManagerID) {
  686. self.eventLoop.assertInEventLoop()
  687. // The connection is no longer available: any streams which haven't been closed will be counted
  688. // as a dropped reservation, we need to tell the pool manager about them.
  689. if let droppedReservations = self._connections[id]?.unavailable(), droppedReservations > 0 {
  690. self.streamLender.returnStreams(droppedReservations, to: self)
  691. }
  692. }
  693. }
  694. extension ConnectionPool: ConnectionManagerHTTP2Delegate {
  695. internal func streamOpened(_ manager: ConnectionManager) {
  696. self.eventLoop.assertInEventLoop()
  697. if let utilization = self._connections[manager.id]?.openedStream(),
  698. let delegate = self.delegate
  699. {
  700. delegate.connectionUtilizationChanged(
  701. id: .init(manager.id),
  702. streamsUsed: utilization.used,
  703. streamCapacity: utilization.capacity
  704. )
  705. }
  706. }
  707. internal func streamClosed(_ manager: ConnectionManager) {
  708. self.eventLoop.assertInEventLoop()
  709. guard let index = self._connections.index(forKey: manager.id) else {
  710. return
  711. }
  712. // Return the stream the connection and to the pool manager.
  713. if let utilization = self._connections.values[index].returnStream(),
  714. let delegate = self.delegate
  715. {
  716. delegate.connectionUtilizationChanged(
  717. id: .init(manager.id),
  718. streamsUsed: utilization.used,
  719. streamCapacity: utilization.capacity
  720. )
  721. }
  722. // Return the stream to the pool manager if the connection is available and not quiescing. For
  723. // quiescing connections streams were returned when the connection started quiescing.
  724. if self._connections.values[index].isAvailable, !self._connections.values[index].isQuiescing {
  725. self.streamLender.returnStreams(1, to: self)
  726. // A stream was returned: we may be able to service a waiter now.
  727. self.tryServiceWaiters()
  728. }
  729. }
  730. internal func receivedSettingsMaxConcurrentStreams(
  731. _ manager: ConnectionManager,
  732. maxConcurrentStreams: Int
  733. ) {
  734. self.eventLoop.assertInEventLoop()
  735. // Find the relevant connection.
  736. guard let index = self._connections.index(forKey: manager.id) else {
  737. return
  738. }
  739. // When the connection is quiescing, the pool manager is not interested in updates to the
  740. // connection, bail out early.
  741. if self._connections.values[index].isQuiescing {
  742. return
  743. }
  744. // If we received a SETTINGS update then a connection is okay: drop the last known error.
  745. self._mostRecentError = nil
  746. let previous = self._connections.values[index].updateMaxConcurrentStreams(maxConcurrentStreams)
  747. let delta: Int
  748. if let previousValue = previous {
  749. // There was a previous value of max concurrent streams, i.e. a change in value for an
  750. // existing connection.
  751. delta = maxConcurrentStreams - previousValue
  752. } else {
  753. // There was no previous value so this must be a new connection. We'll compare against our
  754. // assumed default.
  755. delta = maxConcurrentStreams - self.assumedMaxConcurrentStreams
  756. // Notify the delegate.
  757. self.delegate?.connectSucceeded(id: .init(manager.id), streamCapacity: maxConcurrentStreams)
  758. }
  759. if delta != 0 {
  760. self.streamLender.changeStreamCapacity(by: delta, for: self)
  761. }
  762. // We always check, even if `delta` isn't greater than zero as this might be a new connection.
  763. self.tryServiceWaiters()
  764. }
  765. }
  766. extension ConnectionPool {
  767. // MARK: - Waiters
  768. /// Try to service as many waiters as possible.
  769. ///
  770. /// This an expensive operation, in the worst case it will be `O(W ⨉ N)` where `W` is the number
  771. /// of waiters and `N` is the number of connections.
  772. private func tryServiceWaiters() {
  773. if self.waiters.isEmpty { return }
  774. self.logger.trace(
  775. "servicing waiters",
  776. metadata: [
  777. Metadata.waitersCount: .stringConvertible(self.waiters.count)
  778. ]
  779. )
  780. let now = self.now()
  781. var serviced = 0
  782. while !self.waiters.isEmpty {
  783. if self.waiters.first!.deadlineIsAfter(now) {
  784. if let multiplexer = self._reserveStreamFromMostAvailableConnection() {
  785. // The waiter's deadline is in the future, and we have a suitable connection. Remove and
  786. // succeed the waiter.
  787. let waiter = self.waiters.removeFirst()
  788. serviced &+= 1
  789. waiter.succeed(with: multiplexer)
  790. } else {
  791. // There are waiters but no available connections, we're done.
  792. break
  793. }
  794. } else {
  795. // The waiter's deadline has already expired, there's no point completing it. Remove it and
  796. // let its scheduled timeout fail the promise.
  797. self.waiters.removeFirst()
  798. }
  799. }
  800. self.logger.trace(
  801. "done servicing waiters",
  802. metadata: [
  803. Metadata.waitersCount: .stringConvertible(self.waiters.count),
  804. Metadata.waitersServiced: .stringConvertible(serviced),
  805. ]
  806. )
  807. }
  808. }
  809. extension ConnectionPool {
  810. /// Synchronous operations for the pool, mostly used by tests.
  811. internal struct Sync {
  812. private let pool: ConnectionPool
  813. fileprivate init(_ pool: ConnectionPool) {
  814. self.pool = pool
  815. }
  816. /// The number of outstanding connection waiters.
  817. internal var waiters: Int {
  818. self.pool.eventLoop.assertInEventLoop()
  819. return self.pool.waiters.count
  820. }
  821. /// The number of connection currently in the pool (in any state).
  822. internal var connections: Int {
  823. self.pool.eventLoop.assertInEventLoop()
  824. return self.pool._connections.count
  825. }
  826. /// The number of idle connections in the pool.
  827. internal var idleConnections: Int {
  828. self.pool.eventLoop.assertInEventLoop()
  829. return self.pool._connections.values.reduce(0) { $0 &+ ($1.manager.sync.isIdle ? 1 : 0) }
  830. }
  831. /// The number of active (i.e. connecting or ready) connections in the pool.
  832. internal var activeConnections: Int {
  833. self.pool.eventLoop.assertInEventLoop()
  834. return self.pool._connections.values.reduce(0) {
  835. $0 &+ (($1.manager.sync.isReady || $1.manager.sync.isConnecting) ? 1 : 0)
  836. }
  837. }
  838. /// The number of connections in the pool in transient failure state.
  839. internal var transientFailureConnections: Int {
  840. self.pool.eventLoop.assertInEventLoop()
  841. return self.pool._connections.values.reduce(0) {
  842. $0 &+ ($1.manager.sync.isTransientFailure ? 1 : 0)
  843. }
  844. }
  845. /// The number of streams currently available to reserve across all connections in the pool.
  846. internal var availableStreams: Int {
  847. self.pool.eventLoop.assertInEventLoop()
  848. return self.pool._connections.values.reduce(0) { $0 + $1.availableStreams }
  849. }
  850. /// The number of streams which have been reserved across all connections in the pool.
  851. internal var reservedStreams: Int {
  852. self.pool.eventLoop.assertInEventLoop()
  853. return self.pool._connections.values.reduce(0) { $0 + $1.reservedStreams }
  854. }
  855. /// Updates the most recent connection error.
  856. internal func updateMostRecentError(_ error: Error) {
  857. self.pool.eventLoop.assertInEventLoop()
  858. self.pool.updateMostRecentError(error)
  859. }
  860. }
  861. internal var sync: Sync {
  862. return Sync(self)
  863. }
  864. }
  865. /// An error thrown from the ``GRPCChannelPool``.
  866. public struct GRPCConnectionPoolError: Error, CustomStringConvertible {
  867. public struct Code: Hashable, Sendable, CustomStringConvertible {
  868. enum Code {
  869. case shutdown
  870. case tooManyWaiters
  871. case deadlineExceeded
  872. }
  873. fileprivate var code: Code
  874. private init(_ code: Code) {
  875. self.code = code
  876. }
  877. public var description: String {
  878. String(describing: self.code)
  879. }
  880. /// The pool is shutdown or shutting down.
  881. public static var shutdown: Self { Self(.shutdown) }
  882. /// There are too many waiters in the pool.
  883. public static var tooManyWaiters: Self { Self(.tooManyWaiters) }
  884. /// The deadline for creating a stream has passed.
  885. public static var deadlineExceeded: Self { Self(.deadlineExceeded) }
  886. }
  887. /// The error code.
  888. public var code: Code
  889. /// An underlying error which caused this error to be thrown.
  890. public var underlyingError: Error?
  891. public var description: String {
  892. if let underlyingError = self.underlyingError {
  893. return "\(self.code) (\(underlyingError))"
  894. } else {
  895. return String(describing: self.code)
  896. }
  897. }
  898. /// Create a new connection pool error with the given code and underlying error.
  899. ///
  900. /// - Parameters:
  901. /// - code: The error code.
  902. /// - underlyingError: The underlying error which led to this error being thrown.
  903. public init(code: Code, underlyingError: Error? = nil) {
  904. self.code = code
  905. self.underlyingError = underlyingError
  906. }
  907. }
  908. extension GRPCConnectionPoolError {
  909. @usableFromInline
  910. static let shutdown = Self(code: .shutdown)
  911. @inlinable
  912. static func tooManyWaiters(connectionError: Error?) -> Self {
  913. Self(code: .tooManyWaiters, underlyingError: connectionError)
  914. }
  915. @inlinable
  916. static func deadlineExceeded(connectionError: Error?) -> Self {
  917. Self(code: .deadlineExceeded, underlyingError: connectionError)
  918. }
  919. }
  920. extension GRPCConnectionPoolError: GRPCStatusTransformable {
  921. public func makeGRPCStatus() -> GRPCStatus {
  922. switch self.code.code {
  923. case .shutdown:
  924. return GRPCStatus(
  925. code: .unavailable,
  926. message: "The connection pool is shutdown",
  927. cause: self.underlyingError
  928. )
  929. case .tooManyWaiters:
  930. return GRPCStatus(
  931. code: .resourceExhausted,
  932. message: "The connection pool has no capacity for new RPCs or RPC waiters",
  933. cause: self.underlyingError
  934. )
  935. case .deadlineExceeded:
  936. return GRPCStatus(
  937. code: .deadlineExceeded,
  938. message: "Timed out waiting for an HTTP/2 stream from the connection pool",
  939. cause: self.underlyingError
  940. )
  941. }
  942. }
  943. }
  944. extension Sequence {
  945. fileprivate func count(where predicate: (Element) -> Bool) -> Int {
  946. return self.reduce(0) { count, element in
  947. predicate(element) ? count + 1 : count
  948. }
  949. }
  950. }