@@ -227,7 +227,7 @@ internal class ResponseAccumulator: HTTPClientResponseDelegate {
227
227
case . error:
228
228
break
229
229
}
230
- return task. eventLoop . makeSucceededFuture ( ( ) )
230
+ return task. currentEventLoop . makeSucceededFuture ( ( ) )
231
231
}
232
232
233
233
func didReceiveBodyPart( task: HTTPClient . Task < Response > , _ part: ByteBuffer ) -> EventLoopFuture < Void > {
@@ -245,7 +245,7 @@ internal class ResponseAccumulator: HTTPClientResponseDelegate {
245
245
case . error:
246
246
break
247
247
}
248
- return task. eventLoop . makeSucceededFuture ( ( ) )
248
+ return task. currentEventLoop . makeSucceededFuture ( ( ) )
249
249
}
250
250
251
251
func didReceiveError( task: HTTPClient . Task < Response > , _ error: Error ) {
@@ -343,9 +343,9 @@ extension HTTPClientResponseDelegate {
343
343
344
344
public func didSendRequest( task: HTTPClient . Task < Response > ) { }
345
345
346
- public func didReceiveHead( task: HTTPClient . Task < Response > , _: HTTPResponseHead ) -> EventLoopFuture < Void > { return task. eventLoop . makeSucceededFuture ( ( ) ) }
346
+ public func didReceiveHead( task: HTTPClient . Task < Response > , _: HTTPResponseHead ) -> EventLoopFuture < Void > { return task. currentEventLoop . makeSucceededFuture ( ( ) ) }
347
347
348
- public func didReceiveBodyPart( task: HTTPClient . Task < Response > , _: ByteBuffer ) -> EventLoopFuture < Void > { return task. eventLoop . makeSucceededFuture ( ( ) ) }
348
+ public func didReceiveBodyPart( task: HTTPClient . Task < Response > , _: ByteBuffer ) -> EventLoopFuture < Void > { return task. currentEventLoop . makeSucceededFuture ( ( ) ) }
349
349
350
350
public func didReceiveError( task: HTTPClient . Task < Response > , _: Error ) { }
351
351
}
@@ -366,15 +366,23 @@ extension HTTPClient {
366
366
/// `EventLoopFuture<Response>` of the execution or cancellation of the execution.
367
367
public final class Task < Response> {
368
368
/// `EventLoop` used to execute and process this request.
369
- public let eventLoop : EventLoop
370
- let promise : EventLoopPromise < Response >
369
+ public var currentEventLoop : EventLoop {
370
+ return self . lock. withLock {
371
+ _currentEventLoop
372
+ }
373
+ }
371
374
375
+ /// The stored property used by `currentEventLoop` in combination with the `lock`
376
+ ///
377
+ /// In most cases you should use `currentEventLoop` instead
378
+ private var _currentEventLoop : EventLoop
379
+ let promise : EventLoopPromise < Response >
372
380
private var channel : Channel ?
373
381
private var cancelled : Bool
374
382
private let lock : Lock
375
383
376
- public init ( eventLoop: EventLoop ) {
377
- self . eventLoop = eventLoop
384
+ init ( eventLoop: EventLoop ) {
385
+ self . _currentEventLoop = eventLoop
378
386
self . promise = eventLoop. makePromise ( )
379
387
self . cancelled = false
380
388
self . lock = Lock ( )
@@ -405,8 +413,8 @@ extension HTTPClient {
405
413
406
414
@discardableResult
407
415
func setChannel( _ channel: Channel ) -> Channel {
408
- precondition ( self . eventLoop === channel. eventLoop, " Channel must use same event loop as this task. " )
409
416
return self . lock. withLock {
417
+ self . _currentEventLoop = channel. eventLoop
410
418
self . channel = channel
411
419
return channel
412
420
}
@@ -539,9 +547,11 @@ internal class TaskHandler<T: HTTPClientResponseDelegate>: ChannelInboundHandler
539
547
} else {
540
548
self . state = . head
541
549
self . mayRead = false
542
- self . delegate. didReceiveHead ( task: self . task, head) . whenComplete { result in
543
- self . handleBackpressureResult ( context: context, result: result)
544
- }
550
+ self . delegate. didReceiveHead ( task: self . task, head)
551
+ . hop ( to: context. eventLoop)
552
+ . whenComplete { result in
553
+ self . handleBackpressureResult ( context: context, result: result)
554
+ }
545
555
}
546
556
case . body( let body) :
547
557
switch self . state {
@@ -550,9 +560,11 @@ internal class TaskHandler<T: HTTPClientResponseDelegate>: ChannelInboundHandler
550
560
default :
551
561
self . state = . body
552
562
self . mayRead = false
553
- self . delegate. didReceiveBodyPart ( task: self . task, body) . whenComplete { result in
554
- self . handleBackpressureResult ( context: context, result: result)
555
- }
563
+ self . delegate. didReceiveBodyPart ( task: self . task, body)
564
+ . hop ( to: context. eventLoop)
565
+ . whenComplete { result in
566
+ self . handleBackpressureResult ( context: context, result: result)
567
+ }
556
568
}
557
569
case . end:
558
570
switch self . state {
0 commit comments