stub: simplify BlockingClientCall infinite blocking (#12217) · grpc/grpc-java@ba0a732

@@ -87,7 +87,7 @@ public final class BlockingClientCall<ReqT, RespT> {

8787

*/

8888

public RespT read() throws InterruptedException, StatusException {

8989

try {

90-

return read(true, 0, TimeUnit.NANOSECONDS);

90+

return read(true, 0);

9191

} catch (TimeoutException e) {

9292

throw new AssertionError("should never happen", e);

9393

}

@@ -106,16 +106,14 @@ public RespT read() throws InterruptedException, StatusException {

106106

*/

107107

public RespT read(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException,

108108

StatusException {

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)

113114

throws InterruptedException, TimeoutException, StatusException {

114-

long start = System.nanoTime();

115-

long end = start + unit.toNanos(timeout);

116-117115

Predicate<BlockingClientCall<ReqT, RespT>> predicate = BlockingClientCall::skipWaitingForRead;

118-

executor.waitAndDrainWithTimeout(waitForever, end, predicate, this);

116+

executor.waitAndDrainWithTimeout(waitForever, endNanoTime, predicate, this);

119117

RespT bufferedValue = buffer.poll();

120118121119

if (logger.isLoggable(Level.FINER)) {

@@ -182,7 +180,7 @@ public boolean hasNext() throws InterruptedException, StatusException {

182180

*/

183181

public boolean write(ReqT request) throws InterruptedException, StatusException {

184182

try {

185-

return write(true, request, Integer.MAX_VALUE, TimeUnit.DAYS);

183+

return write(true, request, 0);

186184

} catch (TimeoutException e) {

187185

throw new RuntimeException(e); // should never happen

188186

}

@@ -211,21 +209,20 @@ public boolean write(ReqT request) throws InterruptedException, StatusException

211209

*/

212210

public boolean write(ReqT request, long timeout, TimeUnit unit)

213211

throws 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)

218217

throws InterruptedException, TimeoutException, StatusException {

219218220219

if (writeClosed) {

221220

throw new IllegalStateException("Writes cannot be done after calling halfClose or cancel");

222221

}

223222224-

long end = System.nanoTime() + unit.toNanos(timeout);

225-226223

Predicate<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);

229226

Status savedClosedStatus = closedStatus;

230227

if (savedClosedStatus == null) {

231228

call.sendMessage(request);