Skip to content

Instantly share code, notes, and snippets.

@defHLT
Created June 9, 2015 20:57
Show Gist options
  • Select an option

  • Save defHLT/59143e2ef85306bddb5a to your computer and use it in GitHub Desktop.

Select an option

Save defHLT/59143e2ef85306bddb5a to your computer and use it in GitHub Desktop.
/**
* Created by ice on 5/3/15.
*/
public class RetryWithDelay implements Func1<Observable<? extends Throwable>, Observable<?>> {
private final int maxRetries;
private final int retryDelayMillis;
private int retryCount;
public RetryWithDelay(final int maxRetries, final int retryDelayMillis) {
this.maxRetries = maxRetries;
this.retryDelayMillis = retryDelayMillis;
this.retryCount = 0;
}
@Override
public Observable<?> call(final Observable<? extends Throwable> attempts) {
return attempts
.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(Throwable throwable) {
if (++retryCount < maxRetries) {
// When this Observable calls onNext, the original
// Observable will be retried (i.e. re-subscribed).
Timber.d("retrying");
return Observable.timer(retryDelayMillis,
TimeUnit.MILLISECONDS);
}
// Max retries hit. Just pass the error along.
return Observable.error(throwable);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment