|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package rx.operators; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
| 19 | +import rx.Observable; |
| 20 | +import rx.Scheduler; |
| 21 | +import rx.Subscriber; |
| 22 | +import rx.functions.Action0; |
| 23 | +import rx.functions.Func1; |
| 24 | +import rx.functions.Func2; |
| 25 | +import rx.schedulers.Schedulers; |
| 26 | +import rx.subscriptions.SerialSubscription; |
| 27 | + |
| 28 | +public final class OperatorRetryWithPredicate<T> implements Observable.Operator<T, Observable<T>> { |
| 29 | + final Func2<Integer, Throwable, Boolean> predicate; |
| 30 | + public OperatorRetryWithPredicate(Func1<Throwable, Boolean> predicate) { |
| 31 | + this.predicate = new IgnoreIndexPredicate(predicate); |
| 32 | + } |
| 33 | + public OperatorRetryWithPredicate(Func2<Integer, Throwable, Boolean> predicate) { |
| 34 | + this.predicate = predicate; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Subscriber<? super Observable<T>> call(final Subscriber<? super T> child) { |
| 39 | + final Scheduler.Worker inner = Schedulers.trampoline().createWorker(); |
| 40 | + child.add(inner); |
| 41 | + |
| 42 | + final SerialSubscription serialSubscription = new SerialSubscription(); |
| 43 | + // add serialSubscription so it gets unsubscribed if child is unsubscribed |
| 44 | + child.add(serialSubscription); |
| 45 | + |
| 46 | + return new SourceSubscriber<T>(child, predicate, inner, serialSubscription); |
| 47 | + } |
| 48 | + |
| 49 | + /** Ignore the index parameter and call a Func1 predicate with the throwable only. */ |
| 50 | + static final class IgnoreIndexPredicate implements Func2<Integer, Throwable, Boolean> { |
| 51 | + final Func1<Throwable, Boolean> predicate; |
| 52 | + |
| 53 | + public IgnoreIndexPredicate(Func1<Throwable, Boolean> predicate) { |
| 54 | + this.predicate = predicate; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Boolean call(Integer t1, Throwable t2) { |
| 59 | + return predicate.call(t2); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + static final class SourceSubscriber<T> extends Subscriber<Observable<T>> { |
| 65 | + final Subscriber<? super T> child; |
| 66 | + final Func2<Integer, Throwable, Boolean> predicate; |
| 67 | + final Scheduler.Worker inner; |
| 68 | + final SerialSubscription serialSubscription; |
| 69 | + |
| 70 | + volatile int attempts; |
| 71 | + @SuppressWarnings("rawtypes") |
| 72 | + static final AtomicIntegerFieldUpdater<SourceSubscriber> ATTEMPTS_UPDATER |
| 73 | + = AtomicIntegerFieldUpdater.newUpdater(SourceSubscriber.class, "attempts"); |
| 74 | + |
| 75 | + public SourceSubscriber(Subscriber<? super T> child, final Func2<Integer, Throwable, Boolean> predicate, Scheduler.Worker inner, |
| 76 | + SerialSubscription serialSubscription) { |
| 77 | + this.child = child; |
| 78 | + this.predicate = predicate; |
| 79 | + this.inner = inner; |
| 80 | + this.serialSubscription = serialSubscription; |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onCompleted() { |
| 86 | + // ignore as we expect a single nested Observable<T> |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onError(Throwable e) { |
| 91 | + child.onError(e); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onNext(final Observable<T> o) { |
| 96 | + inner.schedule(new Action0() { |
| 97 | + |
| 98 | + @Override |
| 99 | + public void call() { |
| 100 | + final Action0 _self = this; |
| 101 | + ATTEMPTS_UPDATER.incrementAndGet(SourceSubscriber.this); |
| 102 | + |
| 103 | + // new subscription each time so if it unsubscribes itself it does not prevent retries |
| 104 | + // by unsubscribing the child subscription |
| 105 | + Subscriber<T> subscriber = new Subscriber<T>() { |
| 106 | + |
| 107 | + @Override |
| 108 | + public void onCompleted() { |
| 109 | + child.onCompleted(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void onError(Throwable e) { |
| 114 | + if (predicate.call(attempts, e) && !inner.isUnsubscribed()) { |
| 115 | + // retry again |
| 116 | + inner.schedule(_self); |
| 117 | + } else { |
| 118 | + // give up and pass the failure |
| 119 | + child.onError(e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onNext(T v) { |
| 125 | + child.onNext(v); |
| 126 | + } |
| 127 | + |
| 128 | + }; |
| 129 | + // register this Subscription (and unsubscribe previous if exists) |
| 130 | + serialSubscription.set(subscriber); |
| 131 | + o.unsafeSubscribe(subscriber); |
| 132 | + } |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments