Skip to content

Commit eee7223

Browse files
committed
Fixes for close callback sync
1 parent d08fb71 commit eee7223

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Sources/AsyncHTTPClient/ConnectionPool.swift

+12-10
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,9 @@ final class ConnectionPool {
177177
self.parentPool.release(self)
178178
}
179179

180-
fileprivate func close(runCloseCallback: Bool = true) -> EventLoopFuture<Void> {
181-
return self.channel.eventLoop.flatSubmit {
182-
self.shouldRunCloseCallback = runCloseCallback
183-
self.channel.close(promise: nil)
184-
return self.closeFuture
185-
}
180+
func close() -> EventLoopFuture<Void> {
181+
self.channel.close(promise: nil)
182+
return self.closeFuture
186183
}
187184

188185
var description: String {
@@ -213,7 +210,11 @@ final class ConnectionPool {
213210
/// Indicates that this connection is about to close
214211
var isClosing: Bool = false
215212

216-
fileprivate var shouldRunCloseCallback: Bool = true
213+
/// Indicates wether the usual close callback should be run or not, this allows customizing what happens
214+
/// on close in some cases such as for the `.replaceConnection` action
215+
///
216+
/// - Warning: This should be accessed under the `stateLock` of `HTTP1ConnectionProvider`
217+
fileprivate var mustRunDefaultCloseCallback: Bool = true
217218

218219
/// Convenience property indicating wether the underlying `Channel` is active or not
219220
var isActiveEstimation: Bool {
@@ -314,7 +315,7 @@ final class ConnectionPool {
314315
self.makeConnection(on: eventLoop).cascade(to: promise)
315316

316317
case .replaceConnection(let eventLoop, let promise):
317-
connection.close(runCloseCallback: false).flatMap {
318+
connection.close().flatMap {
318319
self.makeConnection(on: eventLoop)
319320
}.whenComplete { result in
320321
switch result {
@@ -382,7 +383,7 @@ final class ConnectionPool {
382383
connection.channel.closeFuture.whenComplete { result in
383384
let action: HTTP1ConnectionProvider.State.ClosedConnectionRemoveAction? = self.parentPool.connectionProvidersLock.withLock {
384385
self.stateLock.withLock {
385-
guard connection.shouldRunCloseCallback else {
386+
guard connection.mustRunDefaultCloseCallback else {
386387
return nil
387388
}
388389
switch result {
@@ -420,7 +421,7 @@ final class ConnectionPool {
420421
let waitersPromises = waitersCopy.map { $0.promise }
421422
let waitersFutures = waitersPromises.map { $0.futureResult }
422423
waitersPromises.forEach { $0.fail(HTTPClientError.cancelled) }
423-
let closeFutures = self.state.availableConnections.map { $0.channel.close() }
424+
let closeFutures = self.state.availableConnections.map { $0.close() }
424425
return (waitersFutures, closeFutures)
425426
}
426427
try? EventLoopFuture<Connection>.andAllComplete(waitersFutures, on: self.eventLoop).wait()
@@ -524,6 +525,7 @@ final class ConnectionPool {
524525
return .succeed(firstWaiter.promise)
525526
} else {
526527
if requiresSpecifiedEL {
528+
connection.mustRunDefaultCloseCallback = false
527529
return .replaceConnection(channelEL, firstWaiter.promise)
528530
} else {
529531
return .makeConnectionAndComplete(channelEL, firstWaiter.promise)

Sources/AsyncHTTPClient/HTTPHandler.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ extension HTTPClient {
557557

558558
func fail<Delegate: HTTPClientResponseDelegate>(with error: Error, delegateType: Delegate.Type) {
559559
if let connection = self.connection {
560-
connection.channel.close().whenComplete { _ in
560+
connection.close().whenComplete { _ in
561561
self.releaseAssociatedConnection(delegateType: delegateType).whenComplete { _ in
562562
self.promise.fail(error)
563563
}

0 commit comments

Comments
 (0)