Skip to content

Fix bug in migration from HTTP1 to HTTP2 and back to HTTP1 #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,21 @@ extension HTTPConnectionPool {

mutating func migrateToHTTP2() -> HTTP1ToHTTP2MigrationContext {
var migrationContext = HTTP1ToHTTP2MigrationContext()
self.connections.removeAll { connection in
switch connection.migrateToHTTP2(&migrationContext) {
let initialOverflowIndex = self.overflowIndex

self.connections = self.connections.enumerated().compactMap { index, connectionState in
switch connectionState.migrateToHTTP2(&migrationContext) {
case .removeConnection:
return true
// If the connection has an index smaller than the previous overflow index,
// we deal with a general purpose connection.
// For this reason we need to decrement the overflow index.
if index < initialOverflowIndex {
self.overflowIndex = self.connections.index(before: self.overflowIndex)
}
return nil

case .keepConnection:
return false
return connectionState
}
}
return migrationContext
Expand Down Expand Up @@ -654,6 +663,9 @@ extension HTTPConnectionPool {
self.connections = self.connections.enumerated().compactMap { index, connectionState in
switch connectionState.cleanup(&cleanupContext) {
case .removeConnection:
// If the connection has an index smaller than the previous overflow index,
// we deal with a general purpose connection.
// For this reason we need to decrement the overflow index.
if index < initialOverflowIndex {
self.overflowIndex = self.connections.index(before: self.overflowIndex)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extension HTTPConnectionPool_HTTP1ConnectionsTests {
("testMigrationFromHTTP2WithAlreadyLeasedHTTP1Connection", testMigrationFromHTTP2WithAlreadyLeasedHTTP1Connection),
("testMigrationFromHTTP2WithMoreStartingConnectionsThanMaximumAllowedConccurentConnections", testMigrationFromHTTP2WithMoreStartingConnectionsThanMaximumAllowedConccurentConnections),
("testMigrationFromHTTP2StartsEnoghOverflowConnectionsForRequiredEventLoopRequests", testMigrationFromHTTP2StartsEnoghOverflowConnectionsForRequiredEventLoopRequests),
("testMigrationFromHTTP1ToHTTP2AndBackToHTTP1", testMigrationFromHTTP1ToHTTP2AndBackToHTTP1),
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,43 @@ class HTTPConnectionPool_HTTP1ConnectionsTests: XCTestCase {
XCTAssertTrue(context.eventLoop === el)
}
}

func testMigrationFromHTTP1ToHTTP2AndBackToHTTP1() throws {
let elg = EmbeddedEventLoopGroup(loops: 2)
defer { XCTAssertNoThrow(try elg.syncShutdownGracefully()) }
let el1 = elg.next()
let el2 = elg.next()

let generator = HTTPConnectionPool.Connection.ID.Generator()
var connections = HTTPConnectionPool.HTTP1Connections(maximumConcurrentConnections: 8, generator: generator)

let connID1 = connections.createNewConnection(on: el1)

let context = connections.migrateToHTTP2()
XCTAssertEqual(context, .init(
backingOff: [],
starting: [(connID1, el1)],
close: []
))

let connID2 = generator.next()

connections.migrateFromHTTP2(
starting: [(connID2, el2)],
backingOff: []
)

let conn2: HTTPConnectionPool.Connection = .__testOnly_connection(id: connID2, eventLoop: el2)
let (_, idleContext) = connections.newHTTP1ConnectionEstablished(conn2)
XCTAssertEqual(idleContext.use, .generalPurpose)
XCTAssertEqual(idleContext.eventLoop.id, el2.id)
}
}

extension HTTPConnectionPool.HTTP1Connections.HTTP1ToHTTP2MigrationContext: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.close == rhs.close &&
lhs.starting.elementsEqual(rhs.starting, by: { $0.0 == $1.0 && $0.1 === $1.1 }) &&
lhs.backingOff.elementsEqual(rhs.backingOff, by: { $0.0 == $1.0 && $0.1 === $1.1 })
}
}