|
12 | 12 | //
|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
| 15 | +import Logging |
| 16 | +import NIO |
15 | 17 | import NIOConcurrencyHelpers
|
| 18 | +import NIOHTTP1 |
| 19 | + |
| 20 | +extension HTTPConnectionPool { |
| 21 | + final class Manager { |
| 22 | + private typealias Key = ConnectionPool.Key |
| 23 | + |
| 24 | + private enum State { |
| 25 | + case active |
| 26 | + case shuttingDown(promise: EventLoopPromise<Bool>?, unclean: Bool) |
| 27 | + case shutDown |
| 28 | + } |
| 29 | + |
| 30 | + private let eventLoopGroup: EventLoopGroup |
| 31 | + private let configuration: HTTPClient.Configuration |
| 32 | + private let connectionIDGenerator = Connection.ID.globalGenerator |
| 33 | + private let logger: Logger |
| 34 | + |
| 35 | + private var state: State = .active |
| 36 | + private var _pools: [Key: HTTPConnectionPool] = [:] |
| 37 | + private let lock = Lock() |
| 38 | + |
| 39 | + private let sslContextCache = SSLContextCache() |
| 40 | + |
| 41 | + init(eventLoopGroup: EventLoopGroup, |
| 42 | + configuration: HTTPClient.Configuration, |
| 43 | + backgroundActivityLogger logger: Logger) { |
| 44 | + self.eventLoopGroup = eventLoopGroup |
| 45 | + self.configuration = configuration |
| 46 | + self.logger = logger |
| 47 | + } |
| 48 | + |
| 49 | + func executeRequest(_ request: HTTPSchedulableRequest) { |
| 50 | + let poolKey = request.poolKey |
| 51 | + let poolResult = self.lock.withLock { () -> Result<HTTPConnectionPool, HTTPClientError> in |
| 52 | + switch self.state { |
| 53 | + case .active: |
| 54 | + if let pool = self._pools[poolKey] { |
| 55 | + return .success(pool) |
| 56 | + } |
| 57 | + |
| 58 | + let pool = HTTPConnectionPool( |
| 59 | + eventLoopGroup: self.eventLoopGroup, |
| 60 | + sslContextCache: self.sslContextCache, |
| 61 | + tlsConfiguration: request.tlsConfiguration, |
| 62 | + clientConfiguration: self.configuration, |
| 63 | + key: poolKey, |
| 64 | + delegate: self, |
| 65 | + idGenerator: self.connectionIDGenerator, |
| 66 | + backgroundActivityLogger: self.logger |
| 67 | + ) |
| 68 | + self._pools[poolKey] = pool |
| 69 | + return .success(pool) |
| 70 | + |
| 71 | + case .shuttingDown, .shutDown: |
| 72 | + return .failure(HTTPClientError.alreadyShutdown) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + switch poolResult { |
| 77 | + case .success(let pool): |
| 78 | + pool.executeRequest(request) |
| 79 | + case .failure(let error): |
| 80 | + request.fail(error) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// Shutdown the connection pool manager. You **must** shutdown the pool manager, since it leak otherwise. |
| 85 | + /// |
| 86 | + /// - Parameter promise: An `EventLoopPromise` that is succeeded once all connections pools are shutdown. |
| 87 | + /// - Returns: An EventLoopFuture that is succeeded once the pool is shutdown. The bool indicates if the |
| 88 | + /// shutdown was unclean. |
| 89 | + func shutdown(promise: EventLoopPromise<Bool>?) { |
| 90 | + enum ShutdownAction { |
| 91 | + case done(EventLoopPromise<Bool>?) |
| 92 | + case shutdown([Key: HTTPConnectionPool]) |
| 93 | + } |
| 94 | + |
| 95 | + let action = self.lock.withLock { () -> ShutdownAction in |
| 96 | + switch self.state { |
| 97 | + case .active: |
| 98 | + // If there aren't any pools, we can mark the pool as shut down right away. |
| 99 | + if self._pools.isEmpty { |
| 100 | + self.state = .shutDown |
| 101 | + return .done(promise) |
| 102 | + } else { |
| 103 | + // this promise will be succeeded once all connection pools are shutdown |
| 104 | + self.state = .shuttingDown(promise: promise, unclean: false) |
| 105 | + return .shutdown(self._pools) |
| 106 | + } |
| 107 | + |
| 108 | + case .shuttingDown, .shutDown: |
| 109 | + preconditionFailure("PoolManager already shutdown") |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // if no pools are returned, the manager is already shutdown completely. Inform the |
| 114 | + // delegate. This is a very clean shutdown... |
| 115 | + switch action { |
| 116 | + case .done(let promise): |
| 117 | + promise?.succeed(false) |
| 118 | + |
| 119 | + case .shutdown(let pools): |
| 120 | + pools.values.forEach { pool in |
| 121 | + pool.shutdown() |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +extension HTTPConnectionPool.Manager: HTTPConnectionPoolDelegate { |
| 129 | + func connectionPoolDidShutdown(_ pool: HTTPConnectionPool, unclean: Bool) { |
| 130 | + enum CloseAction { |
| 131 | + case close(EventLoopPromise<Bool>?, unclean: Bool) |
| 132 | + case wait |
| 133 | + } |
| 134 | + |
| 135 | + let closeAction = self.lock.withLock { () -> CloseAction in |
| 136 | + switch self.state { |
| 137 | + case .active, .shutDown: |
| 138 | + preconditionFailure("Why are pools shutting down, if the manager did not give a signal") |
| 139 | + |
| 140 | + case .shuttingDown(let promise, let soFarUnclean): |
| 141 | + guard self._pools.removeValue(forKey: pool.key) === pool else { |
| 142 | + preconditionFailure("Expected that the pool was created by this manager and is known for this reason.") |
| 143 | + } |
| 144 | + |
| 145 | + if self._pools.isEmpty { |
| 146 | + self.state = .shutDown |
| 147 | + return .close(promise, unclean: soFarUnclean || unclean) |
| 148 | + } else { |
| 149 | + self.state = .shuttingDown(promise: promise, unclean: soFarUnclean || unclean) |
| 150 | + return .wait |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + switch closeAction { |
| 156 | + case .close(let promise, unclean: let unclean): |
| 157 | + promise?.succeed(unclean) |
| 158 | + case .wait: |
| 159 | + break |
| 160 | + } |
| 161 | + } |
| 162 | +} |
16 | 163 |
|
17 | 164 | extension HTTPConnectionPool.Connection.ID {
|
18 | 165 | static var globalGenerator = Generator()
|
|
0 commit comments