Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTimeout.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.Objects;
17 import java.util.concurrent.TimeoutException;
18 import java.util.concurrent.atomic.*;
19
20 import org.reactivestreams.*;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.Disposable;
24 import io.reactivex.rxjava3.exceptions.Exceptions;
25 import io.reactivex.rxjava3.functions.Function;
26 import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
27 import io.reactivex.rxjava3.internal.operators.flowable.FlowableTimeoutTimed.TimeoutSupport;
28 import io.reactivex.rxjava3.internal.subscriptions.*;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30
31 public final class FlowableTimeout<T, U, V> extends AbstractFlowableWithUpstream<T, T> {
32 final Publisher<U> firstTimeoutIndicator;
33 final Function<? super T, ? extends Publisher<V>> itemTimeoutIndicator;
34 final Publisher<? extends T> other;
35
36 public FlowableTimeout(
37 Flowable<T> source,
38 Publisher<U> firstTimeoutIndicator,
39 Function<? super T, ? extends Publisher<V>> itemTimeoutIndicator,
40 Publisher<? extends T> other) {
41 super(source);
42 this.firstTimeoutIndicator = firstTimeoutIndicator;
43 this.itemTimeoutIndicator = itemTimeoutIndicator;
44 this.other = other;
45 }
46
47 @Override
48 protected void subscribeActual(Subscriber<? super T> s) {
49 if (other == null) {
50 TimeoutSubscriber<T> parent = new TimeoutSubscriber<>(s, itemTimeoutIndicator);
51 s.onSubscribe(parent);
52 parent.startFirstTimeout(firstTimeoutIndicator);
53 source.subscribe(parent);
54 } else {
55 TimeoutFallbackSubscriber<T> parent = new TimeoutFallbackSubscriber<>(s, itemTimeoutIndicator, other);
56 s.onSubscribe(parent);
57 parent.startFirstTimeout(firstTimeoutIndicator);
58 source.subscribe(parent);
59 }
60 }
61
62 interface TimeoutSelectorSupport extends TimeoutSupport {
63 void onTimeoutError(long idx, Throwable ex);
64 }
65
66 static final class TimeoutSubscriber<T> extends AtomicLong
67 implements FlowableSubscriber<T>, Subscription, TimeoutSelectorSupport {
68
69 private static final long serialVersionUID = 3764492702657003550L;
70
71 final Subscriber<? super T> downstream;
72
73 final Function<? super T, ? extends Publisher<?>> itemTimeoutIndicator;
74
75 final SequentialDisposable task;
76
77 final AtomicReference<Subscription> upstream;
78
79 final AtomicLong requested;
80
81 TimeoutSubscriber(Subscriber<? super T> actual, Function<? super T, ? extends Publisher<?>> itemTimeoutIndicator) {
82 this.downstream = actual;
83 this.itemTimeoutIndicator = itemTimeoutIndicator;
84 this.task = new SequentialDisposable();
85 this.upstream = new AtomicReference<>();
86 this.requested = new AtomicLong();
87 }
88
89 @Override
90 public void onSubscribe(Subscription s) {
91 SubscriptionHelper.deferredSetOnce(upstream, requested, s);
92 }
93
94 @Override
95 public void onNext(T t) {
96 long idx = get();
97 if (idx == Long.MAX_VALUE || !compareAndSet(idx, idx + 1)) {
98 return;
99 }
100
101 Disposable d = task.get();
102 if (d != null) {
103 d.dispose();
104 }
105
106 downstream.onNext(t);
107
108 Publisher<?> itemTimeoutPublisher;
109
110 try {
111 itemTimeoutPublisher = Objects.requireNonNull(
112 itemTimeoutIndicator.apply(t),
113 "The itemTimeoutIndicator returned a null Publisher.");
114 } catch (Throwable ex) {
115 Exceptions.throwIfFatal(ex);
116 upstream.get().cancel();
117 getAndSet(Long.MAX_VALUE);
118 downstream.onError(ex);
119 return;
120 }
121
122 TimeoutConsumer consumer = new TimeoutConsumer(idx + 1, this);
123 if (task.replace(consumer)) {
124 itemTimeoutPublisher.subscribe(consumer);
125 }
126 }
127
128 void startFirstTimeout(Publisher<?> firstTimeoutIndicator) {
129 if (firstTimeoutIndicator != null) {
130 TimeoutConsumer consumer = new TimeoutConsumer(0L, this);
131 if (task.replace(consumer)) {
132 firstTimeoutIndicator.subscribe(consumer);
133 }
134 }
135 }
136
137 @Override
138 public void onError(Throwable t) {
139 if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
140 task.dispose();
141
142 downstream.onError(t);
143 } else {
144 RxJavaPlugins.onError(t);
145 }
146 }
147
148 @Override
149 public void onComplete() {
150 if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
151 task.dispose();
152
153 downstream.onComplete();
154 }
155 }
156
157 @Override
158 public void onTimeout(long idx) {
159 if (compareAndSet(idx, Long.MAX_VALUE)) {
160 SubscriptionHelper.cancel(upstream);
161
162 downstream.onError(new TimeoutException());
163 }
164 }
165
166 @Override
167 public void onTimeoutError(long idx, Throwable ex) {
168 if (compareAndSet(idx, Long.MAX_VALUE)) {
169 SubscriptionHelper.cancel(upstream);
170
171 downstream.onError(ex);
172 } else {
173 RxJavaPlugins.onError(ex);
174 }
175 }
176
177 @Override
178 public void request(long n) {
179 SubscriptionHelper.deferredRequest(upstream, requested, n);
180 }
181
182 @Override
183 public void cancel() {
184 SubscriptionHelper.cancel(upstream);
185 task.dispose();
186 }
187 }
188
189 static final class TimeoutFallbackSubscriber<T> extends SubscriptionArbiter
190 implements FlowableSubscriber<T>, TimeoutSelectorSupport {
191
192 private static final long serialVersionUID = 3764492702657003550L;
193
194 final Subscriber<? super T> downstream;
195
196 final Function<? super T, ? extends Publisher<?>> itemTimeoutIndicator;
197
198 final SequentialDisposable task;
199
200 final AtomicReference<Subscription> upstream;
201
202 final AtomicLong index;
203
204 Publisher<? extends T> fallback;
205
206 long consumed;
207
208 TimeoutFallbackSubscriber(Subscriber<? super T> actual,
209 Function<? super T, ? extends Publisher<?>> itemTimeoutIndicator,
210 Publisher<? extends T> fallback) {
211 super(true);
212 this.downstream = actual;
213 this.itemTimeoutIndicator = itemTimeoutIndicator;
214 this.task = new SequentialDisposable();
215 this.upstream = new AtomicReference<>();
216 this.fallback = fallback;
217 this.index = new AtomicLong();
218 }
219
220 @Override
221 public void onSubscribe(Subscription s) {
222 if (SubscriptionHelper.setOnce(this.upstream, s)) {
223 setSubscription(s);
224 }
225 }
226
227 @Override
228 public void onNext(T t) {
229 long idx = index.get();
230 if (idx == Long.MAX_VALUE || !index.compareAndSet(idx, idx + 1)) {
231 return;
232 }
233
234 Disposable d = task.get();
235 if (d != null) {
236 d.dispose();
237 }
238
239 consumed++;
240
241 downstream.onNext(t);
242
243 Publisher<?> itemTimeoutPublisher;
244
245 try {
246 itemTimeoutPublisher = Objects.requireNonNull(
247 itemTimeoutIndicator.apply(t),
248 "The itemTimeoutIndicator returned a null Publisher.");
249 } catch (Throwable ex) {
250 Exceptions.throwIfFatal(ex);
251 upstream.get().cancel();
252 index.getAndSet(Long.MAX_VALUE);
253 downstream.onError(ex);
254 return;
255 }
256
257 TimeoutConsumer consumer = new TimeoutConsumer(idx + 1, this);
258 if (task.replace(consumer)) {
259 itemTimeoutPublisher.subscribe(consumer);
260 }
261 }
262
263 void startFirstTimeout(Publisher<?> firstTimeoutIndicator) {
264 if (firstTimeoutIndicator != null) {
265 TimeoutConsumer consumer = new TimeoutConsumer(0L, this);
266 if (task.replace(consumer)) {
267 firstTimeoutIndicator.subscribe(consumer);
268 }
269 }
270 }
271
272 @Override
273 public void onError(Throwable t) {
274 if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
275 task.dispose();
276
277 downstream.onError(t);
278
279 task.dispose();
280 } else {
281 RxJavaPlugins.onError(t);
282 }
283 }
284
285 @Override
286 public void onComplete() {
287 if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
288 task.dispose();
289
290 downstream.onComplete();
291
292 task.dispose();
293 }
294 }
295
296 @Override
297 public void onTimeout(long idx) {
298 if (index.compareAndSet(idx, Long.MAX_VALUE)) {
299 SubscriptionHelper.cancel(upstream);
300
301 Publisher<? extends T> f = fallback;
302 fallback = null;
303
304 long c = consumed;
305 if (c != 0L) {
306 produced(c);
307 }
308
309 f.subscribe(new FlowableTimeoutTimed.FallbackSubscriber<T>(downstream, this));
310 }
311 }
312
313 @Override
314 public void onTimeoutError(long idx, Throwable ex) {
315 if (index.compareAndSet(idx, Long.MAX_VALUE)) {
316 SubscriptionHelper.cancel(upstream);
317
318 downstream.onError(ex);
319 } else {
320 RxJavaPlugins.onError(ex);
321 }
322 }
323
324 @Override
325 public void cancel() {
326 super.cancel();
327 task.dispose();
328 }
329 }
330
331 static final class TimeoutConsumer extends AtomicReference<Subscription>
332 implements FlowableSubscriber<Object>, Disposable {
333
334 private static final long serialVersionUID = 8708641127342403073L;
335
336 final TimeoutSelectorSupport parent;
337
338 final long idx;
339
340 TimeoutConsumer(long idx, TimeoutSelectorSupport parent) {
341 this.idx = idx;
342 this.parent = parent;
343 }
344
345 @Override
346 public void onSubscribe(Subscription s) {
347 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
348 }
349
350 @Override
351 public void onNext(Object t) {
352 Subscription upstream = get();
353 if (upstream != SubscriptionHelper.CANCELLED) {
354 upstream.cancel();
355 lazySet(SubscriptionHelper.CANCELLED);
356 parent.onTimeout(idx);
357 }
358 }
359
360 @Override
361 public void onError(Throwable t) {
362 if (get() != SubscriptionHelper.CANCELLED) {
363 lazySet(SubscriptionHelper.CANCELLED);
364 parent.onTimeoutError(idx, t);
365 } else {
366 RxJavaPlugins.onError(t);
367 }
368 }
369
370 @Override
371 public void onComplete() {
372 if (get() != SubscriptionHelper.CANCELLED) {
373 lazySet(SubscriptionHelper.CANCELLED);
374 parent.onTimeout(idx);
375 }
376 }
377
378 @Override
379 public void dispose() {
380 SubscriptionHelper.cancel(this);
381 }
382
383 @Override
384 public boolean isDisposed() {
385 return this.get() == SubscriptionHelper.CANCELLED;
386 }
387 }
388
389 }