mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Handle CancellationException in JdkClientHttpRequest
Handle CancellationException in order to throw an HttpTimeoutException when the timeout handler caused the cancellation. See gh-34721 Signed-off-by: giampaolo <giampaorr@gmail.com> fix: use timeoutHandler with a flag isTimeout Closes gh-33973 Signed-off-by: giampaolo <giampaorr@gmail.com>
This commit is contained in:
@@ -37,6 +37,7 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Flow;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -97,12 +98,13 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||
@SuppressWarnings("NullAway")
|
||||
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
|
||||
CompletableFuture<HttpResponse<InputStream>> responseFuture = null;
|
||||
TimeoutHandler timeoutHandler = null;
|
||||
try {
|
||||
HttpRequest request = buildRequest(headers, body);
|
||||
responseFuture = this.httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
|
||||
if (this.timeout != null) {
|
||||
TimeoutHandler timeoutHandler = new TimeoutHandler(responseFuture, this.timeout);
|
||||
timeoutHandler = new TimeoutHandler(responseFuture, this.timeout);
|
||||
HttpResponse<InputStream> response = responseFuture.get();
|
||||
InputStream inputStream = timeoutHandler.wrapInputStream(response);
|
||||
return new JdkClientHttpResponse(response, inputStream);
|
||||
@@ -121,7 +123,10 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||
Throwable cause = ex.getCause();
|
||||
|
||||
if (cause instanceof CancellationException) {
|
||||
throw new HttpTimeoutException("Request timed out");
|
||||
if (timeoutHandler != null && timeoutHandler.isTimeout()) {
|
||||
throw new HttpTimeoutException("Request timed out");
|
||||
}
|
||||
throw new IOException("Request was cancelled");
|
||||
}
|
||||
if (cause instanceof UncheckedIOException uioEx) {
|
||||
throw uioEx.getCause();
|
||||
@@ -136,6 +141,12 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||
throw new IOException(cause.getMessage(), cause);
|
||||
}
|
||||
}
|
||||
catch (CancellationException ex) {
|
||||
if (timeoutHandler != null && timeoutHandler.isTimeout()) {
|
||||
throw new HttpTimeoutException("Request timed out");
|
||||
}
|
||||
throw new IOException("Request was cancelled");
|
||||
}
|
||||
}
|
||||
|
||||
private HttpRequest buildRequest(HttpHeaders headers, @Nullable Body body) {
|
||||
@@ -233,6 +244,7 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||
private static final class TimeoutHandler {
|
||||
|
||||
private final CompletableFuture<Void> timeoutFuture;
|
||||
private final AtomicBoolean isTimeout = new AtomicBoolean(false);
|
||||
|
||||
private TimeoutHandler(CompletableFuture<HttpResponse<InputStream>> future, Duration timeout) {
|
||||
|
||||
@@ -241,6 +253,7 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||
|
||||
this.timeoutFuture.thenRun(() -> {
|
||||
if (future.cancel(true) || future.isCompletedExceptionally() || !future.isDone()) {
|
||||
isTimeout.set(true);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -268,6 +281,10 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public boolean isTimeout() {
|
||||
return isTimeout.get();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user