Skip to content

[HTTP2] Forward HTTP/2 events to StateMachine #466

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 1 commit into from
Nov 4, 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 @@ -37,7 +37,7 @@ final class HTTP2Connection {

enum State {
case initialized
case starting(EventLoopPromise<Void>)
case starting(EventLoopPromise<Int>)
case active(maxStreams: Int)
case closing
case closed
Expand Down Expand Up @@ -117,9 +117,9 @@ final class HTTP2Connection {
delegate: HTTP2ConnectionDelegate,
configuration: HTTPClient.Configuration,
logger: Logger
) -> EventLoopFuture<HTTP2Connection> {
) -> EventLoopFuture<(HTTP2Connection, Int)> {
let connection = HTTP2Connection(channel: channel, connectionID: connectionID, delegate: delegate, logger: logger)
return connection.start().map { _ in connection }
return connection.start().map { maxStreams in (connection, maxStreams) }
}

func executeRequest(_ request: HTTPExecutableRequest) {
Expand Down Expand Up @@ -154,10 +154,10 @@ final class HTTP2Connection {
return promise.futureResult
}

private func start() -> EventLoopFuture<Void> {
private func start() -> EventLoopFuture<Int> {
self.channel.eventLoop.assertInEventLoop()

let readyToAcceptConnectionsPromise = self.channel.eventLoop.makePromise(of: Void.self)
let readyToAcceptConnectionsPromise = self.channel.eventLoop.makePromise(of: Int.self)

self.state = .starting(readyToAcceptConnectionsPromise)
self.channel.closeFuture.whenComplete { _ in
Expand Down Expand Up @@ -266,7 +266,7 @@ extension HTTP2Connection: HTTP2IdleHandlerDelegate {

case .starting(let promise):
self.state = .active(maxStreams: maxStreams)
promise.succeed(())
promise.succeed(maxStreams)

case .active:
self.state = .active(maxStreams: maxStreams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ extension HTTPConnectionPool.ConnectionFactory {
logger: logger
).whenComplete { result in
switch result {
case .success(let connection):
requester.http2ConnectionCreated(connection, maximumStreams: 0)
case .success((let connection, let maximumStreams)):
requester.http2ConnectionCreated(connection, maximumStreams: maximumStreams)
case .failure(let error):
requester.failedToCreateHTTPConnection(connectionID, error: error)
}
Expand Down
65 changes: 39 additions & 26 deletions Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,14 @@ extension HTTPConnectionPool: HTTPConnectionRequester {
}

func http2ConnectionCreated(_ connection: HTTP2Connection, maximumStreams: Int) {
preconditionFailure("Did not expect http/2 connections right now.")
// let action = self.stateLock.withLock { () -> StateMachine.Action in
// if let settings = connection.settings {
// return self._state.newHTTP2ConnectionCreated(.http2(connection), settings: settings)
// } else {
// // immidiate connection closure before we can register with state machine
// // is the only reason we don't have settings
// struct ImmidiateConnectionClose: Error {}
// return self._state.failedToCreateNewConnection(ImmidiateConnectionClose(), connectionID: connection.id)
// }
// }
// self.run(action: action)
self.logger.trace("successfully created connection", metadata: [
"ahc-connection-id": "\(connection.id)",
"ahc-http-version": "http/2",
"ahc-max-streams": "\(maximumStreams)",
])
self.modifyStateAndRunActions {
$0.newHTTP2ConnectionCreated(.http2(connection), maxConcurrentStreams: maximumStreams)
}
}

func failedToCreateHTTPConnection(_ connectionID: HTTPConnectionPool.Connection.ID, error: Error) {
Expand Down Expand Up @@ -497,27 +493,44 @@ extension HTTPConnectionPool: HTTP1ConnectionDelegate {

extension HTTPConnectionPool: HTTP2ConnectionDelegate {
func http2Connection(_ connection: HTTP2Connection, newMaxStreamSetting: Int) {
// ignore for now
self.logger.debug("new max stream setting", metadata: [
"ahc-connection-id": "\(connection.id)",
"ahc-http-version": "http/2",
"ahc-max-streams": "\(newMaxStreamSetting)",
])
self.modifyStateAndRunActions {
$0.newHTTP2MaxConcurrentStreamsReceived(connection.id, newMaxStreams: newMaxStreamSetting)
}
}

func http2ConnectionGoAwayReceived(_: HTTP2Connection) {
// ignore for now
func http2ConnectionGoAwayReceived(_ connection: HTTP2Connection) {
self.logger.debug("connection go away received", metadata: [
"ahc-connection-id": "\(connection.id)",
"ahc-http-version": "http/2",
])
self.modifyStateAndRunActions {
$0.http2ConnectionGoAwayReceived(connection.id)
}
}

func http2ConnectionClosed(_: HTTP2Connection) {
// ignore for now
// let action = self.stateLock.withLock {
// self._state.connectionClosed(connection.id)
// }
// self.run(action: action)
func http2ConnectionClosed(_ connection: HTTP2Connection) {
self.logger.debug("connection closed", metadata: [
"ahc-connection-id": "\(connection.id)",
"ahc-http-version": "http/2",
])
self.modifyStateAndRunActions {
$0.http2ConnectionClosed(connection.id)
}
}

func http2ConnectionStreamClosed(_ connection: HTTP2Connection, availableStreams: Int) {
// ignore for now
// let action = self.stateLock.withLock {
// self._state.http2ConnectionStreamClosed(connection.id, availableStreams: availableStreams)
// }
// self.run(action: action)
self.logger.trace("stream closed", metadata: [
"ahc-connection-id": "\(connection.id)",
"ahc-http-version": "http/2",
])
self.modifyStateAndRunActions {
$0.http2ConnectionStreamClosed(connection.id)
}
}
}

Expand Down