io.reactivex.rxjava3.internal.operators.flowable.FlowableTimeout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/*
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.internal.operators.flowable;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
import io.reactivex.rxjava3.internal.operators.flowable.FlowableTimeoutTimed.TimeoutSupport;
import io.reactivex.rxjava3.internal.subscriptions.*;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
public final class FlowableTimeout extends AbstractFlowableWithUpstream {
final Publisher firstTimeoutIndicator;
final Function super T, ? extends Publisher> itemTimeoutIndicator;
final Publisher extends T> other;
public FlowableTimeout(
Flowable source,
Publisher firstTimeoutIndicator,
Function super T, ? extends Publisher> itemTimeoutIndicator,
Publisher extends T> other) {
super(source);
this.firstTimeoutIndicator = firstTimeoutIndicator;
this.itemTimeoutIndicator = itemTimeoutIndicator;
this.other = other;
}
@Override
protected void subscribeActual(Subscriber super T> s) {
if (other == null) {
TimeoutSubscriber parent = new TimeoutSubscriber<>(s, itemTimeoutIndicator);
s.onSubscribe(parent);
parent.startFirstTimeout(firstTimeoutIndicator);
source.subscribe(parent);
} else {
TimeoutFallbackSubscriber parent = new TimeoutFallbackSubscriber<>(s, itemTimeoutIndicator, other);
s.onSubscribe(parent);
parent.startFirstTimeout(firstTimeoutIndicator);
source.subscribe(parent);
}
}
interface TimeoutSelectorSupport extends TimeoutSupport {
void onTimeoutError(long idx, Throwable ex);
}
static final class TimeoutSubscriber extends AtomicLong
implements FlowableSubscriber, Subscription, TimeoutSelectorSupport {
private static final long serialVersionUID = 3764492702657003550L;
final Subscriber super T> downstream;
final Function super T, ? extends Publisher>> itemTimeoutIndicator;
final SequentialDisposable task;
final AtomicReference upstream;
final AtomicLong requested;
TimeoutSubscriber(Subscriber super T> actual, Function super T, ? extends Publisher>> itemTimeoutIndicator) {
this.downstream = actual;
this.itemTimeoutIndicator = itemTimeoutIndicator;
this.task = new SequentialDisposable();
this.upstream = new AtomicReference<>();
this.requested = new AtomicLong();
}
@Override
public void onSubscribe(Subscription s) {
SubscriptionHelper.deferredSetOnce(upstream, requested, s);
}
@Override
public void onNext(T t) {
long idx = get();
if (idx == Long.MAX_VALUE || !compareAndSet(idx, idx + 1)) {
return;
}
Disposable d = task.get();
if (d != null) {
d.dispose();
}
downstream.onNext(t);
Publisher> itemTimeoutPublisher;
try {
itemTimeoutPublisher = Objects.requireNonNull(
itemTimeoutIndicator.apply(t),
"The itemTimeoutIndicator returned a null Publisher.");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.get().cancel();
getAndSet(Long.MAX_VALUE);
downstream.onError(ex);
return;
}
TimeoutConsumer consumer = new TimeoutConsumer(idx + 1, this);
if (task.replace(consumer)) {
itemTimeoutPublisher.subscribe(consumer);
}
}
void startFirstTimeout(Publisher> firstTimeoutIndicator) {
if (firstTimeoutIndicator != null) {
TimeoutConsumer consumer = new TimeoutConsumer(0L, this);
if (task.replace(consumer)) {
firstTimeoutIndicator.subscribe(consumer);
}
}
}
@Override
public void onError(Throwable t) {
if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
task.dispose();
downstream.onError(t);
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
task.dispose();
downstream.onComplete();
}
}
@Override
public void onTimeout(long idx) {
if (compareAndSet(idx, Long.MAX_VALUE)) {
SubscriptionHelper.cancel(upstream);
downstream.onError(new TimeoutException());
}
}
@Override
public void onTimeoutError(long idx, Throwable ex) {
if (compareAndSet(idx, Long.MAX_VALUE)) {
SubscriptionHelper.cancel(upstream);
downstream.onError(ex);
} else {
RxJavaPlugins.onError(ex);
}
}
@Override
public void request(long n) {
SubscriptionHelper.deferredRequest(upstream, requested, n);
}
@Override
public void cancel() {
SubscriptionHelper.cancel(upstream);
task.dispose();
}
}
static final class TimeoutFallbackSubscriber extends SubscriptionArbiter
implements FlowableSubscriber, TimeoutSelectorSupport {
private static final long serialVersionUID = 3764492702657003550L;
final Subscriber super T> downstream;
final Function super T, ? extends Publisher>> itemTimeoutIndicator;
final SequentialDisposable task;
final AtomicReference upstream;
final AtomicLong index;
Publisher extends T> fallback;
long consumed;
TimeoutFallbackSubscriber(Subscriber super T> actual,
Function super T, ? extends Publisher>> itemTimeoutIndicator,
Publisher extends T> fallback) {
super(true);
this.downstream = actual;
this.itemTimeoutIndicator = itemTimeoutIndicator;
this.task = new SequentialDisposable();
this.upstream = new AtomicReference<>();
this.fallback = fallback;
this.index = new AtomicLong();
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.setOnce(this.upstream, s)) {
setSubscription(s);
}
}
@Override
public void onNext(T t) {
long idx = index.get();
if (idx == Long.MAX_VALUE || !index.compareAndSet(idx, idx + 1)) {
return;
}
Disposable d = task.get();
if (d != null) {
d.dispose();
}
consumed++;
downstream.onNext(t);
Publisher> itemTimeoutPublisher;
try {
itemTimeoutPublisher = Objects.requireNonNull(
itemTimeoutIndicator.apply(t),
"The itemTimeoutIndicator returned a null Publisher.");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
upstream.get().cancel();
index.getAndSet(Long.MAX_VALUE);
downstream.onError(ex);
return;
}
TimeoutConsumer consumer = new TimeoutConsumer(idx + 1, this);
if (task.replace(consumer)) {
itemTimeoutPublisher.subscribe(consumer);
}
}
void startFirstTimeout(Publisher> firstTimeoutIndicator) {
if (firstTimeoutIndicator != null) {
TimeoutConsumer consumer = new TimeoutConsumer(0L, this);
if (task.replace(consumer)) {
firstTimeoutIndicator.subscribe(consumer);
}
}
}
@Override
public void onError(Throwable t) {
if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
task.dispose();
downstream.onError(t);
task.dispose();
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
if (index.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
task.dispose();
downstream.onComplete();
task.dispose();
}
}
@Override
public void onTimeout(long idx) {
if (index.compareAndSet(idx, Long.MAX_VALUE)) {
SubscriptionHelper.cancel(upstream);
Publisher extends T> f = fallback;
fallback = null;
long c = consumed;
if (c != 0L) {
produced(c);
}
f.subscribe(new FlowableTimeoutTimed.FallbackSubscriber(downstream, this));
}
}
@Override
public void onTimeoutError(long idx, Throwable ex) {
if (index.compareAndSet(idx, Long.MAX_VALUE)) {
SubscriptionHelper.cancel(upstream);
downstream.onError(ex);
} else {
RxJavaPlugins.onError(ex);
}
}
@Override
public void cancel() {
super.cancel();
task.dispose();
}
}
static final class TimeoutConsumer extends AtomicReference
implements FlowableSubscriber
© 2015 - 2024 Weber Informatics LLC | Privacy Policy