Increase retry timeout to 1 minute.

Also, reduce the retry timeout in unit tests -- this speeds them up by
~80 seconds. Some cleanup of the libcurl perform timeout.

BUG=5576
TEST=unit tests, gmerged on device, ran update, looked at logs

Change-Id: Ifd554913cf437c43c481950897e7a5067fadee01

Review URL: http://codereview.chromium.org/3187005
diff --git a/http_fetcher_unittest.cc b/http_fetcher_unittest.cc
index 2b2e2cb..17e359e 100644
--- a/http_fetcher_unittest.cc
+++ b/http_fetcher_unittest.cc
@@ -122,7 +122,9 @@
  public:
   HttpFetcher* NewLargeFetcher() {
     LibcurlHttpFetcher *ret = new LibcurlHttpFetcher;
-    ret->set_idle_ms(1000);  // speeds up test execution
+    // Speed up test execution.
+    ret->set_idle_seconds(1);
+    ret->set_retry_seconds(1);
     return ret;
   }
   HttpFetcher* NewSmallFetcher() {
diff --git a/libcurl_http_fetcher.cc b/libcurl_http_fetcher.cc
index 0707d99..f636764 100644
--- a/libcurl_http_fetcher.cc
+++ b/libcurl_http_fetcher.cc
@@ -112,7 +112,7 @@
         if (delegate_)
           delegate_->TransferComplete(this, false);  // success
       } else {
-        g_timeout_add_seconds(5,
+        g_timeout_add_seconds(retry_seconds_,
                               &LibcurlHttpFetcher::StaticRetryTimeoutCallback,
                               this);
       }
@@ -216,29 +216,12 @@
     }
   }
 
-  // Wet up a timeout callback for libcurl
-  long ms = 0;
-  CHECK_EQ(curl_multi_timeout(curl_multi_handle_, &ms), CURLM_OK);
-  if (ms < 0) {
-    // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html:
-    //     if libcurl returns a -1 timeout here, it just means that libcurl
-    //     currently has no stored timeout value. You must not wait too long
-    //     (more than a few seconds perhaps) before you call
-    //     curl_multi_perform() again.
-    ms = idle_ms_;
-  }
+  // Set up a timeout callback for libcurl.
   if (!timeout_source_) {
-    LOG(INFO) << "setting up timeout source:" << ms;
-    timeout_source_ = g_timeout_source_new_seconds(1);
-    CHECK(timeout_source_);
-    g_source_set_callback(timeout_source_, StaticTimeoutCallback, this,
-                          NULL);
+    LOG(INFO) << "Setting up timeout source: " << idle_seconds_ << " seconds.";
+    timeout_source_ = g_timeout_source_new_seconds(idle_seconds_);
+    g_source_set_callback(timeout_source_, StaticTimeoutCallback, this, NULL);
     g_source_attach(timeout_source_, NULL);
-    static int counter = 0;
-    counter++;
-    if (counter % 50 == 0) {
-      LOG(INFO) << "counter = " << counter;
-    }
   }
 }
 
diff --git a/libcurl_http_fetcher.h b/libcurl_http_fetcher.h
index 66c1117..a4ed52f 100644
--- a/libcurl_http_fetcher.h
+++ b/libcurl_http_fetcher.h
@@ -21,9 +21,13 @@
 class LibcurlHttpFetcher : public HttpFetcher {
  public:
   LibcurlHttpFetcher()
-      : curl_multi_handle_(NULL), curl_handle_(NULL),
-        timeout_source_(NULL), transfer_in_progress_(false),
-        retry_count_(0), idle_ms_(1000) {}
+      : curl_multi_handle_(NULL),
+        curl_handle_(NULL),
+        timeout_source_(NULL),
+        transfer_in_progress_(false),
+        retry_count_(0),
+        retry_seconds_(60),
+        idle_seconds_(1) {}
 
   // Cleans up all internal state. Does not notify delegate
   ~LibcurlHttpFetcher();
@@ -50,9 +54,11 @@
   //     currently has no stored timeout value. You must not wait too long
   //     (more than a few seconds perhaps) before you call
   //     curl_multi_perform() again.
-  void set_idle_ms(long ms) {
-    idle_ms_ = ms;
-  }
+  void set_idle_seconds(int seconds) { idle_seconds_ = seconds; }
+
+  // Sets the retry timeout. Useful for testing.
+  void set_retry_seconds(int seconds) { retry_seconds_ = seconds; }
+
  private:
   // Resumes a transfer where it left off. This will use the
   // HTTP Range: header to make a new connection from where the last
@@ -73,7 +79,7 @@
   static gboolean StaticTimeoutCallback(gpointer data) {
     return reinterpret_cast<LibcurlHttpFetcher*>(data)->TimeoutCallback();
   }
-  
+
   gboolean RetryTimeoutCallback();
   static gboolean StaticRetryTimeoutCallback(void* arg) {
     return static_cast<LibcurlHttpFetcher*>(arg)->RetryTimeoutCallback();
@@ -128,11 +134,16 @@
   // If we resumed an earlier transfer, data offset that we used for the
   // new connection.  0 otherwise.
   off_t resume_offset_;
-  
+
   // Number of resumes performed.
   int retry_count_;
 
-  long idle_ms_;
+  // Seconds to wait before retrying a resume.
+  int retry_seconds_;
+
+  // Seconds to wait before asking libcurl to "perform".
+  int idle_seconds_;
+
   DISALLOW_COPY_AND_ASSIGN(LibcurlHttpFetcher);
 };