hu.akarnokd.rxjava3.operators.FlowableOnBackpressureTimeout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava3-extensions Show documentation
Show all versions of rxjava3-extensions Show documentation
RxJava 3.x extra sources, operators and components and ports of many 1.x companion libraries.
/*
* Copyright 2016-2019 David Karnok
*
* 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 hu.akarnokd.rxjava3.operators;
import java.util.ArrayDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.core.Scheduler.Worker;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
import io.reactivex.rxjava3.internal.util.BackpressureHelper;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
/**
* If the downstream doesn't request, it buffers events and times out
* old elements from the front of the buffer.
* @param the input and output element type
*/
final class FlowableOnBackpressureTimeout extends Flowable
implements FlowableTransformer {
final Publisher source;
final int maxSize;
final long timeout;
final TimeUnit unit;
final Scheduler scheduler;
final Consumer super T> onEvict;
FlowableOnBackpressureTimeout(Publisher source, int maxSize, long timeout, TimeUnit unit,
Scheduler scheduler, Consumer super T> onEvict) {
this.source = source;
this.maxSize = maxSize;
this.timeout = timeout;
this.unit = unit;
this.scheduler = scheduler;
this.onEvict = onEvict;
}
@Override
public Publisher apply(Flowable upstream) {
return new FlowableOnBackpressureTimeout(upstream, maxSize, timeout, unit, scheduler, onEvict);
}
@Override
protected void subscribeActual(Subscriber super T> s) {
source.subscribe(new OnBackpressureTimeoutSubscriber(s, maxSize, timeout, unit, scheduler.createWorker(), onEvict));
}
static final class OnBackpressureTimeoutSubscriber
extends AtomicInteger
implements Subscriber, Subscription, Runnable {
private static final long serialVersionUID = 2264324530873250941L;
final Subscriber super T> downstream;
final AtomicLong requested;
final int maxSizeDouble;
final long timeout;
final TimeUnit unit;
final Worker worker;
final Consumer super T> onEvict;
Subscription upstream;
final ArrayDeque