When using fetch
in the frontend, it is possible to abort the request using an AbortController
.
AFAIK (but I might be wrong on this), when using HTTP2, aborting the request sends an RST_FRAME
to the backend to indicate that the request is aborted (https://stackoverflow.com/a/28796790/1076463).
I was hoping that I could somehow listen/query for this in a Spring Boot @RestController
in the backend. This would allow to stop calculating a result that the client is no longer interested in.
Anybody knows if this is possible for HTTP2, and if so, how ?
I already found this question stating that it is not possible, but that seems to only apply to HTTP1 requests while I'm using HTTP2.
Setup: Spring Boot 2.3.0 / Java 11 / embedded Tomcat webserver / HTTP2
Edit:
with some debugging, I found that the information that the RST_FRAME
is received is available in Tomcat, but I don't see a way to access it except using a bunch of reflection.
In the HttpServletResponse
you receive in @RestController
, you find the information on the following path:
((StreamProcessor) ((Response) ((ResponseFacade) ((FirewalledResponse) ((HeaderWriterFilter.HeaderWriterResponse) ((ShallowEtagHeaderFilter.ConditionalContentCachingResponseWrapper) response).getResponse()).getResponse()).getResponse()).response).coyoteResponse.hook).stream.state.canWrite()
That last canWrite
call is org.apache.coyote.http2.StreamStateMachine#canWrite
which delegates to org.apache.coyote.http2.StreamStateMachine.State#canWrite
.
And that state is set to State#CLOSED_RST_TX
when the RST_FRAME
is received, which indicates that the stream is no longer writable.