Skip to content

Instantly share code, notes, and snippets.

@sddamico
Last active April 29, 2018 04:26
Show Gist options
  • Save sddamico/c45d7cdabc41e663bea1 to your computer and use it in GitHub Desktop.
Save sddamico/c45d7cdabc41e663bea1 to your computer and use it in GitHub Desktop.

Revisions

  1. sddamico revised this gist Dec 28, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    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.
  2. sddamico revised this gist Jul 7, 2015. 1 changed file with 5 additions and 8 deletions.
    13 changes: 5 additions & 8 deletions RxponetialBackoff
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,21 @@

    /**
    * @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) {
    final long time = TimeUnit.MILLISECONDS.convert(interval, units);

    return new Observable.Transformer<T, T>() {
    @Override
    public Observable<T> call(final Observable<T> observable) {
    return observable.retryWhen(
    retryFunc(time, retryAttempts),
    retryFunc(interval, units, retryAttempts),
    Schedulers.immediate()
    );
    }
    };
    }

    private static Func1<? super Observable<? extends Throwable>, ? extends Observable<?>> retryFunc(final long ms, final int attempts) {
    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) {
    @@ -36,14 +33,14 @@
    .flatMap(new Func1<Integer, Observable<Long>>() {
    @Override
    public Observable<Long> call(final Integer integer) {
    long newInterval = ms * ((long) integer * (long) 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, TimeUnit.MILLISECONDS, Schedulers.immediate());
    return Observable.timer(newInterval, units, Schedulers.immediate());
    }
    });
    }
    };
    }
    }
  3. sddamico revised this gist Jul 7, 2015. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions RxponetialBackoff
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,17 @@

    /**
    * @param intervalMs The base interval to start backing off from. The function is: attemptNum^2 * intervalMs
    * @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 intervalMs, final int retryAttempts) {
    public static <T> Observable.Transformer<T, T> backoff(final long interval, final TimeUnit units, final int retryAttempts) {
    final long time = TimeUnit.MILLISECONDS.convert(interval, units);

    return new Observable.Transformer<T, T>() {
    @Override
    public Observable<T> call(final Observable<T> observable) {
    return observable.retryWhen(
    retryFunc(intervalMs, retryAttempts),
    retryFunc(time, retryAttempts),
    Schedulers.immediate()
    );
    }
    @@ -43,4 +46,4 @@
    });
    }
    };
    }
    }
  4. sddamico created this gist Jul 2, 2015.
    46 changes: 46 additions & 0 deletions RxponetialBackoff
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    /**
    * @param intervalMs The base interval to start backing off from. The function is: attemptNum^2 * intervalMs
    * @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 intervalMs, final int retryAttempts) {

    return new Observable.Transformer<T, T>() {
    @Override
    public Observable<T> call(final Observable<T> observable) {
    return observable.retryWhen(
    retryFunc(intervalMs, retryAttempts),
    Schedulers.immediate()
    );
    }
    };
    }

    private static Func1<? super Observable<? extends Throwable>, ? extends Observable<?>> retryFunc(final long ms, 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 = ms * ((long) integer * (long) integer);
    if (newInterval < 0) {
    newInterval = Long.MAX_VALUE;
    }
    // use Schedulers#immediate() to keep on same thread
    return Observable.timer(newInterval, TimeUnit.MILLISECONDS, Schedulers.immediate());
    }
    });
    }
    };
    }