Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRetryBiPredicate.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.BiPredicate;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter;
24
25 public final class FlowableRetryBiPredicate<T> extends AbstractFlowableWithUpstream<T, T> {
26 final BiPredicate<? super Integer, ? super Throwable> predicate;
27 public FlowableRetryBiPredicate(
28 Flowable<T> source,
29 BiPredicate<? super Integer, ? super Throwable> predicate) {
30 super(source);
31 this.predicate = predicate;
32 }
33
34 @Override
35 public void subscribeActual(Subscriber<? super T> s) {
36 SubscriptionArbiter sa = new SubscriptionArbiter(false);
37 s.onSubscribe(sa);
38
39 RetryBiSubscriber<T> rs = new RetryBiSubscriber<>(s, predicate, sa, source);
40 rs.subscribeNext();
41 }
42
43 static final class RetryBiSubscriber<T> extends AtomicInteger implements FlowableSubscriber<T> {
44
45 private static final long serialVersionUID = -7098360935104053232L;
46
47 final Subscriber<? super T> downstream;
48 final SubscriptionArbiter sa;
49 final Publisher<? extends T> source;
50 final BiPredicate<? super Integer, ? super Throwable> predicate;
51 int retries;
52
53 long produced;
54
55 RetryBiSubscriber(Subscriber<? super T> actual,
56 BiPredicate<? super Integer, ? super Throwable> predicate, SubscriptionArbiter sa, Publisher<? extends T> source) {
57 this.downstream = actual;
58 this.sa = sa;
59 this.source = source;
60 this.predicate = predicate;
61 }
62
63 @Override
64 public void onSubscribe(Subscription s) {
65 sa.setSubscription(s);
66 }
67
68 @Override
69 public void onNext(T t) {
70 produced++;
71 downstream.onNext(t);
72 }
73
74 @Override
75 public void onError(Throwable t) {
76 boolean b;
77 try {
78 b = predicate.test(++retries, t);
79 } catch (Throwable e) {
80 Exceptions.throwIfFatal(e);
81 downstream.onError(new CompositeException(t, e));
82 return;
83 }
84 if (!b) {
85 downstream.onError(t);
86 return;
87 }
88 subscribeNext();
89 }
90
91 @Override
92 public void onComplete() {
93 downstream.onComplete();
94 }
95
96 /**
97 * Subscribes to the source again via trampolining.
98 */
99 void subscribeNext() {
100 if (getAndIncrement() == 0) {
101 int missed = 1;
102 for (;;) {
103 if (sa.isCancelled()) {
104 return;
105 }
106
107 long p = produced;
108 if (p != 0L) {
109 produced = 0L;
110 sa.produced(p);
111 }
112
113 source.subscribe(this);
114
115 missed = addAndGet(-missed);
116 if (missed == 0) {
117 break;
118 }
119 }
120 }
121 }
122 }
123 }