Skip to content

Instantly share code, notes, and snippets.

@sddamico sddamico/LICENSE
Last active Apr 29, 2018

Embed
What would you like to do?
Exponential Backoff Transformer
Copyright (c) 2016 Stephen D'Amico
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* @param interval The base interval to start backing off from. The function is: attemptNum^2 * intervalTime
* @param units The units for interval
* @param retryAttempts The max number of attempts to retry this task or -1 to try MAX_INT times,
*/
public static <T> Observable.Transformer<T, T> backoff(final long interval, final TimeUnit units, final int retryAttempts) {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(final Observable<T> observable) {
return observable.retryWhen(
retryFunc(interval, units, retryAttempts),
Schedulers.immediate()
);
}
};
}
private static Func1<? super Observable<? extends Throwable>, ? extends Observable<?>> retryFunc(final long interval, final TimeUnit units, final int attempts) {
return new Func1<Observable<? extends Throwable>, Observable<Long>>() {
@Override
public Observable<Long> call(Observable<? extends Throwable> observable) {
// zip our number of retries to the incoming errors so that we only produce retries
// when there's been an error
return observable.zipWith(
Observable.range(1, attempts > 0 ? attempts : Integer.MAX_VALUE),
new Func2<Throwable, Integer, Integer>() {
@Override
public Integer call(Throwable throwable, Integer attemptNumber) {
return attemptNumber;
}
})
// flatMap the int attempt number to a timer that will wait the specified delay
.flatMap(new Func1<Integer, Observable<Long>>() {
@Override
public Observable<Long> call(final Integer integer) {
long newInterval = interval * ((long) integer * (long) integer);
if (newInterval < 0) {
newInterval = Long.MAX_VALUE;
}
// use Schedulers#immediate() to keep on same thread
return Observable.timer(newInterval, units, Schedulers.immediate());
}
});
}
};
}
@plastiv

This comment has been minimized.

Copy link

commented Jul 3, 2015

I understand that overwriting default scheduler with immediate() would save switching between threads and keeping a main observable worker thread from releasing back to the pool. Is it a correct assumption?

@sddamico

This comment has been minimized.

Copy link
Owner Author

commented Jul 7, 2015

@plastiv sorry for missing this comment, but yes you're correct.

@sddamico

This comment has been minimized.

Copy link
Owner Author

commented Jul 7, 2015

OP updated with TimeUnit to be better aligned with mainline api's that work with time.

@DavidMihola

This comment has been minimized.

Copy link

commented Nov 13, 2015

Just to make sure I understanding/using this correctly: This does never actually forward the last error, right? That is, after the last retry I will get an onCompleted() in my Subscriber and not an onError?

@ltn614

This comment has been minimized.

Copy link

commented Nov 25, 2015

I think there is a problem in your code, since you use Schedulers.immediate() in the backoff, you shouldn't use
it again in retryFunc's Observable.timer, it will cause the other operation in retryFunc block, am I right?

@roman-yu

This comment has been minimized.

Copy link

commented Feb 20, 2017

@DavidMihola, I think you are right, I have encountered exactly the problem you mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.