@@ -87,7 +87,7 @@ public final class BlockingClientCall<ReqT, RespT> {
87
87
*/
88
88
public RespT read () throws InterruptedException , StatusException {
89
89
try {
90
- return read (true , 0 , TimeUnit . NANOSECONDS );
90
+ return read (true , 0 );
91
91
} catch (TimeoutException e ) {
92
92
throw new AssertionError ("should never happen" , e );
93
93
}
@@ -106,16 +106,14 @@ public RespT read() throws InterruptedException, StatusException {
106
106
*/
107
107
public RespT read (long timeout , TimeUnit unit ) throws InterruptedException , TimeoutException ,
108
108
StatusException {
109
- return read (false , timeout , unit );
109
+ long endNanoTime = System .nanoTime () + unit .toNanos (timeout );
110
+ return read (false , endNanoTime );
110
111
}
111
112
112
- private RespT read (boolean waitForever , long timeout , TimeUnit unit )
113
+ private RespT read (boolean waitForever , long endNanoTime )
113
114
throws InterruptedException , TimeoutException , StatusException {
114
- long start = System .nanoTime ();
115
- long end = start + unit .toNanos (timeout );
116
-
117
115
Predicate <BlockingClientCall <ReqT , RespT >> predicate = BlockingClientCall ::skipWaitingForRead ;
118
- executor .waitAndDrainWithTimeout (waitForever , end , predicate , this );
116
+ executor .waitAndDrainWithTimeout (waitForever , endNanoTime , predicate , this );
119
117
RespT bufferedValue = buffer .poll ();
120
118
121
119
if (logger .isLoggable (Level .FINER )) {
@@ -182,7 +180,7 @@ public boolean hasNext() throws InterruptedException, StatusException {
182
180
*/
183
181
public boolean write (ReqT request ) throws InterruptedException , StatusException {
184
182
try {
185
- return write (true , request , Integer . MAX_VALUE , TimeUnit . DAYS );
183
+ return write (true , request , 0 );
186
184
} catch (TimeoutException e ) {
187
185
throw new RuntimeException (e ); // should never happen
188
186
}
@@ -211,21 +209,20 @@ public boolean write(ReqT request) throws InterruptedException, StatusException
211
209
*/
212
210
public boolean write (ReqT request , long timeout , TimeUnit unit )
213
211
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 );
215
214
}
216
215
217
- private boolean write (boolean waitForever , ReqT request , long timeout , TimeUnit unit )
216
+ private boolean write (boolean waitForever , ReqT request , long endNanoTime )
218
217
throws InterruptedException , TimeoutException , StatusException {
219
218
220
219
if (writeClosed ) {
221
220
throw new IllegalStateException ("Writes cannot be done after calling halfClose or cancel" );
222
221
}
223
222
224
- long end = System .nanoTime () + unit .toNanos (timeout );
225
-
226
223
Predicate <BlockingClientCall <ReqT , RespT >> predicate =
227
224
(x ) -> x .call .isReady () || x .closedStatus != null ;
228
- executor .waitAndDrainWithTimeout (waitForever , end , predicate , this );
225
+ executor .waitAndDrainWithTimeout (waitForever , endNanoTime , predicate , this );
229
226
Status savedClosedStatus = closedStatus ;
230
227
if (savedClosedStatus == null ) {
231
228
call .sendMessage (request );
0 commit comments