Bug 729410 - Part 1: Implement basic automatic backoff unless a sync is forced. r=nalexander
authorRichard Newman <rnewman@mozilla.com>
Thu, 23 Feb 2012 08:14:05 -0800
changeset 87529 5e18c023268af4dc7fb1778f6951dacfeb714cf0
parent 87528 307abe0852487a89d1d1edc2f4c9496c3a5d87f0
child 87530 95675530bf288aea0b3e0dad160b01bb640754d7
push id6390
push userrnewman@mozilla.com
push dateThu, 23 Feb 2012 16:14:36 +0000
treeherdermozilla-inbound@0987e5b0d371 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs729410
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 729410 - Part 1: Implement basic automatic backoff unless a sync is forced. r=nalexander
mobile/android/base/sync/syncadapter/SyncAdapter.java
--- a/mobile/android/base/sync/syncadapter/SyncAdapter.java
+++ b/mobile/android/base/sync/syncadapter/SyncAdapter.java
@@ -76,16 +76,17 @@ import android.util.Log;
 public class SyncAdapter extends AbstractThreadedSyncAdapter implements GlobalSessionCallback {
   private static final String  LOG_TAG = "SyncAdapter";
 
   private static final String  PREFS_EARLIEST_NEXT_SYNC = "earliestnextsync";
   private static final String  PREFS_INVALIDATE_AUTH_TOKEN = "invalidateauthtoken";
 
   private static final int     SHARED_PREFERENCES_MODE = 0;
   private static final int     BACKOFF_PAD_SECONDS = 5;
+  private static final int     MINIMUM_SYNC_INTERVAL_MILLISECONDS = 5 * 60 * 1000;   // 5 minutes.
 
   private final AccountManager mAccountManager;
   private final Context        mContext;
 
   public SyncAdapter(Context context, boolean autoInitialize) {
     super(context, autoInitialize);
     mContext = context;
     Log.d(LOG_TAG, "AccountManager.get(" + mContext + ")");
@@ -217,22 +218,27 @@ public class SyncAdapter extends Abstrac
   @Override
   public void onPerformSync(final Account account,
                             final Bundle extras,
                             final String authority,
                             final ContentProviderClient provider,
                             final SyncResult syncResult) {
     Utils.reseedSharedRandom(); // Make sure we don't work with the same random seed for too long.
 
+    boolean force = (extras != null) && (extras.getBoolean("force", false));
     long delay = delayMilliseconds();
     if (delay > 0) {
-      Log.i(LOG_TAG, "Not syncing: must wait another " + delay + "ms.");
-      long remainingSeconds = delay / 1000;
-      syncResult.delayUntil = remainingSeconds + BACKOFF_PAD_SECONDS;
-      return;
+      if (force) {
+        Log.i(LOG_TAG, "Forced sync: overruling remaining backoff of " + delay + "ms.");
+      } else {
+        Log.i(LOG_TAG, "Not syncing: must wait another " + delay + "ms.");
+        long remainingSeconds = delay / 1000;
+        syncResult.delayUntil = remainingSeconds + BACKOFF_PAD_SECONDS;
+        return;
+      }
     }
 
     // TODO: don't clear the auth token unless we have a sync error.
     Log.i(LOG_TAG, "Got onPerformSync. Extras bundle is " + extras);
     Log.i(LOG_TAG, "Account name: " + account.name);
 
     // TODO: don't always invalidate; use getShouldInvalidateAuthToken.
     // However, this fixes Bug 716815, so it'll do for now.
@@ -295,16 +301,19 @@ public class SyncAdapter extends Abstrac
       // Perform the work in a new thread from within this synchronized block,
       // which allows us to be waiting on the monitor before the callback can
       // notify us in a failure case. Oh, concurrent programming.
       new Thread(fetchAuthToken).start();
 
       Log.i(LOG_TAG, "Waiting on sync monitor.");
       try {
         syncMonitor.wait();
+        long next = System.currentTimeMillis() + MINIMUM_SYNC_INTERVAL_MILLISECONDS;
+        Log.i(LOG_TAG, "Setting minimum next sync time to " + next);
+        extendEarliestNextSync(next);
       } catch (InterruptedException e) {
         Log.i(LOG_TAG, "Waiting on sync monitor interrupted.", e);
       }
     }
  }
 
 
   /**
@@ -333,17 +342,16 @@ public class SyncAdapter extends Abstrac
     Log.i(LOG_TAG, "Performing sync.");
     this.syncResult = syncResult;
     // TODO: default serverURL.
     GlobalSession globalSession = new GlobalSession(SyncConfiguration.DEFAULT_USER_API,
                                                     serverURL, username, password, prefsPath,
                                                     keyBundle, this, this.mContext, extras);
 
     globalSession.start();
-
   }
 
   private void notifyMonitor() {
     synchronized (syncMonitor) {
       Log.i(LOG_TAG, "Notifying sync monitor.");
       syncMonitor.notifyAll();
     }
   }