You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ClientRequestContext#cancel cancels the associated request immediately (#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]>
0 commit comments