-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClientRequestContext#cancel
cancels the associated request immediat…
…ely (#5800) Motivation: In order to handle #4591, I propose that we first define an API which allows users to cancel a request. Currently, `ClientRequestContext#cancel` is invoked once `CancellationScheduler#start` is invoked. I propose to change the behavior of `ClientRequestContext#cancel` such that the associated request is aborted immediately. Once this API is available, it would be trivial to implement `ResponseTimeoutMode` by adjusting where to call `CancellationScheduler#start`. Additionally, users may easily implement their own timeout mechanism if they would like. Modifications: - `CancellationScheduler` related changes - `DefaultCancellationScheduler` is refactored to be lock based instead of event loop based. The reasoning for this change was for the scenario where the request execution didn't reach the event loop yet. i.e. If a user calls `ctx.cancel` in the decorator, a connection shouldn't be opened. - e.g. https://github.com/line/armeria/blob/59aa40a59e1f1122716e70f9f1d6f1402a6aae0e/core/src/test/java/com/linecorp/armeria/client/ContextCancellationTest.java#L90-L116 - `CancellationScheduler#updateTask` is introduced. This API updates the cancellation task if the scheduler isn't invoked yet. If the scheduler is invoked already, the cancellation task will be executed eventually. This API allows `ctx.cancel` to attempt cancellation depending on which stage the request is at. For instance, at the decorators only `req.abort` needs to be called but at the request write stage, the cancellation task may need to send a reset signal. - Misc. an infinite timeout is internally represented as `Long.MAX_VALUE` instead of `0` - `AbstractHttpRequestHandler` related changes - `CancellationTask` in `AbstractHttpRequestHandler`, `HttpResponseWrapper`, `AbstractHttpResponseHandler` is modified to be scheduled inside an event loop. The reasoning is that `ctx.cancel`, and hence `CancellationTask#run` can be invoked from any thread. - There is a higher chance of `AbstractHttpRequestHandler` calling `fail` or `failAndReset` multiple times. There is no point in doing so, so added a boolean flag `failed` for safety. - `HttpResponseWrapper` related changes - The original intention of `cancelTimeoutAndLog` was to not log if the response is unexpected. Modified so that if the response is cancelled or the context is cancelled, no logging is done - There is probably no reason to not call `close` when a timeout occurs. Unified the fragmented logic of closing the `HttpResponseWrapper`. Result: - Users can call `ClientRequestContext#cancel` to cancel the ongoing request easily. - #5793 can be prepared for <!-- Visit this URL to learn more about how to write a pull request description: https://armeria.dev/community/developer-guide#how-to-write-pull-request-description --> --------- Co-authored-by: Ikhun Um <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,063 additions
and
576 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.