| 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 static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import org.reactivestreams.*; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable; |
| 25 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
|
| 28 |
public final class FlowableTimeoutTimed<T> extends AbstractFlowableWithUpstream<T, T> { |
| 29 |
final long timeout; |
| 30 |
final TimeUnit unit; |
| 31 |
final Scheduler scheduler; |
| 32 |
final Publisher<? extends T> other; |
| 33 |
|
| 34 |
public FlowableTimeoutTimed(Flowable<T> source, |
| 35 |
long timeout, TimeUnit unit, Scheduler scheduler, Publisher<? extends T> other) { |
| 36 |
super(source); |
| 37 |
this.timeout = timeout; |
| 38 |
this.unit = unit; |
| 39 |
this.scheduler = scheduler; |
| 40 |
this.other = other; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 45 |
if (other == null) { |
| 46 |
TimeoutSubscriber<T> parent = new TimeoutSubscriber<>(s, timeout, unit, scheduler.createWorker()); |
| 47 |
s.onSubscribe(parent); |
| 48 |
parent.startTimeout(0L); |
| 49 |
source.subscribe(parent); |
| 50 |
} else { |
| 51 |
TimeoutFallbackSubscriber<T> parent = new TimeoutFallbackSubscriber<>(s, timeout, unit, scheduler.createWorker(), other); |
| 52 |
s.onSubscribe(parent); |
| 53 |
parent.startTimeout(0L); |
| 54 |
source.subscribe(parent); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
static final class TimeoutSubscriber<T> extends AtomicLong |
| 59 |
implements FlowableSubscriber<T>, Subscription, TimeoutSupport { |
| 60 |
|
| 61 |
private static final long serialVersionUID = 3764492702657003550L; |
| 62 |
|
| 63 |
final Subscriber<? super T> downstream; |
| 64 |
|
| 65 |
final long timeout; |
| 66 |
|
| 67 |
final TimeUnit unit; |
| 68 |
|
| 69 |
final Scheduler.Worker worker; |
| 70 |
|
| 71 |
final SequentialDisposable task; |
| 72 |
|
| 73 |
final AtomicReference<Subscription> upstream; |
| 74 |
|
| 75 |
final AtomicLong requested; |
| 76 |
|
| 77 |
TimeoutSubscriber(Subscriber<? super T> actual, long timeout, TimeUnit unit, Scheduler.Worker worker) { |
| 78 |
this.downstream = actual; |
| 79 |
this.timeout = timeout; |
| 80 |
this.unit = unit; |
| 81 |
this.worker = worker; |
| 82 |
this.task = new SequentialDisposable(); |
| 83 |
this.upstream = new AtomicReference<>(); |
| 84 |
this.requested = new AtomicLong(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onSubscribe(Subscription s) { |
| 89 |
SubscriptionHelper.deferredSetOnce(upstream, requested, s); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onNext(T t) { |
| 94 |
long idx = get(); |
| 95 |
if (idx == Long.MAX_VALUE || !compareAndSet(idx, idx + 1)) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
task.get().dispose(); |
| 100 |
|
| 101 |
downstream.onNext(t); |
| 102 |
|
| 103 |
startTimeout(idx + 1); |
| 104 |
} |
| 105 |
|
| 106 |
void startTimeout(long nextIndex) { |
| 107 |
task.replace(worker.schedule(new TimeoutTask(nextIndex, this), timeout, unit)); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void onError(Throwable t) { |
| 112 |
if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 113 |
task.dispose(); |
| 114 |
|
| 115 |
downstream.onError(t); |
| 116 |
|
| 117 |
worker.dispose(); |
| 118 |
} else { |
| 119 |
RxJavaPlugins.onError(t); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void onComplete() { |
| 125 |
if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 126 |
task.dispose(); |
| 127 |
|
| 128 |
downstream.onComplete(); |
| 129 |
|
| 130 |
worker.dispose(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void onTimeout(long idx) { |
| 136 |
if (compareAndSet(idx, Long.MAX_VALUE)) { |
| 137 |
SubscriptionHelper.cancel(upstream); |
| 138 |
|
| 139 |
downstream.onError(new TimeoutException(timeoutMessage(timeout, unit))); |
| 140 |
|
| 141 |
worker.dispose(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void request(long n) { |
| 147 |
SubscriptionHelper.deferredRequest(upstream, requested, n); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void cancel() { |
| 152 |
SubscriptionHelper.cancel(upstream); |
| 153 |
worker.dispose(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static final class TimeoutTask implements Runnable { |
| 158 |
|
| 159 |
final TimeoutSupport parent; |
| 160 |
|
| 161 |
final long idx; |
| 162 |
|
| 163 |
TimeoutTask(long idx, TimeoutSupport parent) { |
| 164 |
this.idx = idx; |
| 165 |
this.parent = parent; |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void run() { |
| 170 |
parent.onTimeout(idx); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
static final class TimeoutFallbackSubscriber<T> extends SubscriptionArbiter |
| 175 |
implements FlowableSubscriber<T>, TimeoutSupport { |
| 176 |
|
| 177 |
private static final long serialVersionUID = 3764492702657003550L; |
| 178 |
|
| 179 |
final Subscriber<? super T> downstream; |
| 180 |
|
| 181 |
final long timeout; |
| 182 |
|
| 183 |
final TimeUnit unit; |
| 184 |
|
| 185 |
final Scheduler.Worker worker; |
| 186 |
|
| 187 |
final SequentialDisposable task; |
| 188 |
|
| 189 |
final AtomicReference<Subscription> upstream; |
| 190 |
|
| 191 |
final AtomicLong index; |
| 192 |
|
| 193 |
long consumed; |
| 194 |
|
| 195 |
Publisher<? extends T> fallback; |
| 196 |
|
| 197 |
TimeoutFallbackSubscriber(Subscriber<? super T> actual, long timeout, TimeUnit unit, |
| 198 |
Scheduler.Worker worker, Publisher<? extends T> fallback) { |
| 199 |
super(true); |
| 200 |
this.downstream = actual; |
| 201 |
this.timeout = timeout; |
| 202 |
this.unit = unit; |
| 203 |
this.worker = worker; |
| 204 |
this.fallback = fallback; |
| 205 |
this.task = new SequentialDisposable(); |
| 206 |
this.upstream = new AtomicReference<>(); |
| 207 |
this.index = new AtomicLong(); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void onSubscribe(Subscription s) { |
| 212 |
if (SubscriptionHelper.setOnce(upstream, s)) { |
| 213 |
setSubscription(s); |
| 214 |
} |
| 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 |
task.get().dispose(); |
| 225 |
|
| 226 |
consumed++; |
| 227 |
|
| 228 |
downstream.onNext(t); |
| 229 |
|
| 230 |
startTimeout(idx + 1); |
| 231 |
} |
| 232 |
|
| 233 |
void startTimeout(long nextIndex) { |
| 234 |
task.replace(worker.schedule(new TimeoutTask(nextIndex, this), timeout, unit)); |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void onError(Throwable t) { |
| 239 |
if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 240 |
task.dispose(); |
| 241 |
|
| 242 |
downstream.onError(t); |
| 243 |
|
| 244 |
worker.dispose(); |
| 245 |
} else { |
| 246 |
RxJavaPlugins.onError(t); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public void onComplete() { |
| 252 |
if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) { |
| 253 |
task.dispose(); |
| 254 |
|
| 255 |
downstream.onComplete(); |
| 256 |
|
| 257 |
worker.dispose(); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public void onTimeout(long idx) { |
| 263 |
if (index.compareAndSet(idx, Long.MAX_VALUE)) { |
| 264 |
SubscriptionHelper.cancel(upstream); |
| 265 |
|
| 266 |
long c = consumed; |
| 267 |
if (c != 0L) { |
| 268 |
produced(c); |
| 269 |
} |
| 270 |
|
| 271 |
Publisher<? extends T> f = fallback; |
| 272 |
fallback = null; |
| 273 |
|
| 274 |
f.subscribe(new FallbackSubscriber<T>(downstream, this)); |
| 275 |
|
| 276 |
worker.dispose(); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public void cancel() { |
| 282 |
super.cancel(); |
| 283 |
worker.dispose(); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
static final class FallbackSubscriber<T> implements FlowableSubscriber<T> { |
| 288 |
|
| 289 |
final Subscriber<? super T> downstream; |
| 290 |
|
| 291 |
final SubscriptionArbiter arbiter; |
| 292 |
|
| 293 |
FallbackSubscriber(Subscriber<? super T> actual, SubscriptionArbiter arbiter) { |
| 294 |
this.downstream = actual; |
| 295 |
this.arbiter = arbiter; |
| 296 |
} |
| 297 |
|
| 298 |
@Override |
| 299 |
public void onSubscribe(Subscription s) { |
| 300 |
arbiter.setSubscription(s); |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public void onNext(T t) { |
| 305 |
downstream.onNext(t); |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public void onError(Throwable t) { |
| 310 |
downstream.onError(t); |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public void onComplete() { |
| 315 |
downstream.onComplete(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
interface TimeoutSupport { |
| 320 |
|
| 321 |
void onTimeout(long idx); |
| 322 |
|
| 323 |
} |
| 324 |
} |