From 4c0c4dbc54aa40619066a19c83cb21257fa3e07b Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Mon, 9 Sep 2013 23:51:43 -0700 Subject: [PATCH 1/2] Operator: throttleWithTimeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Another take on `throttle` … I believe this matches Rx.Net behavior. This will wait until timeout value has passed without any further values before emitting the received value. --- rxjava-core/src/main/java/rx/Observable.java | 31 +++ .../java/rx/concurrency/TestScheduler.java | 27 +- .../OperationThrottleWithTimeout.java | 250 ++++++++++++++++++ .../java/rx/ThrottleWithTimeoutTests.java | 46 ++++ 4 files changed, 349 insertions(+), 5 deletions(-) create mode 100644 rxjava-core/src/main/java/rx/operators/OperationThrottleWithTimeout.java create mode 100644 rxjava-core/src/test/java/rx/ThrottleWithTimeoutTests.java diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 2ac0f42754..7ae3326bf8 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -64,6 +64,7 @@ import rx.operators.OperationTakeLast; import rx.operators.OperationTakeUntil; import rx.operators.OperationTakeWhile; +import rx.operators.OperationThrottleWithTimeout; import rx.operators.OperationTimestamp; import rx.operators.OperationToObservableFuture; import rx.operators.OperationToObservableIterable; @@ -1809,6 +1810,36 @@ public static Observable interval(long interval, TimeUnit unit, Scheduler return create(OperationInterval.interval(interval, unit, scheduler)); } + /** + * Throttles the {@link Observable} by dropping values which are followed by newer values before the timer has expired. + * + * @param timeout + * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. + * + * @param unit + * The {@link TimeUnit} for the timeout. + * + * @return An {@link Observable} which filters out values which are too quickly followed up with newer values. + */ + public Observable throttleWithTimeout(long timeout, TimeUnit unit) { + return create(OperationThrottleWithTimeout.throttleWithTimeout(this, timeout, unit)); + } + + /** + * Throttles the {@link Observable} by dropping values which are followed by newer values before the timer has expired. + * + * @param timeout + * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. + * @param unit + * The {@link TimeUnit} for the timeout. + * @param scheduler + * The {@link Scheduler} to use when timing incoming values. + * @return An {@link Observable} which filters out values which are too quickly followed up with newer values. + */ + public Observable throttleWithTimeout(long timeout, TimeUnit unit, Scheduler scheduler) { + return create(OperationThrottleWithTimeout.throttleWithTimeout(this, timeout, unit, scheduler)); + } + /** * Wraps each item emitted by a source Observable in a {@link Timestamped} object. *

diff --git a/rxjava-core/src/main/java/rx/concurrency/TestScheduler.java b/rxjava-core/src/main/java/rx/concurrency/TestScheduler.java index 7afab7ec42..04b8c1a2c5 100644 --- a/rxjava-core/src/main/java/rx/concurrency/TestScheduler.java +++ b/rxjava-core/src/main/java/rx/concurrency/TestScheduler.java @@ -19,20 +19,22 @@ import java.util.PriorityQueue; import java.util.Queue; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import rx.Scheduler; import rx.Subscription; -import rx.subscriptions.Subscriptions; import rx.util.functions.Func2; public class TestScheduler extends Scheduler { private final Queue> queue = new PriorityQueue>(11, new CompareActionsByTime()); private static class TimedAction { + private final long time; private final Func2 action; private final T state; private final TestScheduler scheduler; + private final AtomicBoolean isCancelled = new AtomicBoolean(false); private TimedAction(TestScheduler scheduler, long time, Func2 action, T state) { this.time = time; @@ -41,6 +43,10 @@ private TimedAction(TestScheduler scheduler, long time, Func2) current.action).call(current.scheduler, current.state); + + // Only execute if the TimedAction has not yet been cancelled + if (!current.isCancelled.get()) { + // because the queue can have wildcards we have to ignore the type T for the state + ((Func2) current.action).call(current.scheduler, current.state); + } } time = targetTimeInNanos; } @@ -97,7 +107,14 @@ public Subscription schedule(T state, Func2 Subscription schedule(T state, Func2 action, long delayTime, TimeUnit unit) { - queue.add(new TimedAction(this, time + unit.toNanos(delayTime), action, state)); - return Subscriptions.empty(); + final TimedAction timedAction = new TimedAction(this, time + unit.toNanos(delayTime), action, state); + queue.add(timedAction); + + return new Subscription() { + @Override + public void unsubscribe() { + timedAction.cancel(); + } + }; } } diff --git a/rxjava-core/src/main/java/rx/operators/OperationThrottleWithTimeout.java b/rxjava-core/src/main/java/rx/operators/OperationThrottleWithTimeout.java new file mode 100644 index 0000000000..a6c77c2084 --- /dev/null +++ b/rxjava-core/src/main/java/rx/operators/OperationThrottleWithTimeout.java @@ -0,0 +1,250 @@ +/** + * Copyright 2013 Netflix, Inc. + * + * 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 rx.operators; + +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; + +import rx.Observable; +import rx.Observable.OnSubscribeFunc; +import rx.Observer; +import rx.Scheduler; +import rx.Subscription; +import rx.concurrency.Schedulers; +import rx.concurrency.TestScheduler; +import rx.subscriptions.Subscriptions; +import rx.util.functions.Action0; +import rx.util.functions.Func1; + +/** + * This operation is used to filter out bursts of events. This is done by ignoring the events from an observable which are too + * quickly followed up with other values. Values which are not followed up by other values within the specified timeout are published + * as soon as the timeout expires. + */ +public final class OperationThrottleWithTimeout { + + /** + * This operation filters out events which are published too quickly in succession. This is done by dropping events which are + * followed up by other events before a specified timer has expired. If the timer expires and no follow up event was published (yet) + * the last received event is published. + * + * @param items + * The {@link Observable} which is publishing events. + * @param timeout + * How long each event has to be the 'last event' before it gets published. + * @param unit + * The unit of time for the specified timeout. + * @return A {@link Func1} which performs the throttle operation. + */ + public static OnSubscribeFunc throttleWithTimeout(Observable items, long timeout, TimeUnit unit) { + return throttleWithTimeout(items, timeout, unit, Schedulers.threadPoolForComputation()); + } + + /** + * This operation filters out events which are published too quickly in succession. This is done by dropping events which are + * followed up by other events before a specified timer has expired. If the timer expires and no follow up event was published (yet) + * the last received event is published. + * + * @param items + * The {@link Observable} which is publishing events. + * @param timeout + * How long each event has to be the 'last event' before it gets published. + * @param unit + * The unit of time for the specified timeout. + * @param scheduler + * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. + * @return A {@link Func1} which performs the throttle operation. + */ + public static OnSubscribeFunc throttleWithTimeout(final Observable items, final long timeout, final TimeUnit unit, final Scheduler scheduler) { + return new OnSubscribeFunc() { + @Override + public Subscription onSubscribe(Observer observer) { + return new Throttle(items, timeout, unit, scheduler).onSubscribe(observer); + } + }; + } + + private static class Throttle implements OnSubscribeFunc { + + private final Observable items; + private final long timeout; + private final TimeUnit unit; + private final Scheduler scheduler; + + public Throttle(Observable items, long timeout, TimeUnit unit, Scheduler scheduler) { + this.items = items; + this.timeout = timeout; + this.unit = unit; + this.scheduler = scheduler; + } + + @Override + public Subscription onSubscribe(Observer observer) { + return items.subscribe(new ThrottledObserver(observer, timeout, unit, scheduler)); + } + } + + private static class ThrottledObserver implements Observer { + + private final Observer observer; + private final long timeout; + private final TimeUnit unit; + private final Scheduler scheduler; + + private final AtomicReference lastScheduledNotification = new AtomicReference(); + + public ThrottledObserver(Observer observer, long timeout, TimeUnit unit, Scheduler scheduler) { + this.observer = observer; + this.timeout = timeout; + this.unit = unit; + this.scheduler = scheduler; + } + + @Override + public void onCompleted() { + observer.onCompleted(); + } + + @Override + public void onError(Throwable e) { + lastScheduledNotification.get().unsubscribe(); + observer.onError(e); + } + + @Override + public void onNext(final T v) { + Subscription previousSubscription = lastScheduledNotification.getAndSet(scheduler.schedule(new Action0() { + + @Override + public void call() { + observer.onNext(v); + } + + }, timeout, unit)); + // cancel previous if not already executed + if (previousSubscription != null) { + previousSubscription.unsubscribe(); + } + } + } + + public static class UnitTest { + + private TestScheduler scheduler; + private Observer observer; + + @Before + @SuppressWarnings("unchecked") + public void before() { + scheduler = new TestScheduler(); + observer = mock(Observer.class); + } + + @Test + public void testThrottlingWithCompleted() { + Observable source = Observable.create(new OnSubscribeFunc() { + @Override + public Subscription onSubscribe(Observer observer) { + publishNext(observer, 100, "one"); // Should be skipped since "two" will arrive before the timeout expires. + publishNext(observer, 400, "two"); // Should be published since "three" will arrive after the timeout expires. + publishNext(observer, 900, "three"); // Should be skipped since onCompleted will arrive before the timeout expires. + publishCompleted(observer, 1000); // Should be published as soon as the timeout expires. + + return Subscriptions.empty(); + } + }); + + Observable sampled = Observable.create(OperationThrottleWithTimeout.throttleWithTimeout(source, 400, TimeUnit.MILLISECONDS, scheduler)); + sampled.subscribe(observer); + + scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS); + InOrder inOrder = inOrder(observer); + // must go to 800 since it must be 400 after when two is sent, which is at 400 + scheduler.advanceTimeTo(800, TimeUnit.MILLISECONDS); + inOrder.verify(observer, times(1)).onNext("two"); + scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS); + inOrder.verify(observer, times(1)).onCompleted(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testThrottlingWithError() { + Observable source = Observable.create(new OnSubscribeFunc() { + @Override + public Subscription onSubscribe(Observer observer) { + Exception error = new TestException(); + publishNext(observer, 100, "one"); // Should be published since "two" will arrive after the timeout expires. + publishNext(observer, 600, "two"); // Should be skipped since onError will arrive before the timeout expires. + publishError(observer, 700, error); // Should be published as soon as the timeout expires. + + return Subscriptions.empty(); + } + }); + + Observable sampled = Observable.create(OperationThrottleWithTimeout.throttleWithTimeout(source, 400, TimeUnit.MILLISECONDS, scheduler)); + sampled.subscribe(observer); + + scheduler.advanceTimeTo(0, TimeUnit.MILLISECONDS); + InOrder inOrder = inOrder(observer); + // 100 + 400 means it triggers at 500 + scheduler.advanceTimeTo(500, TimeUnit.MILLISECONDS); + inOrder.verify(observer).onNext("one"); + scheduler.advanceTimeTo(701, TimeUnit.MILLISECONDS); + inOrder.verify(observer).onError(any(TestException.class)); + inOrder.verifyNoMoreInteractions(); + } + + private void publishCompleted(final Observer observer, long delay) { + scheduler.schedule(new Action0() { + @Override + public void call() { + observer.onCompleted(); + } + }, delay, TimeUnit.MILLISECONDS); + } + + private void publishError(final Observer observer, long delay, final Exception error) { + scheduler.schedule(new Action0() { + @Override + public void call() { + observer.onError(error); + } + }, delay, TimeUnit.MILLISECONDS); + } + + private void publishNext(final Observer observer, final long delay, final T value) { + scheduler.schedule(new Action0() { + @Override + public void call() { + observer.onNext(value); + } + }, delay, TimeUnit.MILLISECONDS); + } + + @SuppressWarnings("serial") + private class TestException extends Exception { + } + + } + +} diff --git a/rxjava-core/src/test/java/rx/ThrottleWithTimeoutTests.java b/rxjava-core/src/test/java/rx/ThrottleWithTimeoutTests.java new file mode 100644 index 0000000000..3503fe7adb --- /dev/null +++ b/rxjava-core/src/test/java/rx/ThrottleWithTimeoutTests.java @@ -0,0 +1,46 @@ +package rx; + +import static org.mockito.Mockito.*; + +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.mockito.InOrder; + +import rx.concurrency.TestScheduler; +import rx.subjects.PublishSubject; + +public class ThrottleWithTimeoutTests { + + @Test + public void testThrottle() { + @SuppressWarnings("unchecked") + Observer observer = mock(Observer.class); + TestScheduler s = new TestScheduler(); + PublishSubject o = PublishSubject.create(); + o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s).subscribe(observer); + + // send events with simulated time increments + s.advanceTimeTo(0, TimeUnit.MILLISECONDS); + o.onNext(1); // skip + o.onNext(2); // deliver + s.advanceTimeTo(501, TimeUnit.MILLISECONDS); + o.onNext(3); // skip + s.advanceTimeTo(600, TimeUnit.MILLISECONDS); + o.onNext(4); // skip + s.advanceTimeTo(700, TimeUnit.MILLISECONDS); + o.onNext(5); // skip + o.onNext(6); // deliver at 1300 after 500ms has passed since onNext(5) + s.advanceTimeTo(1300, TimeUnit.MILLISECONDS); + o.onNext(7); // deliver + s.advanceTimeTo(1800, TimeUnit.MILLISECONDS); + o.onCompleted(); + + InOrder inOrder = inOrder(observer); + inOrder.verify(observer).onNext(2); + inOrder.verify(observer).onNext(6); + inOrder.verify(observer).onNext(7); + inOrder.verify(observer).onCompleted(); + inOrder.verifyNoMoreInteractions(); + } +} From 2a3ade2d6162dc328a340ce247ccdf569284ffa8 Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Tue, 10 Sep 2013 00:13:51 -0700 Subject: [PATCH 2/2] Update javadoc for throttleWithTimeout --- rxjava-core/src/main/java/rx/Observable.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 7ae3326bf8..93b1f3c5ac 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -1811,11 +1811,12 @@ public static Observable interval(long interval, TimeUnit unit, Scheduler } /** - * Throttles the {@link Observable} by dropping values which are followed by newer values before the timer has expired. + * Throttles by dropping all values that are followed by newer values before the timeout value expires. The timer reset on each `onNext` call. + *

+ * NOTE: If the timeout is set higher than the rate of traffic then this will drop all data. * * @param timeout * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. - * * @param unit * The {@link TimeUnit} for the timeout. * @@ -1826,15 +1827,17 @@ public Observable throttleWithTimeout(long timeout, TimeUnit unit) { } /** - * Throttles the {@link Observable} by dropping values which are followed by newer values before the timer has expired. + * Throttles by dropping all values that are followed by newer values before the timeout value expires. The timer reset on each `onNext` call. + *

+ * NOTE: If the timeout is set higher than the rate of traffic then this will drop all data. * * @param timeout * The time each value has to be 'the most recent' of the {@link Observable} to ensure that it's not dropped. * @param unit - * The {@link TimeUnit} for the timeout. + * The unit of time for the specified timeout. * @param scheduler - * The {@link Scheduler} to use when timing incoming values. - * @return An {@link Observable} which filters out values which are too quickly followed up with newer values. + * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. + * @return Observable which performs the throttle operation. */ public Observable throttleWithTimeout(long timeout, TimeUnit unit, Scheduler scheduler) { return create(OperationThrottleWithTimeout.throttleWithTimeout(this, timeout, unit, scheduler));