41
41
import org .asynchttpclient .providers .netty .DiscardEvent ;
42
42
import org .asynchttpclient .providers .netty .channel .Channels ;
43
43
import org .asynchttpclient .providers .netty .request .NettyRequest ;
44
+ import org .asynchttpclient .providers .netty .request .timeout .TimeoutsHolder ;
44
45
import org .slf4j .Logger ;
45
46
import org .slf4j .LoggerFactory ;
46
47
@@ -59,7 +60,8 @@ public enum STATE {
59
60
}
60
61
61
62
private final int requestTimeoutInMs ;
62
- private final AsyncHttpClientConfig config ;
63
+ private volatile boolean requestTimeoutReached ;
64
+ private volatile boolean idleConnectionTimeoutReached ;
63
65
private final long start = millisTime ();
64
66
private final ConnectionPoolKeyStrategy connectionPoolKeyStrategy ;
65
67
private final ProxyServer proxyServer ;
@@ -80,7 +82,7 @@ public enum STATE {
80
82
private final AtomicBoolean throwableCalled = new AtomicBoolean (false );
81
83
private final AtomicReference <V > content = new AtomicReference <V >();
82
84
private final AtomicReference <ExecutionException > exEx = new AtomicReference <ExecutionException >();
83
- private volatile FutureReaper reaperFuture ;
85
+ private volatile TimeoutsHolder timeoutsHolder ;
84
86
85
87
// state mutated only inside the event loop
86
88
private Channel channel ;
@@ -111,7 +113,6 @@ public NettyResponseFuture(URI uri,//
111
113
this .request = request ;
112
114
this .nettyRequest = nettyRequest ;
113
115
this .uri = uri ;
114
- this .config = config ;
115
116
this .connectionPoolKeyStrategy = connectionPoolKeyStrategy ;
116
117
this .proxyServer = proxyServer ;
117
118
@@ -156,7 +157,7 @@ public void setAsyncHandler(AsyncHandler<V> asyncHandler) {
156
157
157
158
@ Override
158
159
public boolean cancel (boolean force ) {
159
- cancelReaper ();
160
+ cancelTimeouts ();
160
161
161
162
if (isCancelled .get ())
162
163
return false ;
@@ -186,31 +187,39 @@ public boolean cancel(boolean force) {
186
187
* @return <code>true</code> if response has expired and should be terminated.
187
188
*/
188
189
public boolean hasExpired () {
189
- long now = millisTime ();
190
- return hasConnectionIdleTimedOut (now ) || hasRequestTimedOut (now );
190
+ return requestTimeoutReached || idleConnectionTimeoutReached ;
191
191
}
192
192
193
- public boolean hasConnectionIdleTimedOut ( long now ) {
194
- return config . getIdleConnectionTimeoutInMs () != - 1 && ( now - touch . get ()) >= config . getIdleConnectionTimeoutInMs () ;
193
+ public void setRequestTimeoutReached ( ) {
194
+ this . requestTimeoutReached = true ;
195
195
}
196
196
197
- public boolean hasRequestTimedOut (long now ) {
198
- return requestTimeoutInMs != -1 && (now - start ) >= requestTimeoutInMs ;
197
+ public boolean isRequestTimeoutReached () {
198
+ return requestTimeoutReached ;
199
+ }
200
+
201
+ public void setIdleConnectionTimeoutReached () {
202
+ this .idleConnectionTimeoutReached = true ;
203
+ }
204
+
205
+ public boolean isIdleConnectionTimeoutReached () {
206
+ return idleConnectionTimeoutReached ;
199
207
}
200
208
201
209
@ Override
202
210
public V get () throws InterruptedException , ExecutionException {
203
211
try {
204
212
return get (requestTimeoutInMs , TimeUnit .MILLISECONDS );
205
213
} catch (TimeoutException e ) {
206
- cancelReaper ();
214
+ cancelTimeouts ();
207
215
throw new ExecutionException (e );
208
216
}
209
217
}
210
218
211
- public void cancelReaper () {
212
- if (reaperFuture != null ) {
213
- reaperFuture .cancel (false );
219
+ public void cancelTimeouts () {
220
+ if (timeoutsHolder != null ) {
221
+ timeoutsHolder .cancel ();
222
+ timeoutsHolder = null ;
214
223
}
215
224
}
216
225
@@ -242,7 +251,7 @@ public V get(long l, TimeUnit tu) throws InterruptedException, TimeoutException,
242
251
}
243
252
throw new ExecutionException (te );
244
253
} finally {
245
- cancelReaper ();
254
+ cancelTimeouts ();
246
255
}
247
256
}
248
257
}
@@ -278,7 +287,7 @@ private V getContent() throws ExecutionException {
278
287
}
279
288
throw new RuntimeException (ex );
280
289
} finally {
281
- cancelReaper ();
290
+ cancelTimeouts ();
282
291
}
283
292
}
284
293
}
@@ -290,7 +299,7 @@ private V getContent() throws ExecutionException {
290
299
public final void done () {
291
300
292
301
try {
293
- cancelReaper ();
302
+ cancelTimeouts ();
294
303
295
304
if (exEx .get () != null ) {
296
305
return ;
@@ -311,7 +320,7 @@ public final void done() {
311
320
}
312
321
313
322
public final void abort (final Throwable t ) {
314
- cancelReaper ();
323
+ cancelTimeouts ();
315
324
316
325
if (isDone .get () || isCancelled .get ())
317
326
return ;
@@ -366,9 +375,8 @@ public int incrementAndGetCurrentRedirectCount() {
366
375
return redirectCount .incrementAndGet ();
367
376
}
368
377
369
- public void setReaperFuture (FutureReaper reaperFuture ) {
370
- cancelReaper ();
371
- this .reaperFuture = reaperFuture ;
378
+ public void setTimeoutsHolder (TimeoutsHolder timeoutsHolder ) {
379
+ this .timeoutsHolder = timeoutsHolder ;
372
380
}
373
381
374
382
public boolean isInAuth () {
@@ -411,6 +419,10 @@ public void setStreamWasAlreadyConsumed(boolean streamWasAlreadyConsumed) {
411
419
public void touch () {
412
420
touch .set (millisTime ());
413
421
}
422
+
423
+ public long getLastTouch () {
424
+ return touch .get ();
425
+ }
414
426
415
427
@ Override
416
428
public boolean getAndSetWriteHeaders (boolean writeHeaders ) {
@@ -499,7 +511,7 @@ public String toString() {
499
511
",\n \t httpHeaders=" + httpHeaders + //
500
512
",\n \t exEx=" + exEx + //
501
513
",\n \t redirectCount=" + redirectCount + //
502
- ",\n \t reaperFuture =" + reaperFuture + //
514
+ ",\n \t imeoutsHolder =" + timeoutsHolder + //
503
515
",\n \t inAuth=" + inAuth + //
504
516
",\n \t statusReceived=" + statusReceived + //
505
517
",\n \t touch=" + touch + //
0 commit comments