diff options
author | Carl Mastrangelo <notcarl@google.com> | 2017-11-01 17:15:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 17:15:37 -0700 |
commit | 37a67bf04111c0a9d42a7722b89bab028c882000 (patch) | |
tree | 49bacfd2c49e6ff61946258abffe95ff5a2d234b /stub | |
parent | 2e992145480560e293b77adcceb528b492fd3fda (diff) | |
download | grpc-grpc-java-37a67bf04111c0a9d42a7722b89bab028c882000.tar.gz |
stub: clean up call cancellation
Diffstat (limited to 'stub')
-rw-r--r-- | stub/src/main/java/io/grpc/stub/ClientCalls.java | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/stub/src/main/java/io/grpc/stub/ClientCalls.java b/stub/src/main/java/io/grpc/stub/ClientCalls.java index f26796ce0..41e03a994 100644 --- a/stub/src/main/java/io/grpc/stub/ClientCalls.java +++ b/stub/src/main/java/io/grpc/stub/ClientCalls.java @@ -47,6 +47,9 @@ import javax.annotation.Nullable; * that the runtime can vary behavior without requiring regeneration of the stub. */ public final class ClientCalls { + + private static final Logger logger = Logger.getLogger(ClientCalls.class.getName()); + // Prevent instantiation private ClientCalls() {} @@ -96,9 +99,10 @@ public final class ClientCalls { public static <ReqT, RespT> RespT blockingUnaryCall(ClientCall<ReqT, RespT> call, ReqT param) { try { return getUnchecked(futureUnaryCall(call, param)); - } catch (Throwable t) { - call.cancel(null, t); - throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); + } catch (RuntimeException e) { + throw cancelThrow(call, e); + } catch (Error e) { + throw cancelThrow(call, e); } } @@ -118,13 +122,17 @@ public final class ClientCalls { executor.waitAndDrain(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw Status.CANCELLED.withCause(e).asRuntimeException(); + throw Status.CANCELLED + .withDescription("Call was interrupted") + .withCause(e) + .asRuntimeException(); } } return getUnchecked(responseFuture); - } catch (Throwable t) { - call.cancel(null, t); - throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); + } catch (RuntimeException e) { + throw cancelThrow(call, e); + } catch (Error e) { + throw cancelThrow(call, e); } } @@ -186,9 +194,12 @@ public final class ClientCalls { return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw Status.CANCELLED.withCause(e).asRuntimeException(); + throw Status.CANCELLED + .withDescription("Call was interrupted") + .withCause(e) + .asRuntimeException(); } catch (ExecutionException e) { - throw toStatusRuntimeException(e); + throw toStatusRuntimeException(e.getCause()); } } @@ -214,6 +225,27 @@ public final class ClientCalls { return Status.UNKNOWN.withCause(t).asRuntimeException(); } + /** + * Cancels a call, and throws the exception. + * + * @param t must be a RuntimeException or Error + */ + private static RuntimeException cancelThrow(ClientCall<?, ?> call, Throwable t) { + try { + call.cancel(null, t); + } catch (Throwable e) { + assert e instanceof RuntimeException || e instanceof Error; + logger.log(Level.SEVERE, "RuntimeException encountered while closing call", e); + } + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } else if (t instanceof Error) { + throw (Error) t; + } + // should be impossible + throw new AssertionError(t); + } + private static <ReqT, RespT> void asyncUnaryRequestCall( ClientCall<ReqT, RespT> call, ReqT param, StreamObserver<RespT> responseObserver, boolean streamingResponse) { @@ -236,9 +268,10 @@ public final class ClientCalls { try { call.sendMessage(param); call.halfClose(); - } catch (Throwable t) { - call.cancel(null, t); - throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); + } catch (RuntimeException e) { + throw cancelThrow(call, e); + } catch (Error e) { + throw cancelThrow(call, e); } } |