Skip to content

Commit bb8c4fa

Browse files
authored
Refactor provider shutdown and pending flows (#240)
1 parent 8add6b8 commit bb8c4fa

File tree

3 files changed

+135
-111
lines changed

3 files changed

+135
-111
lines changed

Diff for: Sources/AsyncHTTPClient/ConnectionPool.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ final class ConnectionPool {
8080
if let existing = self.providers[key], existing.enqueue() {
8181
return existing
8282
} else {
83-
// Connection provider will be created with `pending = 1`
8483
let provider = HTTP1ConnectionProvider(key: key,
8584
eventLoop: taskEventLoop,
8685
configuration: self.configuration,
8786
pool: self,
8887
backgroundActivityLogger: self.backgroundActivityLogger)
88+
let enqueued = provider.enqueue()
89+
assert(enqueued)
8990
self.providers[key] = provider
9091
return provider
9192
}
@@ -263,8 +264,6 @@ struct ConnectionKey: Hashable {
263264
/// of concurrent requests as it has built-in politeness regarding the maximum number
264265
/// of concurrent requests to the server.
265266
class HTTP1ConnectionProvider {
266-
struct ProviderClosedError: Error {}
267-
268267
/// The client configuration used to bootstrap new requests
269268
private let configuration: HTTPClient.Configuration
270269

@@ -318,7 +317,7 @@ class HTTP1ConnectionProvider {
318317
self.state.assertInvariants()
319318
}
320319

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

496+
if available.isEmpty, leased.isEmpty {
497+
self.closePromise.succeed(())
498+
return self.closePromise.futureResult.map { clean }
499+
}
500+
497501
EventLoopFuture.andAllComplete(leased.map { $0.cancel() }, on: self.eventLoop).flatMap { _ in
498502
EventLoopFuture.andAllComplete(available.map { $0.close() }, on: self.eventLoop)
499503
}.whenFailure { error in

Diff for: Sources/AsyncHTTPClient/ConnectionsState.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension HTTP1ConnectionProvider {
6464
private var openedConnectionsCount: Int = 0
6565

6666
/// Number of enqueued requests, used to track if it is safe to delete the provider.
67-
private var pending: Int = 1
67+
private var pending: Int = 0
6868

6969
init(maximumConcurrentConnections: Int = 8, eventLoop: EventLoop) {
7070
self.maximumConcurrentConnections = maximumConcurrentConnections
@@ -148,7 +148,7 @@ extension HTTP1ConnectionProvider {
148148
return .none
149149
}
150150
case .closed:
151-
return .fail(waiter, ProviderClosedError())
151+
return .fail(waiter, HTTPClientError.alreadyShutdown)
152152
}
153153
}
154154

0 commit comments

Comments
 (0)