Skip to content

cancel should be done with outbound user event #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,14 @@ extension HTTPClient {

/// Cancels the request execution.
public func cancel() {
self.lock.withLock {
let channel: Channel? = self.lock.withLock {
if !cancelled {
cancelled = true
channel?.pipeline.fireUserInboundEventTriggered(TaskCancelEvent())
return self.channel
}
return nil
}
channel?.triggerUserOutboundEvent(TaskCancelEvent(), promise: nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artemredkin I'm pretty sure this doesn't actually work (which proves there's no test) because whatever ChannelHandler is supposed to catch this event needs to change from inbound to outbound user event.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to add a test that verifies cancellation works.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we definitely have the test, it just never finishes :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed! and tests pass now

}

@discardableResult
Expand Down Expand Up @@ -732,12 +734,19 @@ extension TaskHandler: ChannelDuplexHandler {
self.state = .end
let error = HTTPClientError.readTimeout
self.failTaskAndNotifyDelegate(error: error, self.delegate.didReceiveError)
} else if (event as? TaskCancelEvent) != nil {
} else {
context.fireUserInboundEventTriggered(event)
}
}

func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
if (event as? TaskCancelEvent) != nil {
self.state = .end
let error = HTTPClientError.cancelled
self.failTaskAndNotifyDelegate(error: error, self.delegate.didReceiveError)
promise?.succeed(())
} else {
context.fireUserInboundEventTriggered(event)
context.triggerUserOutboundEvent(event, promise: promise)
}
}

Expand Down