Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x: Fix Observable.window (size & time) cancellation and abandonment #6761

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public void onNext(T t) {
long i = index;

UnicastProcessor<T> w = window;
WindowSubscribeIntercept<T> intercept = null;
FlowableWindowSubscribeIntercept<T> intercept = null;
if (i == 0) {
getAndIncrement();

w = UnicastProcessor.<T>create(bufferSize, this);
window = w;

intercept = new WindowSubscribeIntercept<T>(w);
intercept = new FlowableWindowSubscribeIntercept<T>(w);
downstream.onNext(intercept);
}

Expand Down Expand Up @@ -211,15 +211,15 @@ public void onSubscribe(Subscription s) {
public void onNext(T t) {
long i = index;

WindowSubscribeIntercept<T> intercept = null;
FlowableWindowSubscribeIntercept<T> intercept = null;
UnicastProcessor<T> w = window;
if (i == 0) {
getAndIncrement();

w = UnicastProcessor.<T>create(bufferSize, this);
window = w;

intercept = new WindowSubscribeIntercept<T>(w);
intercept = new FlowableWindowSubscribeIntercept<T>(w);
downstream.onNext(intercept);
}

Expand Down Expand Up @@ -477,7 +477,7 @@ void drain() {
break;
}

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(t);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(t);
a.onNext(intercept);

if (intercept.tryAbandon()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
* @param <T> the element type of the flow.
* @since 3.0.0
*/
final class WindowSubscribeIntercept<T> extends Flowable<T> {
final class FlowableWindowSubscribeIntercept<T> extends Flowable<T> {

final FlowableProcessor<T> window;

final AtomicBoolean once;

WindowSubscribeIntercept(FlowableProcessor<T> source) {
FlowableWindowSubscribeIntercept(FlowableProcessor<T> source) {
this.window = source;
this.once = new AtomicBoolean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
import io.reactivex.rxjava3.internal.util.BackpressureHelper;
import io.reactivex.rxjava3.processors.UnicastProcessor;
import io.reactivex.rxjava3.subscribers.SerializedSubscriber;

public final class FlowableWindowTimed<T> extends AbstractFlowableWithUpstream<T, Flowable<T>> {
final long timespan;
Expand All @@ -53,23 +52,21 @@ public FlowableWindowTimed(Flowable<T> source,
}

@Override
protected void subscribeActual(Subscriber<? super Flowable<T>> s) {
SerializedSubscriber<Flowable<T>> actual = new SerializedSubscriber<Flowable<T>>(s);

protected void subscribeActual(Subscriber<? super Flowable<T>> downstream) {
if (timespan == timeskip) {
if (maxSize == Long.MAX_VALUE) {
source.subscribe(new WindowExactUnboundedSubscriber<T>(
actual,
downstream,
timespan, unit, scheduler, bufferSize));
return;
}
source.subscribe(new WindowExactBoundedSubscriber<T>(
actual,
downstream,
timespan, unit, scheduler,
bufferSize, maxSize, restartTimerOnMaxSize));
return;
}
source.subscribe(new WindowSkipSubscriber<T>(actual,
source.subscribe(new WindowSkipSubscriber<T>(downstream,
timespan, timeskip, unit, scheduler.createWorker(), bufferSize));
}

Expand Down Expand Up @@ -100,8 +97,8 @@ abstract static class AbstractWindowSubscriber<T>

final AtomicInteger windowCount;

AbstractWindowSubscriber(Subscriber<? super Flowable<T>> actual, long timespan, TimeUnit unit, int bufferSize) {
this.downstream = actual;
AbstractWindowSubscriber(Subscriber<? super Flowable<T>> downstream, long timespan, TimeUnit unit, int bufferSize) {
this.downstream = downstream;
this.queue = new MpscLinkedQueue<Object>();
this.timespan = timespan;
this.unit = unit;
Expand Down Expand Up @@ -204,7 +201,7 @@ void createFirstWindow() {

emitted = 1;

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(window);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(window);
downstream.onNext(intercept);

timer.replace(scheduler.schedulePeriodicallyDirect(this, timespan, timespan, unit));
Expand Down Expand Up @@ -293,7 +290,7 @@ else if (!isEmpty) {
window = UnicastProcessor.create(bufferSize, windowRunnable);
this.window = window;

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(window);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(window);
downstream.onNext(intercept);

if (intercept.tryAbandon()) {
Expand Down Expand Up @@ -372,7 +369,7 @@ void createFirstWindow() {
windowCount.getAndIncrement();
window = UnicastProcessor.create(bufferSize, this);

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(window);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(window);
downstream.onNext(intercept);

Runnable boundaryTask = new WindowBoundaryRunnable(this, 1L);
Expand Down Expand Up @@ -510,7 +507,7 @@ UnicastProcessor<T> createNewWindow(UnicastProcessor<T> window) {
window = UnicastProcessor.create(bufferSize, this);
this.window = window;

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(window);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(window);
downstream.onNext(intercept);

if (restartTimerOnMaxSize) {
Expand Down Expand Up @@ -573,7 +570,7 @@ void createFirstWindow() {
UnicastProcessor<T> window = UnicastProcessor.create(bufferSize, this);
windows.add(window);

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(window);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(window);
downstream.onNext(intercept);

worker.schedule(new WindowBoundaryRunnable(this, false), timespan, unit);
Expand Down Expand Up @@ -647,7 +644,7 @@ void drain() {
UnicastProcessor<T> window = UnicastProcessor.create(bufferSize, this);
windows.add(window);

WindowSubscribeIntercept<T> intercept = new WindowSubscribeIntercept<T>(window);
FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<T>(window);
downstream.onNext(intercept);

worker.schedule(new WindowBoundaryRunnable(this, false), timespan, unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ public void onSubscribe(Disposable d) {
@Override
public void onNext(T t) {
UnicastSubject<T> w = window;
ObservableWindowSubscribeIntercept<T> intercept = null;
if (w == null && !cancelled) {
w = UnicastSubject.create(capacityHint, this);
window = w;
downstream.onNext(w);
intercept = new ObservableWindowSubscribeIntercept<T>(w);
downstream.onNext(intercept);
}

if (w != null) {
w.onNext(t);

if (++size >= count) {
size = 0;
window = null;
Expand All @@ -93,6 +96,12 @@ public void onNext(T t) {
upstream.dispose();
}
}

if (intercept != null && intercept.tryAbandon()) {
w.onComplete();
w = null;
window = null;
}
}
}

Expand Down Expand Up @@ -180,11 +189,14 @@ public void onNext(T t) {

long s = skip;

ObservableWindowSubscribeIntercept<T> intercept = null;

if (i % s == 0 && !cancelled) {
wip.getAndIncrement();
UnicastSubject<T> w = UnicastSubject.create(capacityHint, this);
intercept = new ObservableWindowSubscribeIntercept<T>(w);
ws.offer(w);
downstream.onNext(w);
downstream.onNext(intercept);
}

long c = firstEmission + 1;
Expand All @@ -205,6 +217,10 @@ public void onNext(T t) {
}

index = i + 1;

if (intercept != null && intercept.tryAbandon()) {
intercept.window.onComplete();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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.observable;

import java.util.concurrent.atomic.AtomicBoolean;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.subjects.Subject;

/**
* Wrapper for a Subject that detects an incoming subscriber.
* @param <T> the element type of the flow.
* @since 3.0.0
*/
final class ObservableWindowSubscribeIntercept<T> extends Observable<T> {

final Subject<T> window;

final AtomicBoolean once;

ObservableWindowSubscribeIntercept(Subject<T> source) {
this.window = source;
this.once = new AtomicBoolean();
}

@Override
protected void subscribeActual(Observer<? super T> s) {
window.subscribe(s);
once.set(true);
}

boolean tryAbandon() {
return !once.get() && once.compareAndSet(false, true);
}
}
Loading