Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/AbstractBackpressureThrottlingSubscriber.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 io.reactivex.rxjava3.core.FlowableSubscriber;
17 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
18 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
19 import org.reactivestreams.Subscriber;
20 import org.reactivestreams.Subscription;
21
22 import java.util.concurrent.atomic.AtomicInteger;
23 import java.util.concurrent.atomic.AtomicLong;
24 import java.util.concurrent.atomic.AtomicReference;
25
26 /**
27 * Abstract base class for operators that throttle excessive updates from upstream in case if
28 * downstream {@link Subscriber} is not ready to receive updates.
29 *
30 * @param <T> the upstream value type
31 * @param <R> the downstream value type
32 */
33 abstract class AbstractBackpressureThrottlingSubscriber<T, R> extends AtomicInteger implements FlowableSubscriber<T>, Subscription {
34
35 private static final long serialVersionUID = -5050301752721603566L;
36
37 final Subscriber<? super R> downstream;
38
39 Subscription upstream;
40
41 volatile boolean done;
42 Throwable error;
43
44 volatile boolean cancelled;
45
46 final AtomicLong requested = new AtomicLong();
47
48 final AtomicReference<R> current = new AtomicReference<>();
49
50 AbstractBackpressureThrottlingSubscriber(Subscriber<? super R> downstream) {
51 this.downstream = downstream;
52 }
53
54 @Override
55 public void onSubscribe(Subscription s) {
56 if (SubscriptionHelper.validate(this.upstream, s)) {
57 this.upstream = s;
58 downstream.onSubscribe(this);
59 s.request(Long.MAX_VALUE);
60 }
61 }
62
63 @Override
64 public abstract void onNext(T t);
65
66 @Override
67 public void onError(Throwable t) {
68 error = t;
69 done = true;
70 drain();
71 }
72
73 @Override
74 public void onComplete() {
75 done = true;
76 drain();
77 }
78
79 @Override
80 public void request(long n) {
81 if (SubscriptionHelper.validate(n)) {
82 BackpressureHelper.add(requested, n);
83 drain();
84 }
85 }
86
87 @Override
88 public void cancel() {
89 if (!cancelled) {
90 cancelled = true;
91 upstream.cancel();
92
93 if (getAndIncrement() == 0) {
94 current.lazySet(null);
95 }
96 }
97 }
98
99 void drain() {
100 if (getAndIncrement() != 0) {
101 return;
102 }
103 final Subscriber<? super R> a = downstream;
104 int missed = 1;
105 final AtomicLong r = requested;
106 final AtomicReference<R> q = current;
107
108 for (;;) {
109 long e = 0L;
110
111 while (e != r.get()) {
112 boolean d = done;
113 R v = q.getAndSet(null);
114 boolean empty = v == null;
115
116 if (checkTerminated(d, empty, a, q)) {
117 return;
118 }
119
120 if (empty) {
121 break;
122 }
123
124 a.onNext(v);
125
126 e++;
127 }
128
129 if (e == r.get() && checkTerminated(done, q.get() == null, a, q)) {
130 return;
131 }
132
133 if (e != 0L) {
134 BackpressureHelper.produced(r, e);
135 }
136
137 missed = addAndGet(-missed);
138 if (missed == 0) {
139 break;
140 }
141 }
142 }
143
144 boolean checkTerminated(boolean d, boolean empty, Subscriber<?> a, AtomicReference<R> q) {
145 if (cancelled) {
146 q.lazySet(null);
147 return true;
148 }
149
150 if (d) {
151 Throwable e = error;
152 if (e != null) {
153 q.lazySet(null);
154 a.onError(e);
155 return true;
156 } else
157 if (empty) {
158 a.onComplete();
159 return true;
160 }
161 }
162
163 return false;
164 }
165 }