Skip to content

Commit 828c3f4

Browse files
committed
Fix fixmes: fail task when client is shutdown
1 parent 29682e7 commit 828c3f4

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

Sources/AsyncHTTPClient/ConnectionPool.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ class ConnectionPool {
404404
}
405405
}
406406

407-
// FIXME: Should this be a precondition or an assertion?
408407
private func preconditionIsOpened() {
409408
self.stateLock.withLock {
410409
precondition(self.state.isClosed == false, "Attempting to use closed HTTP1ConnectionProvider")

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,6 @@ public class HTTPClient {
262262
delegate: Delegate,
263263
eventLoop eventLoopPreference: EventLoopPreference,
264264
deadline: NIODeadline? = nil) -> Task<Delegate.Response> {
265-
// FIXME: Is this correct to assert?
266-
self.stateLock.withLock {
267-
switch state {
268-
case .upAndRunning:
269-
break
270-
case .shuttingDown, .shutDown:
271-
assertionFailure("Attempting to execute a request on shutdown client")
272-
}
273-
}
274-
275265
let taskEL: EventLoop
276266
switch eventLoopPreference.preference {
277267
case .indifferent:
@@ -286,6 +276,19 @@ public class HTTPClient {
286276
taskEL = delegateEL
287277
}
288278

279+
let failedTask: Task<Delegate.Response>? = self.stateLock.withLock {
280+
switch state {
281+
case .upAndRunning:
282+
return nil
283+
case .shuttingDown, .shutDown:
284+
return Task<Delegate.Response>.failedTask(eventLoop: taskEL, error: HTTPClientError.alreadyShutdown)
285+
}
286+
}
287+
288+
if let failedTask = failedTask {
289+
return failedTask
290+
}
291+
289292
let redirectHandler: RedirectHandler<Delegate.Response>?
290293
switch self.configuration.redirectConfiguration.configuration {
291294
case .follow(let max, let allowCycles):

Sources/AsyncHTTPClient/HTTPHandler.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ extension HTTPClient {
498498
self.lock = Lock()
499499
}
500500

501+
static func failedTask(eventLoop: EventLoop, error: Error) -> Task<Response> {
502+
let task = self.init(eventLoop: eventLoop)
503+
task.promise.fail(error)
504+
return task
505+
}
506+
501507
/// `EventLoopFuture` for the response returned by this request.
502508
public var futureResult: EventLoopFuture<Response> {
503509
return self.promise.futureResult
@@ -564,7 +570,9 @@ extension HTTPClient {
564570
}
565571

566572
} else {
567-
// TODO: This is only reached in some internal unit t
573+
// TODO: This seems only reached in some internal unit test
574+
// Maybe there could be a better handling in the future to make
575+
// it an error outside of testing contexts
568576
return self.eventLoop.makeSucceededFuture(())
569577
}
570578
}

0 commit comments

Comments
 (0)