-
-
Save ZacSweers/7902e3a0286774630f4f to your computer and use it in GitHub Desktop.
Exponential Backoff using Rx.retryWhen() I DON'T KNOW WHY THIS HAS HIGH SEO I JUST FORKED IT TO SAVE A COPY
This file contains 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 characters
// retries up to 3 times while exponentially backing off with each retry | |
.retryWhen(errors -> | |
errors | |
.zipWith( | |
Observable.range(1, MAX_RETRIES), (n, i) -> i | |
) | |
.flatMap( | |
retryCount -> Observable.timer((long) Math.pow(4, retryCount), TimeUnit.SECONDS) | |
) | |
) |
@trajakovic you can try this
// retries up to 3 times while exponentially backing off with each retry
.retryWhen(errors ->
errors
.scan((errCount, err) -> {
if (errCount > MAX_RETRIES)
throw err;
return errCount + 1;
}, 0)
.flatMap(
retryCount -> Observable.timer((long) Math.pow(4, retryCount), TimeUnit.SECONDS)
)
)
You guys should try something like this, the example retry 3 times(With a delay of one second) after this return the error:
static final int retrycount=4;
///...
.retryWhen(errors->errors
.zipWith(Observable.range(1,retrycount),(err,attempt)->
attempt<retrycount?
Observable.timer(1,TimeUnit.SECONDS):
Observable.error(err))
.flatMap(x->x)
)
//Tested and running at 100%
@FAMM2017, thank you. And I also found the same answer here: https://stackoverflow.com/questions/37860856/catch-error-if-retrywhens-retries-runs-out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@trajakovic that is correct, you just need to use doOnError because the error is handled silently because of retry.
Something like this: doOnError(throwable ->Timber.e(throwable, "Blablabla Error has been occurred"))
For me it is more interesting, that the timer is not happening anymore, but the statement in retryWhen gets called obviously.