Skip to content

Commit b7c5edd

Browse files
committed
fix review comments
1 parent 45225b9 commit b7c5edd

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+HTTP1Connections.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ extension HTTPConnectionPool {
195195
case keepConnection
196196
}
197197

198-
func migrateToHTTP2(_ context: inout HTTP1Connections.HTTP2ToHTTP1MigrationContext) -> MigrateAction {
198+
func migrateToHTTP2(_ context: inout HTTP1Connections.HTTP1ToHTTP2MigrationContext) -> MigrateAction {
199199
switch self.state {
200200
case .starting:
201201
context.starting.append((self.connectionID, self.eventLoop))
@@ -322,7 +322,7 @@ extension HTTPConnectionPool {
322322
var connectionsStartingForUseCase: Int
323323
}
324324

325-
struct HTTP2ToHTTP1MigrationContext {
325+
struct HTTP1ToHTTP2MigrationContext {
326326
var backingOff: [(Connection.ID, EventLoop)] = []
327327
var starting: [(Connection.ID, EventLoop)] = []
328328
var close: [Connection] = []
@@ -517,8 +517,8 @@ extension HTTPConnectionPool {
517517

518518
// MARK: Migration
519519

520-
mutating func migrateToHTTP2() -> HTTP2ToHTTP1MigrationContext {
521-
var migrationContext = HTTP2ToHTTP1MigrationContext()
520+
mutating func migrateToHTTP2() -> HTTP1ToHTTP2MigrationContext {
521+
var migrationContext = HTTP1ToHTTP2MigrationContext()
522522
self.connections.removeAll { connection in
523523
switch connection.migrateToHTTP2(&migrationContext) {
524524
case .removeConnection:

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+HTTP2StateMachine.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import NIOCore
1616
import NIOHTTP2
1717

1818
extension HTTPConnectionPool {
19-
struct HTTP2StateMaschine {
19+
struct HTTP2StateMachine {
2020
typealias Action = HTTPConnectionPool.StateMachine.Action
2121
typealias RequestAction = HTTPConnectionPool.StateMachine.RequestAction
2222
typealias ConnectionAction = HTTPConnectionPool.StateMachine.ConnectionAction
@@ -70,6 +70,7 @@ extension HTTPConnectionPool {
7070
self.requests = requests
7171

7272
// TODO: Close all idle connections from context.close
73+
// TODO: Start new http2 connections for
7374
// TODO: Potentially cancel unneeded bootstraps (Needs cancellable ClientBootstrap)
7475

7576
return .none
@@ -401,14 +402,19 @@ extension HTTPConnectionPool {
401402
// Any http1 connection that becomes idle should be closed right away after the transition
402403
// to http2.
403404
let connection = self.http1Connections!.closeConnection(at: index)
404-
if self.http1Connections!.isEmpty {
405-
self.http1Connections = nil
405+
guard self.http1Connections!.isEmpty else {
406+
return .init(request: .none, connection: .closeConnection(connection, isShutdown: .no))
406407
}
408+
// if there are no more http1Connections, we can remove the struct.
409+
self.http1Connections = nil
410+
411+
// we must also check, if we are shutting down. Was this maybe out last connection?
407412
switch state {
408413
case .running:
409414
return .init(request: .none, connection: .closeConnection(connection, isShutdown: .no))
410415
case .shuttingDown(let unclean):
411-
if self.http1Connections == nil && self.connections.isEmpty {
416+
if self.connections.isEmpty {
417+
// if the http2connections are empty as well, there are no more connections. Shutdown completed.
412418
return .init(request: .none, connection: .closeConnection(connection, isShutdown: .yes(unclean: unclean)))
413419
} else {
414420
return .init(request: .none, connection: .closeConnection(connection, isShutdown: .no))

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+RequestQueue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension CircularBuffer {
147147
///
148148
/// - Complexity: O(*k*), where *k* is the number of elements removed.
149149
fileprivate mutating func popFirst(max: Int) -> [Element] {
150-
precondition(max >= 0, "")
150+
precondition(max >= 0)
151151
let elementCountToRemove = Swift.min(max, self.count)
152152
let array = Array(self[self.startIndex..<self.index(self.startIndex, offsetBy: elementCountToRemove)])
153153
self.removeFirst(elementCountToRemove)

Tests/AsyncHTTPClientTests/HTTPConnectionPool+HTTP2StateMachineTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class HTTPConnectionPool_HTTP2StateMachineTests: XCTestCase {
2929
let el1 = elg.next()
3030
var connections = MockConnectionPool()
3131
var queuer = MockRequestQueuer()
32-
var state = HTTPConnectionPool.HTTP2StateMaschine(idGenerator: .init())
32+
var state = HTTPConnectionPool.HTTP2StateMachine(idGenerator: .init())
3333

3434
/// first request should create a new connection
3535
let mockRequest = MockHTTPRequest(eventLoop: el1)
@@ -119,7 +119,7 @@ class HTTPConnectionPool_HTTP2StateMachineTests: XCTestCase {
119119
let elg = EmbeddedEventLoopGroup(loops: 4)
120120
defer { XCTAssertNoThrow(try elg.syncShutdownGracefully()) }
121121

122-
var state = HTTPConnectionPool.HTTP2StateMaschine(
122+
var state = HTTPConnectionPool.HTTP2StateMachine(
123123
idGenerator: .init()
124124
)
125125

@@ -175,7 +175,7 @@ class HTTPConnectionPool_HTTP2StateMachineTests: XCTestCase {
175175
let elg = EmbeddedEventLoopGroup(loops: 4)
176176
defer { XCTAssertNoThrow(try elg.syncShutdownGracefully()) }
177177

178-
var state = HTTPConnectionPool.HTTP2StateMaschine(
178+
var state = HTTPConnectionPool.HTTP2StateMachine(
179179
idGenerator: .init()
180180
)
181181

@@ -212,7 +212,7 @@ class HTTPConnectionPool_HTTP2StateMachineTests: XCTestCase {
212212
let elg = EmbeddedEventLoopGroup(loops: 4)
213213
defer { XCTAssertNoThrow(try elg.syncShutdownGracefully()) }
214214

215-
var state = HTTPConnectionPool.HTTP2StateMaschine(
215+
var state = HTTPConnectionPool.HTTP2StateMachine(
216216
idGenerator: .init()
217217
)
218218

@@ -294,7 +294,7 @@ class HTTPConnectionPool_HTTP2StateMachineTests: XCTestCase {
294294

295295
// second connection is a HTTP2 connection and we need to migrate
296296
let conn2: HTTPConnectionPool.Connection = .__testOnly_connection(id: conn2ID, eventLoop: el1)
297-
var http2State = HTTPConnectionPool.HTTP2StateMaschine(idGenerator: idGenerator)
297+
var http2State = HTTPConnectionPool.HTTP2StateMachine(idGenerator: idGenerator)
298298

299299
let migrationAction = http2State.migrateConnectionsFromHTTP1(
300300
connections: http1State.connections,

0 commit comments

Comments
 (0)