Higher-level backoff of sync service after API failures to prevent the service from holding eternal wakelocks when the API is unreachable.

This commit is contained in:
dosiecki 2015-08-31 16:59:47 -07:00
parent 6b91256566
commit f9471ac276
2 changed files with 22 additions and 0 deletions

View file

@ -138,6 +138,10 @@ public class NBSyncService extends Service {
BlurDatabaseHelper dbHelper;
private int lastStartIdCompleted = -1;
/** The time of the last hard API failure we encountered. Used to implement back-off so that the sync
service doesn't spin in the background chewing up battery when the API is unavailable. */
private static long lastAPIFailure = 0;
@Override
public void onCreate() {
super.onCreate();
@ -302,6 +306,7 @@ public class NBSyncService extends Service {
*/
private void syncActions() {
if (stopSync()) return;
if (backoffBackgroundCalls()) return;
Cursor c = null;
try {
@ -330,6 +335,7 @@ public class NBSyncService extends Service {
} else if (response.isProtocolError) {
// the network failed or we got a non-200, so be sure we retry
Log.i(this.getClass().getName(), "Holding reading action with server-side or network error.");
noteHardAPIFailure();
continue actionsloop;
} else if (response.isError()) {
Log.e(this.getClass().getName(), "Discarding reading action with user error.");
@ -379,6 +385,7 @@ public class NBSyncService extends Service {
}
if (stopSync()) return;
if (backoffBackgroundCalls()) return;
if (ActMode != ActivationMode.ALL) return;
FFSyncRunning = true;
@ -399,6 +406,7 @@ public class NBSyncService extends Service {
FeedFolderResponse feedResponse = apiManager.getFolderFeedMapping(true);
if (feedResponse == null) {
noteHardAPIFailure();
return;
}
@ -700,6 +708,17 @@ public class NBSyncService extends Service {
return stopSync(this);
}
private void noteHardAPIFailure() {
lastAPIFailure = System.currentTimeMillis();
}
private boolean backoffBackgroundCalls() {
if (NbActivity.getActiveActivityCount() > 0) return false;
if (System.currentTimeMillis() > (lastAPIFailure + AppConstants.API_BACKGROUND_BACKOFF_MILLIS)) return false;
Log.i(this.getClass().getName(), "abandoning background sync due to recent API failures.");
return true;
}
public void onTrimMemory (int level) {
if (level > ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
isMemoryLow = true;

View file

@ -42,6 +42,9 @@ public class AppConstants {
// the base amount for how long to sleep during exponential API failure backoff
public static final long API_BACKOFF_BASE_MILLIS = 750L;
// for how long to back off from background syncs after a hard API failure
public static final long API_BACKGROUND_BACKOFF_MILLIS = 5L * 60L * 1000L;
// when generating a request for multiple feeds, limit the total number requested to prevent
// unworkably long URLs
public static final int MAX_FEED_LIST_SIZE = 250;