|
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 | | -} |
| 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.Func2; |
| 24 | +import rx.schedulers.Schedulers; |
| 25 | +import rx.subscriptions.SerialSubscription; |
| 26 | + |
| 27 | +public final class OperatorRetryWithPredicate<T> implements Observable.Operator<T, Observable<T>> { |
| 28 | + final Func2<Integer, Throwable, Boolean> predicate; |
| 29 | + public OperatorRetryWithPredicate(Func2<Integer, Throwable, Boolean> predicate) { |
| 30 | + this.predicate = predicate; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Subscriber<? super Observable<T>> call(final Subscriber<? super T> child) { |
| 35 | + final Scheduler.Worker inner = Schedulers.trampoline().createWorker(); |
| 36 | + child.add(inner); |
| 37 | + |
| 38 | + final SerialSubscription serialSubscription = new SerialSubscription(); |
| 39 | + // add serialSubscription so it gets unsubscribed if child is unsubscribed |
| 40 | + child.add(serialSubscription); |
| 41 | + |
| 42 | + return new SourceSubscriber<T>(child, predicate, inner, serialSubscription); |
| 43 | + } |
| 44 | + |
| 45 | + static final class SourceSubscriber<T> extends Subscriber<Observable<T>> { |
| 46 | + final Subscriber<? super T> child; |
| 47 | + final Func2<Integer, Throwable, Boolean> predicate; |
| 48 | + final Scheduler.Worker inner; |
| 49 | + final SerialSubscription serialSubscription; |
| 50 | + |
| 51 | + volatile int attempts; |
| 52 | + @SuppressWarnings("rawtypes") |
| 53 | + static final AtomicIntegerFieldUpdater<SourceSubscriber> ATTEMPTS_UPDATER |
| 54 | + = AtomicIntegerFieldUpdater.newUpdater(SourceSubscriber.class, "attempts"); |
| 55 | + |
| 56 | + public SourceSubscriber(Subscriber<? super T> child, final Func2<Integer, Throwable, Boolean> predicate, Scheduler.Worker inner, |
| 57 | + SerialSubscription serialSubscription) { |
| 58 | + this.child = child; |
| 59 | + this.predicate = predicate; |
| 60 | + this.inner = inner; |
| 61 | + this.serialSubscription = serialSubscription; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onCompleted() { |
| 67 | + // ignore as we expect a single nested Observable<T> |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onError(Throwable e) { |
| 72 | + child.onError(e); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onNext(final Observable<T> o) { |
| 77 | + inner.schedule(new Action0() { |
| 78 | + |
| 79 | + @Override |
| 80 | + public void call() { |
| 81 | + final Action0 _self = this; |
| 82 | + ATTEMPTS_UPDATER.incrementAndGet(SourceSubscriber.this); |
| 83 | + |
| 84 | + // new subscription each time so if it unsubscribes itself it does not prevent retries |
| 85 | + // by unsubscribing the child subscription |
| 86 | + Subscriber<T> subscriber = new Subscriber<T>() { |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onCompleted() { |
| 90 | + child.onCompleted(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onError(Throwable e) { |
| 95 | + if (predicate.call(attempts, e) && !inner.isUnsubscribed()) { |
| 96 | + // retry again |
| 97 | + inner.schedule(_self); |
| 98 | + } else { |
| 99 | + // give up and pass the failure |
| 100 | + child.onError(e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onNext(T v) { |
| 106 | + child.onNext(v); |
| 107 | + } |
| 108 | + |
| 109 | + }; |
| 110 | + // register this Subscription (and unsubscribe previous if exists) |
| 111 | + serialSubscription.set(subscriber); |
| 112 | + o.unsafeSubscribe(subscriber); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments