|
| 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 NIO |
| 16 | +import NIOHTTP1 |
| 17 | + |
| 18 | +extension HTTPConnectionPool { |
| 19 | + struct StateMachine { |
| 20 | + struct Action { |
| 21 | + let request: RequestAction |
| 22 | + let connection: ConnectionAction |
| 23 | + |
| 24 | + init(request: RequestAction, connection: ConnectionAction) { |
| 25 | + self.request = request |
| 26 | + self.connection = connection |
| 27 | + } |
| 28 | + |
| 29 | + static let none: Action = Action(request: .none, connection: .none) |
| 30 | + } |
| 31 | + |
| 32 | + enum ConnectionAction { |
| 33 | + enum IsShutdown: Equatable { |
| 34 | + case yes(unclean: Bool) |
| 35 | + case no |
| 36 | + } |
| 37 | + |
| 38 | + case createConnection(Connection.ID, on: EventLoop) |
| 39 | + case scheduleBackoffTimer(Connection.ID, backoff: TimeAmount, on: EventLoop) |
| 40 | + |
| 41 | + case scheduleTimeoutTimer(Connection.ID, on: EventLoop) |
| 42 | + case cancelTimeoutTimer(Connection.ID) |
| 43 | + |
| 44 | + case closeConnection(Connection, isShutdown: IsShutdown) |
| 45 | + case cleanupConnections(CleanupContext, isShutdown: IsShutdown) |
| 46 | + |
| 47 | + case none |
| 48 | + } |
| 49 | + |
| 50 | + enum RequestAction { |
| 51 | + case executeRequest(Request, Connection, cancelTimeout: Bool) |
| 52 | + case executeRequestsAndCancelTimeouts([Request], Connection) |
| 53 | + |
| 54 | + case failRequest(Request, Error, cancelTimeout: Bool) |
| 55 | + case failRequestsAndCancelTimeouts([Request], Error) |
| 56 | + |
| 57 | + case scheduleRequestTimeout(for: Request, on: EventLoop) |
| 58 | + case cancelRequestTimeout(Request.ID) |
| 59 | + |
| 60 | + case none |
| 61 | + } |
| 62 | + |
| 63 | + enum HTTPVersionState { |
| 64 | + case http1(HTTP1StateMachine) |
| 65 | + } |
| 66 | + |
| 67 | + var state: HTTPVersionState |
| 68 | + var isShuttingDown: Bool = false |
| 69 | + |
| 70 | + let eventLoopGroup: EventLoopGroup |
| 71 | + let maximumConcurrentHTTP1Connections: Int |
| 72 | + |
| 73 | + init(eventLoopGroup: EventLoopGroup, idGenerator: Connection.ID.Generator, maximumConcurrentHTTP1Connections: Int) { |
| 74 | + self.maximumConcurrentHTTP1Connections = maximumConcurrentHTTP1Connections |
| 75 | + let http1State = HTTP1StateMachine( |
| 76 | + idGenerator: idGenerator, |
| 77 | + maximumConcurrentConnections: maximumConcurrentHTTP1Connections |
| 78 | + ) |
| 79 | + self.state = .http1(http1State) |
| 80 | + self.eventLoopGroup = eventLoopGroup |
| 81 | + } |
| 82 | + |
| 83 | + mutating func executeRequest(_ request: Request) -> Action { |
| 84 | + switch self.state { |
| 85 | + case .http1(var http1StateMachine): |
| 86 | + let action = http1StateMachine.executeRequest(request) |
| 87 | + self.state = .http1(http1StateMachine) |
| 88 | + return action |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + mutating func newHTTP1ConnectionCreated(_ connection: Connection) -> Action { |
| 93 | + switch self.state { |
| 94 | + case .http1(var http1StateMachine): |
| 95 | + let action = http1StateMachine.newHTTP1ConnectionEstablished(connection) |
| 96 | + self.state = .http1(http1StateMachine) |
| 97 | + return action |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + mutating func failedToCreateNewConnection(_ error: Error, connectionID: Connection.ID) -> Action { |
| 102 | + switch self.state { |
| 103 | + case .http1(var http1StateMachine): |
| 104 | + let action = http1StateMachine.failedToCreateNewConnection( |
| 105 | + error, |
| 106 | + connectionID: connectionID |
| 107 | + ) |
| 108 | + self.state = .http1(http1StateMachine) |
| 109 | + return action |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + mutating func connectionCreationBackoffDone(_ connectionID: Connection.ID) -> Action { |
| 114 | + switch self.state { |
| 115 | + case .http1(var http1StateMachine): |
| 116 | + let action = http1StateMachine.connectionCreationBackoffDone(connectionID) |
| 117 | + self.state = .http1(http1StateMachine) |
| 118 | + return action |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /// A request has timed out. |
| 123 | + /// |
| 124 | + /// This is different to a request being cancelled. If a request times out, we need to fail the |
| 125 | + /// request, but don't need to cancel the timer (it already triggered). If a request is cancelled |
| 126 | + /// we don't need to fail it but we need to cancel its timeout timer. |
| 127 | + mutating func timeoutRequest(_ requestID: Request.ID) -> Action { |
| 128 | + switch self.state { |
| 129 | + case .http1(var http1StateMachine): |
| 130 | + let action = http1StateMachine.timeoutRequest(requestID) |
| 131 | + self.state = .http1(http1StateMachine) |
| 132 | + return action |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// A request was cancelled. |
| 137 | + /// |
| 138 | + /// This is different to a request timing out. If a request is cancelled we don't need to fail it but we |
| 139 | + /// need to cancel its timeout timer. If a request times out, we need to fail the request, but don't |
| 140 | + /// need to cancel the timer (it already triggered). |
| 141 | + mutating func cancelRequest(_ requestID: Request.ID) -> Action { |
| 142 | + switch self.state { |
| 143 | + case .http1(var http1StateMachine): |
| 144 | + let action = http1StateMachine.cancelRequest(requestID) |
| 145 | + self.state = .http1(http1StateMachine) |
| 146 | + return action |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + mutating func connectionIdleTimeout(_ connectionID: Connection.ID) -> Action { |
| 151 | + switch self.state { |
| 152 | + case .http1(var http1StateMachine): |
| 153 | + let action = http1StateMachine.connectionIdleTimeout(connectionID) |
| 154 | + self.state = .http1(http1StateMachine) |
| 155 | + return action |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /// A connection has been closed |
| 160 | + mutating func connectionClosed(_ connectionID: Connection.ID) -> Action { |
| 161 | + switch self.state { |
| 162 | + case .http1(var http1StateMachine): |
| 163 | + let action = http1StateMachine.connectionClosed(connectionID) |
| 164 | + self.state = .http1(http1StateMachine) |
| 165 | + return action |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + mutating func http1ConnectionReleased(_ connectionID: Connection.ID) -> Action { |
| 170 | + switch self.state { |
| 171 | + case .http1(var http1StateMachine): |
| 172 | + let action = http1StateMachine.http1ConnectionReleased(connectionID) |
| 173 | + self.state = .http1(http1StateMachine) |
| 174 | + return action |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + mutating func shutdown() -> Action { |
| 179 | + precondition(!self.isShuttingDown, "Shutdown must only be called once") |
| 180 | + |
| 181 | + self.isShuttingDown = true |
| 182 | + |
| 183 | + switch self.state { |
| 184 | + case .http1(var http1StateMachine): |
| 185 | + let action = http1StateMachine.shutdown() |
| 186 | + self.state = .http1(http1StateMachine) |
| 187 | + return action |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +extension HTTPConnectionPool { |
| 194 | + /// The pool cleanup todo list. |
| 195 | + struct CleanupContext: Equatable { |
| 196 | + /// the connections to close right away. These are idle. |
| 197 | + var close: [Connection] |
| 198 | + |
| 199 | + /// the connections that currently run a request that needs to be cancelled to close the connections |
| 200 | + var cancel: [Connection] |
| 201 | + |
| 202 | + /// the connections that are backing off from connection creation |
| 203 | + var connectBackoff: [Connection.ID] |
| 204 | + |
| 205 | + init(close: [Connection] = [], cancel: [Connection] = [], connectBackoff: [Connection.ID] = []) { |
| 206 | + self.close = close |
| 207 | + self.cancel = cancel |
| 208 | + self.connectBackoff = connectBackoff |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +extension HTTPConnectionPool.StateMachine: CustomStringConvertible { |
| 214 | + var description: String { |
| 215 | + switch self.state { |
| 216 | + case .http1(let http1): |
| 217 | + return ".http1(\(http1))" |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments