< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java

Print this page

        

@@ -25,11 +25,11 @@
 
 package jdk.internal.net.http;
 
 import java.io.IOException;
 import java.net.ConnectException;
-import java.time.Duration;
+import java.net.http.HttpConnectTimeoutException;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.security.AccessControlContext;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;

@@ -86,11 +86,11 @@
     static final int max_attempts = Utils.getIntegerNetProperty(
             "jdk.httpclient.redirects.retrylimit", DEFAULT_MAX_ATTEMPTS
     );
 
     private final LinkedList<HeaderFilter> filters;
-    TimedEvent timedEvent;
+    ResponseTimerEvent responseTimerEvent;
     volatile boolean cancelled;
     final PushGroup<T> pushGroup;
 
     /**
      * Filter fields. These are attached as required by filters

@@ -132,11 +132,11 @@
         }
 
         this.exchange = new Exchange<>(request, this);
     }
 
-    private synchronized Exchange<T> getExchange() {
+    synchronized Exchange<T> getExchange() {
         return exchange;
     }
 
     HttpClientImpl client() {
         return client;

@@ -155,12 +155,12 @@
         }
         this.exchange = exchange;
     }
 
     private void cancelTimer() {
-        if (timedEvent != null) {
-            client.cancelTimer(timedEvent);
+        if (responseTimerEvent != null) {
+            client.cancelTimer(responseTimerEvent);
         }
     }
 
     private void requestFilters(HttpRequestImpl r) throws IOException {
         Log.logTrace("Applying request filters");

@@ -218,12 +218,12 @@
         CompletableFuture<Response> cf;
         if (attempts.incrementAndGet() > max_attempts) {
             cf = failedFuture(new IOException("Too many retries", retryCause));
         } else {
             if (currentreq.timeout().isPresent()) {
-                timedEvent = new TimedEvent(currentreq.timeout().get());
-                client.registerTimer(timedEvent);
+                responseTimerEvent = ResponseTimerEvent.of(this);
+                client.registerTimer(responseTimerEvent);
             }
             try {
                 // 1. apply request filters
                 // if currentreq == previousreq the filters have already
                 // been applied once. Applying them a second time might

@@ -342,11 +342,13 @@
             if (t.getCause() != null) {
                 t = t.getCause();
             }
         }
         if (cancelled && t instanceof IOException) {
-            t = new HttpTimeoutException("request timed out");
+            if (!(t instanceof HttpTimeoutException)) {
+                t = toTimeoutException((IOException)t);
+            }
         } else if (retryOnFailure(t)) {
             Throwable cause = retryCause(t);
 
             if (!(t instanceof ConnectException)) {
                 if (!canRetryRequest(currentreq)) {

@@ -376,19 +378,26 @@
             }
         }
         return failedFuture(t);
     }
 
-    class TimedEvent extends TimeoutEvent {
-        TimedEvent(Duration duration) {
-            super(duration);
+    private HttpTimeoutException toTimeoutException(IOException ioe) {
+        HttpTimeoutException t = null;
+
+        // more specific, "request timed out", when connected
+        Exchange<?> exchange = getExchange();
+        if (exchange != null) {
+            ExchangeImpl<?> exchangeImpl = exchange.exchImpl;
+            if (exchangeImpl != null) {
+                if (exchangeImpl.connection().connected()) {
+                    t = new HttpTimeoutException("request timed out");
+                    t.initCause(ioe);  // TODO: remove this if all testing goes well
+                }
         }
-        @Override
-        public void handle() {
-            if (debug.on()) {
-                debug.log("Cancelling MultiExchange due to timeout for request %s",
-                        request);
             }
-            cancel(new HttpTimeoutException("request timed out"));
+        if (t == null) {
+            t = new HttpConnectTimeoutException("HTTP connect timed out");
+            t.initCause(new ConnectException("HTTP connect timed out"));
         }
+        return t;
     }
 }
< prev index next >