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