Skip to content

Commit

Permalink
Remove handleResponse that returning CompletableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
wplong11 committed Oct 10, 2022
1 parent 659ceef commit 9aa5dc0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
17 changes: 8 additions & 9 deletions core/src/main/java/feign/AsynchronousMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,15 @@ private static Response ensureRequestIsSet(Response response,
}

private CompletableFuture<Object> handleResponse(Response response, long elapsedTime) {
CompletableFuture<Object> resultFuture = new CompletableFuture<>();

responseHandler.handleResponse(resultFuture, metadata.configKey(), response,
methodInfo.underlyingReturnType(), elapsedTime);

if (!resultFuture.isDone()) {
resultFuture.completeExceptionally(new IllegalStateException("Response handling not done"));
try {
final Object result = responseHandler.handleResponse(
metadata.configKey(), response, methodInfo.underlyingReturnType(), elapsedTime);
return CompletableFuture.completedFuture(result);
} catch (final Exception e) {
final CompletableFuture<Object> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(e);
return failedFuture;
}

return resultFuture;
}

private long elapsedTime(long start) {
Expand Down
22 changes: 4 additions & 18 deletions core/src/main/java/feign/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,10 @@ public ResponseHandler(Level logLevel, Logger logger, Decoder decoder,
this.responseInterceptor = responseInterceptor;
}

public void handleResponse(CompletableFuture<Object> resultFuture,
String configKey,
Response response,
Type returnType,
long elapsedTime) {
try {
resultFuture.complete(
handleResponse(configKey, response, returnType, elapsedTime)
);
} catch (Exception e) {
resultFuture.completeExceptionally(e);
}
}

private Object handleResponse(String configKey,
Response response,
Type returnType,
long elapsedTime) throws Exception {
public Object handleResponse(String configKey,
Response response,
Type returnType,
long elapsedTime) throws Exception {
try {
response = logAndRebufferResponseIfNeeded(configKey, response, elapsedTime);
if (returnType == Response.class) {
Expand Down
22 changes: 2 additions & 20 deletions core/src/main/java/feign/SynchronousMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@
import feign.codec.ErrorDecoder;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

final class SynchronousMethodHandler implements MethodHandler {

private static final long MAX_RESPONSE_BUFFER_SIZE = 8192L;

private final MethodMetadata metadata;
private final Target<?> target;
private final Client client;
Expand Down Expand Up @@ -117,22 +112,9 @@ Object executeAndDecode(RequestTemplate template, Options options) throws Throwa
}
throw errorExecuting(request, e);
}
long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);

CompletableFuture<Object> resultFuture = new CompletableFuture<>();
responseHandler.handleResponse(resultFuture, metadata.configKey(), response,
metadata.returnType(), elapsedTime);

try {
if (!resultFuture.isDone())
throw new IllegalStateException("Response handling not done");
return resultFuture.join();
} catch (CompletionException e) {
Throwable cause = e.getCause();
if (cause != null)
throw cause;
throw e;
}
long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
return responseHandler.handleResponse(metadata.configKey(), response, metadata.returnType(), elapsedTime);
}

long elapsedTime(long start) {
Expand Down

0 comments on commit 9aa5dc0

Please sign in to comment.