stub: simplify BlockingClientCall infinite blocking (#12217) · grpc/grpc-java@ba0a732
@@ -87,7 +87,7 @@ public final class BlockingClientCall<ReqT, RespT> {
8787 */
8888public RespT read() throws InterruptedException, StatusException {
8989try {
90-return read(true, 0, TimeUnit.NANOSECONDS);
90+return read(true, 0);
9191 } catch (TimeoutException e) {
9292throw new AssertionError("should never happen", e);
9393 }
@@ -106,16 +106,14 @@ public RespT read() throws InterruptedException, StatusException {
106106 */
107107public RespT read(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException,
108108StatusException {
109-return read(false, timeout, unit);
109+long endNanoTime = System.nanoTime() + unit.toNanos(timeout);
110+return read(false, endNanoTime);
110111 }
111112112-private RespT read(boolean waitForever, long timeout, TimeUnit unit)
113+private RespT read(boolean waitForever, long endNanoTime)
113114throws InterruptedException, TimeoutException, StatusException {
114-long start = System.nanoTime();
115-long end = start + unit.toNanos(timeout);
116-117115Predicate<BlockingClientCall<ReqT, RespT>> predicate = BlockingClientCall::skipWaitingForRead;
118-executor.waitAndDrainWithTimeout(waitForever, end, predicate, this);
116+executor.waitAndDrainWithTimeout(waitForever, endNanoTime, predicate, this);
119117RespT bufferedValue = buffer.poll();
120118121119if (logger.isLoggable(Level.FINER)) {
@@ -182,7 +180,7 @@ public boolean hasNext() throws InterruptedException, StatusException {
182180 */
183181public boolean write(ReqT request) throws InterruptedException, StatusException {
184182try {
185-return write(true, request, Integer.MAX_VALUE, TimeUnit.DAYS);
183+return write(true, request, 0);
186184 } catch (TimeoutException e) {
187185throw new RuntimeException(e); // should never happen
188186 }
@@ -211,21 +209,20 @@ public boolean write(ReqT request) throws InterruptedException, StatusException
211209 */
212210public boolean write(ReqT request, long timeout, TimeUnit unit)
213211throws InterruptedException, TimeoutException, StatusException {
214-return write(false, request, timeout, unit);
212+long endNanoTime = System.nanoTime() + unit.toNanos(timeout);
213+return write(false, request, endNanoTime);
215214 }
216215217-private boolean write(boolean waitForever, ReqT request, long timeout, TimeUnit unit)
216+private boolean write(boolean waitForever, ReqT request, long endNanoTime)
218217throws InterruptedException, TimeoutException, StatusException {
219218220219if (writeClosed) {
221220throw new IllegalStateException("Writes cannot be done after calling halfClose or cancel");
222221 }
223222224-long end = System.nanoTime() + unit.toNanos(timeout);
225-226223Predicate<BlockingClientCall<ReqT, RespT>> predicate =
227224 (x) -> x.call.isReady() || x.closedStatus != null;
228-executor.waitAndDrainWithTimeout(waitForever, end, predicate, this);
225+executor.waitAndDrainWithTimeout(waitForever, endNanoTime, predicate, this);
229226Status savedClosedStatus = closedStatus;
230227if (savedClosedStatus == null) {
231228call.sendMessage(request);