Skip to content

Refactor provider shutdown and pending flows #240

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
Jun 13, 2020
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
12 changes: 8 additions & 4 deletions Sources/AsyncHTTPClient/ConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ final class ConnectionPool {
if let existing = self.providers[key], existing.enqueue() {
return existing
} else {
// Connection provider will be created with `pending = 1`
let provider = HTTP1ConnectionProvider(key: key,
eventLoop: taskEventLoop,
configuration: self.configuration,
pool: self,
backgroundActivityLogger: self.backgroundActivityLogger)
let enqueued = provider.enqueue()
assert(enqueued)
self.providers[key] = provider
return provider
}
Expand Down Expand Up @@ -263,8 +264,6 @@ struct ConnectionKey: Hashable {
/// of concurrent requests as it has built-in politeness regarding the maximum number
/// of concurrent requests to the server.
class HTTP1ConnectionProvider {
struct ProviderClosedError: Error {}

/// The client configuration used to bootstrap new requests
private let configuration: HTTPClient.Configuration

Expand Down Expand Up @@ -318,7 +317,7 @@ class HTTP1ConnectionProvider {
self.state.assertInvariants()
}

private func execute(_ action: Action, logger: Logger) {
func execute(_ action: Action, logger: Logger) {
switch action {
case .lease(let connection, let waiter):
// if connection is became inactive, we create a new one.
Expand Down Expand Up @@ -494,6 +493,11 @@ class HTTP1ConnectionProvider {
$0.promise.fail(HTTPClientError.cancelled)
}

if available.isEmpty, leased.isEmpty {
self.closePromise.succeed(())
return self.closePromise.futureResult.map { clean }
}

EventLoopFuture.andAllComplete(leased.map { $0.cancel() }, on: self.eventLoop).flatMap { _ in
EventLoopFuture.andAllComplete(available.map { $0.close() }, on: self.eventLoop)
}.whenFailure { error in
Expand Down
4 changes: 2 additions & 2 deletions Sources/AsyncHTTPClient/ConnectionsState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension HTTP1ConnectionProvider {
private var openedConnectionsCount: Int = 0

/// Number of enqueued requests, used to track if it is safe to delete the provider.
private var pending: Int = 1
private var pending: Int = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! I left a bunch of stuff like the following comments in the code

        /* BEGIN workaround for #232, this whole block is to be replaced by the commented out line below */
        // not closing the provider here (https://github.com/swift-server/async-http-client/issues/232)
        var state = self.http1ConnectionProvider.state.testsOnly_getInternalState()
        if state.pending == 1, state.waiters.isEmpty, state.leasedConnections.isEmpty, state.openedConnectionsCount == 0 {
            state.pending = 0
            self.http1ConnectionProvider.state.testsOnly_setInternalState(state)
        }
        self.http1ConnectionProvider.closePromise.succeed(())
        /* END workaround for #232 */

and

            // cleanup
            // this cleanup code needs to go and use HTTP1ConnectionProvider's API instead

I believe we can also delete all this code then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to delete the workaround, some clean up code, and I was able to replace some cleanup with API calls instead of direct state manipulation. Currently there are 3-4 tests that manipulate state directly (they all need the state to be near the limit), do you want me to fix it here or when I get to #232?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, let's fix them in a followup. It's super awesome you could remove the workaround


init(maximumConcurrentConnections: Int = 8, eventLoop: EventLoop) {
self.maximumConcurrentConnections = maximumConcurrentConnections
Expand Down Expand Up @@ -148,7 +148,7 @@ extension HTTP1ConnectionProvider {
return .none
}
case .closed:
return .fail(waiter, ProviderClosedError())
return .fail(waiter, HTTPClientError.alreadyShutdown)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artemredkin from what I can tell, no test noticed that this error changed which probably means no test is getting this error ;). I think we should probably add a test case that tests this condition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filed #244

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific test is a good idea, yes, though this error was picked up by a test, just not reliably :)

}
}

Expand Down
Loading