|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | +import NIOHTTP2 |
| 17 | + |
| 18 | +extension HTTPConnectionPool { |
| 19 | + struct HTTP2StateMaschine { |
| 20 | + typealias Action = HTTPConnectionPool.StateMachine.Action |
| 21 | + typealias RequestAction = HTTPConnectionPool.StateMachine.RequestAction |
| 22 | + typealias ConnectionAction = HTTPConnectionPool.StateMachine.ConnectionAction |
| 23 | + |
| 24 | + private var lastConnectFailure: Error? |
| 25 | + private var failedConsecutiveConnectionAttempts = 0 |
| 26 | + |
| 27 | + private var connections: HTTP2Connections |
| 28 | + private var http1Connections: HTTP1Connections? |
| 29 | + |
| 30 | + private var requests: RequestQueue |
| 31 | + |
| 32 | + private let idGenerator: Connection.ID.Generator |
| 33 | + |
| 34 | + init( |
| 35 | + idGenerator: Connection.ID.Generator |
| 36 | + ) { |
| 37 | + self.idGenerator = idGenerator |
| 38 | + self.requests = RequestQueue() |
| 39 | + |
| 40 | + self.connections = HTTP2Connections(generator: idGenerator) |
| 41 | + } |
| 42 | + |
| 43 | + mutating func migrateConnectionsFromHTTP1( |
| 44 | + connections http1Connections: HTTP1Connections, |
| 45 | + requests: RequestQueue |
| 46 | + ) -> Action { |
| 47 | + precondition(self.http1Connections == nil) |
| 48 | + precondition(self.connections.isEmpty) |
| 49 | + precondition(self.requests.isEmpty) |
| 50 | + |
| 51 | + var http1Connections = http1Connections // make http1Connections mutable |
| 52 | + let context = http1Connections.migrateToHTTP2() |
| 53 | + self.connections.migrateConnections( |
| 54 | + starting: context.starting, |
| 55 | + backingOff: context.backingOff |
| 56 | + ) |
| 57 | + |
| 58 | + if !http1Connections.isEmpty { |
| 59 | + self.http1Connections = http1Connections |
| 60 | + } |
| 61 | + |
| 62 | + self.requests = requests |
| 63 | + |
| 64 | + // TODO: Close all idle connections from context.close |
| 65 | + // TODO: Potentially cancel unneeded bootstraps (Needs cancellable ClientBootstrap) |
| 66 | + |
| 67 | + return .none |
| 68 | + } |
| 69 | + |
| 70 | + mutating func executeRequest(_ request: Request) -> Action { |
| 71 | + if let eventLoop = request.requiredEventLoop { |
| 72 | + return self.executeRequest(request, onRequired: eventLoop) |
| 73 | + } else { |
| 74 | + return self.executeRequest(request, onPreferred: request.preferredEventLoop) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private mutating func executeRequest( |
| 79 | + _ request: Request, |
| 80 | + onRequired eventLoop: EventLoop |
| 81 | + ) -> Action { |
| 82 | + if let connection = self.connections.leaseStream(onRequired: eventLoop) { |
| 83 | + /// 1. we have a stream available and can execute the request immediately |
| 84 | + return .init( |
| 85 | + request: .executeRequest(request, connection, cancelTimeout: false), |
| 86 | + connection: .cancelTimeoutTimer(connection.id) |
| 87 | + ) |
| 88 | + } |
| 89 | + /// 2. No available stream so we definitely need to wait until we have one |
| 90 | + self.requests.push(request) |
| 91 | + |
| 92 | + if self.connections.hasConnectionThatCanOrWillBeAbleToExecuteRequests(for: eventLoop) { |
| 93 | + /// 3. we already have a connection, we just need to wait until until it becomes available |
| 94 | + return .init( |
| 95 | + request: .scheduleRequestTimeout(for: request, on: eventLoop), |
| 96 | + connection: .none |
| 97 | + ) |
| 98 | + } else { |
| 99 | + /// 4. we do *not* have a connection, need to create a new one and wait until it is connected. |
| 100 | + let connectionId = self.connections.createNewConnection(on: eventLoop) |
| 101 | + return .init( |
| 102 | + request: .scheduleRequestTimeout(for: request, on: eventLoop), |
| 103 | + connection: .createConnection(connectionId, on: eventLoop) |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private mutating func executeRequest( |
| 109 | + _ request: Request, |
| 110 | + onPreferred eventLoop: EventLoop |
| 111 | + ) -> Action { |
| 112 | + if let connection = self.connections.leaseStream(onPreferred: eventLoop) { |
| 113 | + /// 1. we have a stream available and can execute the request immediately |
| 114 | + return .init( |
| 115 | + request: .executeRequest(request, connection, cancelTimeout: false), |
| 116 | + connection: .cancelTimeoutTimer(connection.id) |
| 117 | + ) |
| 118 | + } |
| 119 | + /// 2. No available stream so we definitely need to wait until we have one |
| 120 | + self.requests.push(request) |
| 121 | + |
| 122 | + if self.connections.hasConnectionThatCanOrWillBeAbleToExecuteRequests { |
| 123 | + /// 3. we already have a connection, we just need to wait until until it becomes available |
| 124 | + return .init( |
| 125 | + request: .scheduleRequestTimeout(for: request, on: eventLoop), |
| 126 | + connection: .none |
| 127 | + ) |
| 128 | + } else { |
| 129 | + /// 4. we do *not* have a connection, need to create a new one and wait until it is connected. |
| 130 | + let connectionId = self.connections.createNewConnection(on: eventLoop) |
| 131 | + return .init( |
| 132 | + request: .scheduleRequestTimeout(for: request, on: eventLoop), |
| 133 | + connection: .createConnection(connectionId, on: eventLoop) |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + mutating func newHTTP2ConnectionEstablished(_ connection: Connection, maxConcurrentStreams: Int) -> Action { |
| 139 | + self.failedConsecutiveConnectionAttempts = 0 |
| 140 | + self.lastConnectFailure = nil |
| 141 | + let (index, context) = self.connections.newHTTP2ConnectionEstablished( |
| 142 | + connection, |
| 143 | + maxConcurrentStreams: maxConcurrentStreams |
| 144 | + ) |
| 145 | + return self.nextActionForAvailableConnection(at: index, context: context) |
| 146 | + } |
| 147 | + |
| 148 | + private mutating func nextActionForAvailableConnection( |
| 149 | + at index: Int, |
| 150 | + context: HTTP2Connections.AvailableConnectionContext |
| 151 | + ) -> Action { |
| 152 | + // We prioritise requests with a required event loop over those without a requirement. |
| 153 | + // This can cause starvation for request without a required event loop. |
| 154 | + // We should come up with a better algorithm in the future. |
| 155 | + |
| 156 | + var requestsToExecute = self.requests.popFirst(max: context.availableStreams, for: context.eventLoop) |
| 157 | + let remainingAvailableStreams = context.availableStreams - requestsToExecute.count |
| 158 | + // use the remaining available streams for requests without a required event loop |
| 159 | + requestsToExecute += self.requests.popFirst(max: remainingAvailableStreams, for: nil) |
| 160 | + let connection = self.connections.leaseStreams(at: index, count: requestsToExecute.count) |
| 161 | + |
| 162 | + let requestAction = { () -> RequestAction in |
| 163 | + if requestsToExecute.isEmpty { |
| 164 | + return .none |
| 165 | + } else { |
| 166 | + return .executeRequestsAndCancelTimeouts(requestsToExecute, connection) |
| 167 | + } |
| 168 | + }() |
| 169 | + |
| 170 | + let connectionAction = { () -> ConnectionAction in |
| 171 | + if context.isIdle, requestsToExecute.isEmpty { |
| 172 | + return .scheduleTimeoutTimer(connection.id, on: context.eventLoop) |
| 173 | + } else { |
| 174 | + return .none |
| 175 | + } |
| 176 | + }() |
| 177 | + |
| 178 | + return .init( |
| 179 | + request: requestAction, |
| 180 | + connection: connectionAction |
| 181 | + ) |
| 182 | + } |
| 183 | + |
| 184 | + mutating func newHTTP2MaxConcurrentStreamsReceived(_ connectionID: Connection.ID, newMaxStreams: Int) -> Action { |
| 185 | + let (index, context) = self.connections.newHTTP2MaxConcurrentStreamsReceived(connectionID, newMaxStreams: newMaxStreams) |
| 186 | + return self.nextActionForAvailableConnection(at: index, context: context) |
| 187 | + } |
| 188 | + |
| 189 | + mutating func http2ConnectionGoAwayReceived(_ connectionID: Connection.ID) -> Action { |
| 190 | + let context = self.connections.goAwayReceived(connectionID) |
| 191 | + return self.nextActionForClosingConnection(on: context.eventLoop) |
| 192 | + } |
| 193 | + |
| 194 | + mutating func http2ConnectionClosed(_ connectionID: Connection.ID) -> Action { |
| 195 | + guard let (index, context) = self.connections.failConnection(connectionID) else { |
| 196 | + return .none |
| 197 | + } |
| 198 | + return self.nextActionForFailedConnection(at: index, on: context.eventLoop) |
| 199 | + } |
| 200 | + |
| 201 | + private mutating func nextActionForFailedConnection(at index: Int, on eventLoop: EventLoop) -> Action { |
| 202 | + let hasPendingRequest = !self.requests.isEmpty(for: eventLoop) || !self.requests.isEmpty(for: nil) |
| 203 | + guard hasPendingRequest else { |
| 204 | + return .none |
| 205 | + } |
| 206 | + |
| 207 | + let (newConnectionID, previousEventLoop) = self.connections.createNewConnectionByReplacingClosedConnection(at: index) |
| 208 | + precondition(previousEventLoop === eventLoop) |
| 209 | + |
| 210 | + return .init( |
| 211 | + request: .none, |
| 212 | + connection: .createConnection(newConnectionID, on: eventLoop) |
| 213 | + ) |
| 214 | + } |
| 215 | + |
| 216 | + private mutating func nextActionForClosingConnection(on eventLoop: EventLoop) -> Action { |
| 217 | + let hasPendingRequest = !self.requests.isEmpty(for: eventLoop) || !self.requests.isEmpty(for: nil) |
| 218 | + guard hasPendingRequest else { |
| 219 | + return .none |
| 220 | + } |
| 221 | + |
| 222 | + let newConnectionID = self.connections.createNewConnection(on: eventLoop) |
| 223 | + |
| 224 | + return .init( |
| 225 | + request: .none, |
| 226 | + connection: .createConnection(newConnectionID, on: eventLoop) |
| 227 | + ) |
| 228 | + } |
| 229 | + |
| 230 | + mutating func http2ConnectionStreamClosed(_ connectionID: Connection.ID) -> Action { |
| 231 | + let (index, context) = self.connections.releaseStream(connectionID) |
| 232 | + return self.nextActionForAvailableConnection(at: index, context: context) |
| 233 | + } |
| 234 | + |
| 235 | + mutating func failedToCreateNewConnection(_ error: Error, connectionID: Connection.ID) -> Action { |
| 236 | + self.failedConsecutiveConnectionAttempts += 1 |
| 237 | + self.lastConnectFailure = error |
| 238 | + |
| 239 | + let eventLoop = self.connections.backoffNextConnectionAttempt(connectionID) |
| 240 | + let backoff = calculateBackoff(failedAttempt: self.failedConsecutiveConnectionAttempts) |
| 241 | + return .init(request: .none, connection: .scheduleBackoffTimer(connectionID, backoff: backoff, on: eventLoop)) |
| 242 | + } |
| 243 | + |
| 244 | + mutating func connectionCreationBackoffDone(_ connectionID: Connection.ID) -> Action { |
| 245 | + // The naming of `failConnection` is a little confusing here. All it does is moving the |
| 246 | + // connection state from `.backingOff` to `.closed` here. It also returns the |
| 247 | + // connection's index. |
| 248 | + guard let (index, _) = self.connections.failConnection(connectionID) else { |
| 249 | + preconditionFailure("Backing off a connection that is unknown to us?") |
| 250 | + } |
| 251 | + let (newConnectionID, eventLoop) = self.connections.createNewConnectionByReplacingClosedConnection(at: index) |
| 252 | + return .init(request: .none, connection: .createConnection(newConnectionID, on: eventLoop)) |
| 253 | + } |
| 254 | + |
| 255 | + mutating func timeoutRequest(_ requestID: Request.ID) -> Action { |
| 256 | + // 1. check requests in queue |
| 257 | + if let request = self.requests.remove(requestID) { |
| 258 | + var error: Error = HTTPClientError.getConnectionFromPoolTimeout |
| 259 | + if let lastError = self.lastConnectFailure { |
| 260 | + error = lastError |
| 261 | + } else if !self.connections.hasActiveConnections { |
| 262 | + error = HTTPClientError.connectTimeout |
| 263 | + } |
| 264 | + return .init( |
| 265 | + request: .failRequest(request, error, cancelTimeout: false), |
| 266 | + connection: .none |
| 267 | + ) |
| 268 | + } |
| 269 | + |
| 270 | + // 2. This point is reached, because the request may have already been scheduled. A |
| 271 | + // connection might have become available shortly before the request timeout timer |
| 272 | + // fired. |
| 273 | + return .none |
| 274 | + } |
| 275 | + |
| 276 | + mutating func cancelRequest(_ requestID: Request.ID) -> Action { |
| 277 | + // 1. check requests in queue |
| 278 | + if self.requests.remove(requestID) != nil { |
| 279 | + return .init( |
| 280 | + request: .cancelRequestTimeout(requestID), |
| 281 | + connection: .none |
| 282 | + ) |
| 283 | + } |
| 284 | + |
| 285 | + // 2. This is point is reached, because the request may already have been forwarded to |
| 286 | + // an idle connection. In this case the connection will need to handle the |
| 287 | + // cancellation. |
| 288 | + return .none |
| 289 | + } |
| 290 | + |
| 291 | + mutating func connectionIdleTimeout(_ connectionID: Connection.ID) -> Action { |
| 292 | + guard let connection = connections.closeConnectionIfIdle(connectionID) else { |
| 293 | + return .none |
| 294 | + } |
| 295 | + return .init( |
| 296 | + request: .none, |
| 297 | + connection: .closeConnection(connection, isShutdown: .no) |
| 298 | + ) |
| 299 | + } |
| 300 | + |
| 301 | + mutating func connectionClosed(_ connectionID: Connection.ID) -> Action { |
| 302 | + guard let (index, context) = self.connections.failConnection(connectionID) else { |
| 303 | + // When a connection close is initiated by the connection pool, the connection will |
| 304 | + // still report its close to the state machine. In those cases we must ignore the |
| 305 | + // event. |
| 306 | + return .none |
| 307 | + } |
| 308 | + return self.nextActionForFailedConnection(at: index, on: context.eventLoop) |
| 309 | + } |
| 310 | + |
| 311 | + mutating func http1ConnectionReleased(_: Connection.ID) -> Action { |
| 312 | + fatalError("TODO: implement \(#function)") |
| 313 | + } |
| 314 | + |
| 315 | + mutating func shutdown() -> Action { |
| 316 | + // If we have remaining request queued, we should fail all of them with a cancelled |
| 317 | + // error. |
| 318 | + let waitingRequests = self.requests.removeAll() |
| 319 | + |
| 320 | + var requestAction: StateMachine.RequestAction = .none |
| 321 | + if !waitingRequests.isEmpty { |
| 322 | + requestAction = .failRequestsAndCancelTimeouts(waitingRequests, HTTPClientError.cancelled) |
| 323 | + } |
| 324 | + |
| 325 | + // clean up the connections, we can cleanup now! |
| 326 | + let cleanupContext = self.connections.shutdown() |
| 327 | + |
| 328 | + // If there aren't any more connections, everything is shutdown |
| 329 | + let isShutdown: StateMachine.ConnectionAction.IsShutdown |
| 330 | + let unclean = !(cleanupContext.cancel.isEmpty && waitingRequests.isEmpty) |
| 331 | + if self.connections.isEmpty { |
| 332 | + isShutdown = .yes(unclean: unclean) |
| 333 | + } else { |
| 334 | + isShutdown = .no |
| 335 | + } |
| 336 | + return .init( |
| 337 | + request: requestAction, |
| 338 | + connection: .cleanupConnections(cleanupContext, isShutdown: isShutdown) |
| 339 | + ) |
| 340 | + } |
| 341 | + } |
| 342 | +} |
0 commit comments