Skip to content
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

Support cancelling in flight REST requests #41990

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public void handle(RestClientRequestContext requestContext) {
future.subscribe().with(new Consumer<>() {
@Override
public void accept(HttpClientRequest httpClientRequest) {
requestContext.setHttpClientRequest(httpClientRequest);

// adapt headers to HTTP/2 depending on the underlying HTTP connection
ClientSendRequestHandler.this.adaptRequest(httpClientRequest);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jboss.resteasy.reactive.client.impl;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Supplier;

Expand All @@ -10,10 +12,11 @@
import jakarta.ws.rs.core.Response;

import io.smallrye.mutiny.Uni;
import io.vertx.core.http.HttpClientRequest;

public class UniInvoker extends AbstractRxInvoker<Uni<?>> {

private InvocationBuilderImpl invocationBuilder;
private final InvocationBuilderImpl invocationBuilder;

public UniInvoker(InvocationBuilderImpl invocationBuilder) {
this.invocationBuilder = invocationBuilder;
Expand All @@ -22,10 +25,16 @@ public UniInvoker(InvocationBuilderImpl invocationBuilder) {
@Override
public <R> Uni<R> method(String name, Entity<?> entity, GenericType<R> responseType) {
AsyncInvokerImpl invoker = (AsyncInvokerImpl) invocationBuilder.rx();
AtomicReference<RestClientRequestContext> restClientRequestContextRef = new AtomicReference<>();
return Uni.createFrom().completionStage(new Supplier<CompletionStage<R>>() {
@Override
public CompletionStage<R> get() {
return invoker.method(name, entity, responseType);
RestClientRequestContext restClientRequestContext = invoker.performRequestInternal(name, entity,
responseType == null ? new GenericType<>(String.class) : responseType,
true);
restClientRequestContextRef.set(restClientRequestContext);
CompletableFuture response = restClientRequestContext.getResult();
return invoker.mapResponse(response, responseType == null ? String.class : responseType.getRawType());
}
}).onFailure().transform(new Function<>() {
@Override
Expand All @@ -35,6 +44,18 @@ public Throwable apply(Throwable t) {
}
return t;
}
}).onCancellation().invoke(new Runnable() {
@Override
public void run() {
// be very defensive here as things could have been nulled out when the application is being torn down
RestClientRequestContext restClientRequestContext = restClientRequestContextRef.get();
if (restClientRequestContext != null) {
HttpClientRequest httpClientRequest = restClientRequestContext.getHttpClientRequest();
if (httpClientRequest != null) {
httpClientRequest.reset();
}
}
}
});
}

Expand Down
Loading