|
12 | 12 | //
|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
| 15 | +import Logging |
| 16 | +import NIO |
15 | 17 | import NIOConcurrencyHelpers
|
| 18 | +import NIOHTTP1 |
| 19 | + |
| 20 | +protocol HTTPConnectionPoolManagerDelegate: AnyObject { |
| 21 | + func httpConnectionPoolManagerDidShutdown(_: HTTPConnectionPool.Manager, unclean: Bool) |
| 22 | +} |
| 23 | + |
| 24 | +extension HTTPConnectionPool { |
| 25 | + final class Manager { |
| 26 | + private typealias Key = ConnectionPool.Key |
| 27 | + |
| 28 | + private var _pools: [Key: HTTPConnectionPool] = [:] |
| 29 | + private let lock = Lock() |
| 30 | + |
| 31 | + private let sslContextCache = SSLContextCache() |
| 32 | + |
| 33 | + enum State { |
| 34 | + case active |
| 35 | + case shuttingDown(unclean: Bool) |
| 36 | + case shutDown |
| 37 | + } |
| 38 | + |
| 39 | + let eventLoopGroup: EventLoopGroup |
| 40 | + let configuration: HTTPClient.Configuration |
| 41 | + let connectionIDGenerator = Connection.ID.globalGenerator |
| 42 | + let logger: Logger |
| 43 | + |
| 44 | + /// A delegate to inform about the pools managers shutdown |
| 45 | + /// |
| 46 | + /// NOTE: Normally we create retain cycles in SwiftNIO code that we break on shutdown. However we wan't to inform |
| 47 | + /// users that they must call `shutdown` on their AsyncHTTPClient. The best way to make them aware is with |
| 48 | + /// a `preconditionFailure` in the HTTPClient's `deinit`. If we create a retain cycle here, the |
| 49 | + /// `HTTPClient`'s `deinit` can never be reached. Instead the `HTTPClient` would leak. |
| 50 | + /// |
| 51 | + /// The delegate is not thread safe at all. This only works if the HTTPClient sets itself as a delegate in its own |
| 52 | + /// init. |
| 53 | + weak var delegate: HTTPConnectionPoolManagerDelegate? |
| 54 | + |
| 55 | + private var state: State = .active |
| 56 | + |
| 57 | + init(eventLoopGroup: EventLoopGroup, |
| 58 | + configuration: HTTPClient.Configuration, |
| 59 | + backgroundActivityLogger logger: Logger) { |
| 60 | + self.eventLoopGroup = eventLoopGroup |
| 61 | + self.configuration = configuration |
| 62 | + self.logger = logger |
| 63 | + } |
| 64 | + |
| 65 | + deinit { |
| 66 | + guard case .shutDown = self.state else { |
| 67 | + preconditionFailure("Manager must be shutdown before deinit") |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + func execute(request: HTTP1RequestTask) { |
| 72 | + let key = Key(request.request) |
| 73 | + |
| 74 | + let poolResult = self.lock.withLock { () -> Result<HTTPConnectionPool, HTTPClientError> in |
| 75 | + guard case .active = self.state else { |
| 76 | + return .failure(HTTPClientError.alreadyShutdown) |
| 77 | + } |
| 78 | + |
| 79 | + if let pool = self._pools[key] { |
| 80 | + return .success(pool) |
| 81 | + } |
| 82 | + |
| 83 | + let pool = HTTPConnectionPool( |
| 84 | + eventLoopGroup: self.eventLoopGroup, |
| 85 | + sslContextCache: self.sslContextCache, |
| 86 | + tlsConfiguration: request.request.tlsConfiguration, |
| 87 | + clientConfiguration: self.configuration, |
| 88 | + key: key, |
| 89 | + delegate: self, |
| 90 | + idGenerator: self.connectionIDGenerator, |
| 91 | + logger: self.logger |
| 92 | + ) |
| 93 | + self._pools[key] = pool |
| 94 | + return .success(pool) |
| 95 | + } |
| 96 | + |
| 97 | + switch poolResult { |
| 98 | + case .success(let pool): |
| 99 | + pool.execute(request: request) |
| 100 | + case .failure(let error): |
| 101 | + request.fail(error) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + func shutdown() { |
| 106 | + let pools = self.lock.withLock { () -> [Key: HTTPConnectionPool] in |
| 107 | + guard case .active = self.state else { |
| 108 | + preconditionFailure("PoolManager already shutdown") |
| 109 | + } |
| 110 | + |
| 111 | + // If there aren't any pools, we can mark the pool as shut down right away. |
| 112 | + if self._pools.isEmpty { |
| 113 | + self.state = .shutDown |
| 114 | + } else { |
| 115 | + self.state = .shuttingDown(unclean: false) |
| 116 | + } |
| 117 | + |
| 118 | + return self._pools |
| 119 | + } |
| 120 | + |
| 121 | + // if no pools are returned, the manager is already shutdown completely. Inform the |
| 122 | + // delegate. This is a very clean shutdown... |
| 123 | + if pools.isEmpty { |
| 124 | + self.delegate?.httpConnectionPoolManagerDidShutdown(self, unclean: false) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + pools.values.forEach { pool in |
| 129 | + pool.shutdown() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +extension HTTPConnectionPool.Manager: HTTPConnectionPoolDelegate { |
| 136 | + enum CloseAction { |
| 137 | + case close(unclean: Bool) |
| 138 | + case wait |
| 139 | + } |
| 140 | + |
| 141 | + func connectionPoolDidShutdown(_ pool: HTTPConnectionPool, unclean: Bool) { |
| 142 | + let closeAction = self.lock.withLock { () -> CloseAction in |
| 143 | + guard case .shuttingDown(let soFarUnclean) = self.state else { |
| 144 | + preconditionFailure("Why are pools shutting down, if the manager did not give a signal") |
| 145 | + } |
| 146 | + |
| 147 | + guard self._pools.removeValue(forKey: pool.key) === pool else { |
| 148 | + preconditionFailure("Expected that the pool was ") |
| 149 | + } |
| 150 | + |
| 151 | + if self._pools.isEmpty { |
| 152 | + self.state = .shutDown |
| 153 | + return .close(unclean: soFarUnclean || unclean) |
| 154 | + } else { |
| 155 | + self.state = .shuttingDown(unclean: soFarUnclean || unclean) |
| 156 | + return .wait |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + switch closeAction { |
| 161 | + case .close(unclean: let unclean): |
| 162 | + self.delegate?.httpConnectionPoolManagerDidShutdown(self, unclean: unclean) |
| 163 | + case .wait: |
| 164 | + break |
| 165 | + } |
| 166 | + } |
| 167 | +} |
16 | 168 |
|
17 | 169 | extension HTTPConnectionPool.Connection.ID {
|
18 | 170 | static var globalGenerator = Generator()
|
|
0 commit comments