Bug 760614 - Retry on connection failures, to address transient network errors. r=nalexander
authorChenxia Liu <liuche@mozilla.com>
Mon, 04 Jun 2012 18:07:31 -0700
changeset 96157 f0f924ac58d2ed447b2a648fbea9aa85a7482494
parent 96156 44120738b32ccec647b4d22cfb7c4f52eef67cea
child 96158 03a85aeeed1493fc7f327cf285562aaef39501ab
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersnalexander
bugs760614
milestone15.0a1
Bug 760614 - Retry on connection failures, to address transient network errors. r=nalexander
mobile/android/base/sync/net/BaseResource.java
--- a/mobile/android/base/sync/net/BaseResource.java
+++ b/mobile/android/base/sync/net/BaseResource.java
@@ -56,16 +56,18 @@ import ch.boye.httpclientandroidlib.util
  * Exposes simple get/post/put/delete methods.
  */
 public class BaseResource implements Resource {
   private static final String ANDROID_LOOPBACK_IP = "10.0.2.2";
 
   private static final int MAX_TOTAL_CONNECTIONS     = 20;
   private static final int MAX_CONNECTIONS_PER_ROUTE = 10;
 
+  private boolean retryOnFailedRequest = true;
+
   public static boolean rewriteLocalhost = true;
 
   private static final String LOG_TAG = "BaseResource";
 
   protected URI uri;
   protected BasicHttpContext context;
   protected DefaultHttpClient client;
   public    ResourceDelegate delegate;
@@ -244,24 +246,40 @@ public class BaseResource implements Res
       HttpResponseObserver observer = getHttpResponseObserver();
       if (observer != null) {
         observer.observeHttpResponse(response);
       }
       delegate.handleHttpResponse(response);
     } catch (ClientProtocolException e) {
       delegate.handleHttpProtocolException(e);
     } catch (IOException e) {
-      delegate.handleHttpIOException(e);
+      Logger.debug(LOG_TAG, "I/O exception returned from execute.");
+      if (!retryOnFailedRequest) {
+        delegate.handleHttpIOException(e);
+      } else {
+        retryRequest();
+      }
     } catch (Exception e) {
       // Bug 740731: Don't let an exception fall through. Wrapping isn't
       // optimal, but often the exception is treated as an Exception anyway.
-      delegate.handleHttpIOException(new IOException(e));
+      if (!retryOnFailedRequest) {
+        delegate.handleHttpIOException(new IOException(e));
+      } else {
+        retryRequest();
+      }
     }
   }
 
+  private void retryRequest() {
+    // Only retry once.
+    retryOnFailedRequest = false;
+    Logger.debug(LOG_TAG, "Retrying request...");
+    this.execute();
+  }
+
   private void go(HttpRequestBase request) {
    if (delegate == null) {
       throw new IllegalArgumentException("No delegate provided.");
     }
     this.request = request;
     try {
       this.prepareClient();
     } catch (KeyManagementException e) {