@@ -320,43 +320,49 @@ public class HTTPClient {
320
320
321
321
let connection = self . pool. getConnection ( for: request, preference: eventLoopPreference, on: taskEL, deadline: deadline)
322
322
323
- connection. flatMap { connection -> EventLoopFuture < Void > in
324
- let channel = connection. channel
325
- let addedFuture : EventLoopFuture < Void >
326
-
327
- switch self . configuration. decompression {
328
- case . disabled:
329
- addedFuture = channel. eventLoop. makeSucceededFuture ( ( ) )
330
- case . enabled( let limit) :
331
- let decompressHandler = NIOHTTPResponseDecompressor ( limit: limit)
332
- addedFuture = channel. pipeline. addHandler ( decompressHandler, name: " decompressHandler " )
333
- }
334
-
335
- return addedFuture. flatMap {
336
- if let timeout = self . resolve ( timeout: self . configuration. timeout. read, deadline: deadline) {
337
- return channel. pipeline. addHandler ( IdleStateHandler ( readTimeout: timeout) , name: " timeoutHandler " )
338
- } else {
339
- return channel. eventLoop. makeSucceededFuture ( ( ) )
340
- }
341
- } . flatMap {
342
- let taskHandler = TaskHandler ( task: task, delegate: delegate, redirectHandler: redirectHandler, ignoreUncleanSSLShutdown: self . configuration. ignoreUncleanSSLShutdown)
343
- return channel. pipeline. addHandler ( taskHandler, name: " taskHandler " )
344
- } . flatMap {
345
- task. setConnection ( connection)
346
-
347
- let isCancelled = task. lock. withLock {
348
- task. cancelled
323
+ _ = connection
324
+ . flatMap { connection -> EventLoopFuture < Void > in
325
+ let channel = connection. channel
326
+ let addedFuture : EventLoopFuture < Void >
327
+
328
+ switch self . configuration. decompression {
329
+ case . disabled:
330
+ addedFuture = channel. eventLoop. makeSucceededFuture ( ( ) )
331
+ case . enabled( let limit) :
332
+ let decompressHandler = NIOHTTPResponseDecompressor ( limit: limit)
333
+ addedFuture = channel. pipeline. addHandler ( decompressHandler)
349
334
}
350
335
351
- if !isCancelled {
352
- return channel. writeAndFlush ( request)
353
- } else {
354
- return channel. eventLoop. makeSucceededFuture ( ( ) )
336
+ return addedFuture. flatMap {
337
+ if let timeout = self . resolve ( timeout: self . configuration. timeout. read, deadline: deadline) {
338
+ return channel. pipeline. addHandler ( IdleStateHandler ( readTimeout: timeout) )
339
+ } else {
340
+ return channel. eventLoop. makeSucceededFuture ( ( ) )
341
+ }
342
+ } . flatMap {
343
+ let taskHandler = TaskHandler ( task: task, delegate: delegate, redirectHandler: redirectHandler, ignoreUncleanSSLShutdown: self . configuration. ignoreUncleanSSLShutdown)
344
+ return channel. pipeline. addHandler ( taskHandler)
345
+ } . flatMap {
346
+ task. setConnection ( connection)
347
+
348
+ let isCancelled = task. lock. withLock {
349
+ task. cancelled
350
+ }
351
+
352
+ if !isCancelled {
353
+ return channel. writeAndFlush ( request) . flatMapError { _ in
354
+ // At this point the `TaskHandler` will already be present
355
+ // to handle the failure and pass it to the `promise`
356
+ channel. eventLoop. makeSucceededFuture ( ( ) )
357
+ }
358
+ } else {
359
+ return channel. eventLoop. makeSucceededFuture ( ( ) )
360
+ }
355
361
}
362
+ } . whenFailure {
363
+ promise. fail ( $0)
356
364
}
357
- } . whenFailure {
358
- promise. fail ( $0)
359
- }
365
+
360
366
return task
361
367
}
362
368
0 commit comments