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 5c3d2a4 commit a57a3a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 42 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) {
CompletableFuture<Object> resultFuture = new CompletableFuture<>();
resultFuture.completeExceptionally(e);
return resultFuture;
}

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, logLevel, response, elapsedTime);
if (returnType == Response.class) {
Expand Down
17 changes: 2 additions & 15 deletions core/src/main/java/feign/SynchronousMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,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 a57a3a9

Please sign in to comment.