Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRetryPredicate.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.*;
22 import io.reactivex.rxjava3.functions.Predicate;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter;
24
25 public final class FlowableRetryPredicate<T> extends AbstractFlowableWithUpstream<T, T> {
26 final Predicate<? super Throwable> predicate;
27 final long count;
28 public FlowableRetryPredicate(Flowable<T> source,
29 long count,
30 Predicate<? super Throwable> predicate) {
31 super(source);
32 this.predicate = predicate;
33 this.count = count;
34 }
35
36 @Override
37 public void subscribeActual(Subscriber<? super T> s) {
38 SubscriptionArbiter sa = new SubscriptionArbiter(false);
39 s.onSubscribe(sa);
40
41 RetrySubscriber<T> rs = new RetrySubscriber<>(s, count, predicate, sa, source);
42 rs.subscribeNext();
43 }
44
45 static final class RetrySubscriber<T> extends AtomicInteger implements FlowableSubscriber<T> {
46
47 private static final long serialVersionUID = -7098360935104053232L;
48
49 final Subscriber<? super T> downstream;
50 final SubscriptionArbiter sa;
51 final Publisher<? extends T> source;
52 final Predicate<? super Throwable> predicate;
53 long remaining;
54
55 long produced;
56
57 RetrySubscriber(Subscriber<? super T> actual, long count,
58 Predicate<? super Throwable> predicate, SubscriptionArbiter sa, Publisher<? extends T> source) {
59 this.downstream = actual;
60 this.sa = sa;
61 this.source = source;
62 this.predicate = predicate;
63 this.remaining = count;
64 }
65
66 @Override
67 public void onSubscribe(Subscription s) {
68 sa.setSubscription(s);
69 }
70
71 @Override
72 public void onNext(T t) {
73 produced++;
74 downstream.onNext(t);
75 }
76
77 @Override
78 public void onError(Throwable t) {
79 long r = remaining;
80 if (r != Long.MAX_VALUE) {
81 remaining = r - 1;
82 }
83 if (r == 0) {
84 downstream.onError(t);
85 } else {
86 boolean b;
87 try {
88 b = predicate.test(t);
89 } catch (Throwable e) {
90 Exceptions.throwIfFatal(e);
91 downstream.onError(new CompositeException(t, e));
92 return;
93 }
94 if (!b) {
95 downstream.onError(t);
96 return;
97 }
98 subscribeNext();
99 }
100 }
101
102 @Override
103 public void onComplete() {
104 downstream.onComplete();
105 }
106
107 /**
108 * Subscribes to the source again via trampolining.
109 */
110 void subscribeNext() {
111 if (getAndIncrement() == 0) {
112 int missed = 1;
113 for (;;) {
114 if (sa.isCancelled()) {
115 return;
116 }
117
118 long p = produced;
119 if (p != 0L) {
120 produced = 0L;
121 sa.produced(p);
122 }
123
124 source.subscribe(this);
125
126 missed = addAndGet(-missed);
127 if (missed == 0) {
128 break;
129 }
130 }
131 }
132 }
133 }
134 }