ConnectionPool.swift 35 KB

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