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