Created
July 19, 2016 12:53
-
-
Save milechainsaw/811c1b583706da60417ed10d35d2808f to your computer and use it in GitHub Desktop.
Revisions
-
milechainsaw created this gist
Jul 19, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ /** * Created by milechainsaw on 7/19/16. * * Exponential backoff callback for Retrofit2 * */ public abstract class BackoffCallback<T> implements Callback<T> { private static final int RETRY_COUNT = 3; /** * Base retry delay for exponential backoff, in Milliseconds */ private static final double RETRY_DELAY = 300; private int retryCount = 0; @Override public void onFailure(final Call<T> call, Throwable t) { retryCount++; if (retryCount <= RETRY_COUNT) { int expDelay = (int) (RETRY_DELAY * Math.pow(2, Math.max(0, retryCount - 1))); new Handler().postDelayed(new Runnable() { @Override public void run() { retry(call); } }, expDelay); } else { onFailedAfterRetry(t); } } private void retry(Call<T> call) { call.clone().enqueue(this); } public abstract void onFailedAfterRetry(Throwable t); }