4141import org .asynchttpclient .providers .netty .DiscardEvent ;
4242import org .asynchttpclient .providers .netty .channel .Channels ;
4343import org .asynchttpclient .providers .netty .request .NettyRequest ;
44+ import org .asynchttpclient .providers .netty .request .timeout .TimeoutsHolder ;
4445import org .slf4j .Logger ;
4546import org .slf4j .LoggerFactory ;
4647
@@ -59,7 +60,8 @@ public enum STATE {
5960 }
6061
6162private final int requestTimeoutInMs ;
62- private final AsyncHttpClientConfig config ;
63+ private volatile boolean requestTimeoutReached ;
64+ private volatile boolean idleConnectionTimeoutReached ;
6365private final long start =millisTime ();
6466private final ConnectionPoolKeyStrategy connectionPoolKeyStrategy ;
6567private final ProxyServer proxyServer ;
@@ -80,7 +82,7 @@ public enum STATE {
8082private final AtomicBoolean throwableCalled =new AtomicBoolean (false );
8183private final AtomicReference <V >content =new AtomicReference <V >();
8284private final AtomicReference <ExecutionException >exEx =new AtomicReference <ExecutionException >();
83- private volatile FutureReaper reaperFuture ;
85+ private volatile TimeoutsHolder timeoutsHolder ;
8486
8587// state mutated only inside the event loop
8688private Channel channel ;
@@ -111,7 +113,6 @@ public NettyResponseFuture(URI uri,//
111113this .request =request ;
112114this .nettyRequest =nettyRequest ;
113115this .uri =uri ;
114- this .config =config ;
115116this .connectionPoolKeyStrategy =connectionPoolKeyStrategy ;
116117this .proxyServer =proxyServer ;
117118
@@ -156,7 +157,7 @@ public void setAsyncHandler(AsyncHandler<V> asyncHandler) {
156157
157158@ Override
158159public boolean cancel (boolean force ) {
159- cancelReaper ();
160+ cancelTimeouts ();
160161
161162if (isCancelled .get ())
162163return false ;
@@ -186,31 +187,39 @@ public boolean cancel(boolean force) {
186187 * @return <code>true</code> if response has expired and should be terminated.
187188 */
188189public boolean hasExpired () {
189- long now =millisTime ();
190- return hasConnectionIdleTimedOut (now ) ||hasRequestTimedOut (now );
190+ return requestTimeoutReached ||idleConnectionTimeoutReached ;
191191 }
192192
193- public boolean hasConnectionIdleTimedOut ( long now ) {
194- return config . getIdleConnectionTimeoutInMs () != - 1 && ( now - touch . get ()) >= config . getIdleConnectionTimeoutInMs () ;
193+ public void setRequestTimeoutReached ( ) {
194+ this . requestTimeoutReached = true ;
195195 }
196196
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 ;
199207 }
200208
201209@ Override
202210public V get ()throws InterruptedException ,ExecutionException {
203211try {
204212return get (requestTimeoutInMs ,TimeUnit .MILLISECONDS );
205213 }catch (TimeoutException e ) {
206- cancelReaper ();
214+ cancelTimeouts ();
207215throw new ExecutionException (e );
208216 }
209217 }
210218
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 ;
214223 }
215224 }
216225
@@ -242,7 +251,7 @@ public V get(long l, TimeUnit tu) throws InterruptedException, TimeoutException,
242251 }
243252throw new ExecutionException (te );
244253 }finally {
245- cancelReaper ();
254+ cancelTimeouts ();
246255 }
247256 }
248257 }
@@ -278,7 +287,7 @@ private V getContent() throws ExecutionException {
278287 }
279288throw new RuntimeException (ex );
280289 }finally {
281- cancelReaper ();
290+ cancelTimeouts ();
282291 }
283292 }
284293 }
@@ -290,7 +299,7 @@ private V getContent() throws ExecutionException {
290299public final void done () {
291300
292301try {
293- cancelReaper ();
302+ cancelTimeouts ();
294303
295304if (exEx .get () !=null ) {
296305return ;
@@ -311,7 +320,7 @@ public final void done() {
311320 }
312321
313322public final void abort (final Throwable t ) {
314- cancelReaper ();
323+ cancelTimeouts ();
315324
316325if (isDone .get () ||isCancelled .get ())
317326return ;
@@ -366,9 +375,8 @@ public int incrementAndGetCurrentRedirectCount() {
366375return redirectCount .incrementAndGet ();
367376 }
368377
369- public void setReaperFuture (FutureReaper reaperFuture ) {
370- cancelReaper ();
371- this .reaperFuture =reaperFuture ;
378+ public void setTimeoutsHolder (TimeoutsHolder timeoutsHolder ) {
379+ this .timeoutsHolder =timeoutsHolder ;
372380 }
373381
374382public boolean isInAuth () {
@@ -411,6 +419,10 @@ public void setStreamWasAlreadyConsumed(boolean streamWasAlreadyConsumed) {
411419public void touch () {
412420touch .set (millisTime ());
413421 }
422+
423+ public long getLastTouch () {
424+ return touch .get ();
425+ }
414426
415427@ Override
416428public boolean getAndSetWriteHeaders (boolean writeHeaders ) {
@@ -499,7 +511,7 @@ public String toString() {
499511",\n \t httpHeaders=" +httpHeaders +//
500512",\n \t exEx=" +exEx +//
501513",\n \t redirectCount=" +redirectCount +//
502- ",\n \t reaperFuture =" +reaperFuture +//
514+ ",\n \t imeoutsHolder =" +timeoutsHolder +//
503515",\n \t inAuth=" +inAuth +//
504516",\n \t statusReceived=" +statusReceived +//
505517",\n \t touch=" +touch +//