Skip to content

Commit 8496582

Browse files
committed
add request overflow check for OnSubscribeFromIterable
1 parent a0549a8 commit 8496582

File tree

6 files changed

+137
-12
lines changed

6 files changed

+137
-12
lines changed

src/main/java/rx/Producer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public interface Producer {
2323
/**
2424
* Request a certain maximum number of items from this Producer. This is a way of requesting backpressure.
2525
* To disable backpressure, pass {@code Long.MAX_VALUE} to this method.
26+
* <p>
27+
* Requests are additive but if a sequence of requests totals more than {@code Long.MAX_VALUE} then
28+
* {@code Long.MAX_VALUE} requests will be actioned and the extras <i>may</i> be ignored. Arriving at
29+
* {@code Long.MAX_VALUE} by addition of requests cannot be assumed to disable backpressure. For example,
30+
* the code below may result in {@code Long.MAX_VALUE} requests being actioned only.
31+
*
32+
* <pre>
33+
* request(100);
34+
* request(Long.MAX_VALUE-1);
35+
* </pre>
2636
*
2737
* @param n the maximum number of items you want this Producer to produce, or {@code Long.MAX_VALUE} if you
2838
* want the Producer to produce items at its own pace

src/main/java/rx/Subscriber.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ public void onStart() {
8989
* Request a certain maximum number of emitted items from the Observable this Subscriber is subscribed to.
9090
* This is a way of requesting backpressure. To disable backpressure, pass {@code Long.MAX_VALUE} to this
9191
* method.
92-
*
92+
* <p>
93+
* Requests are additive but if a sequence of requests totals more than {@code Long.MAX_VALUE} then
94+
* {@code Long.MAX_VALUE} requests will be actioned and the extras <i>may</i> be ignored. Arriving at
95+
* {@code Long.MAX_VALUE} by addition of requests cannot be assumed to disable backpressure. For example,
96+
* the code below may result in {@code Long.MAX_VALUE} requests being actioned only.
97+
*
98+
* <pre>
99+
* request(100);
100+
* request(Long.MAX_VALUE-1);
101+
* </pre>
102+
*
93103
* @param n the maximum number of items you want the Observable to emit to the Subscriber at this time, or
94104
* {@code Long.MAX_VALUE} if you want the Observable to emit items at its own pace
95105
* @throws IllegalArgumentException
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright 2015 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package rx.internal.operators;
17+
18+
import java.util.concurrent.atomic.AtomicLong;
19+
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
20+
21+
/**
22+
* Utility functions for use with backpressure.
23+
*
24+
*/
25+
final class BackpressureUtils {
26+
27+
/**
28+
* Adds {@code n} to {@code requested} field and returns the value prior to
29+
* addition once the addition is successful (uses CAS semantics). If
30+
* overflows then sets {@code requested} field to {@code Long.MAX_VALUE}.
31+
*
32+
* @param requested
33+
* atomic field updater for a request count
34+
* @param object
35+
* contains the field updated by the updater
36+
* @param n
37+
* the number of requests to add to the requested count
38+
* @return requested value just prior to successful addition
39+
*/
40+
static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) {
41+
// add n to field but check for overflow
42+
while (true) {
43+
long current = requested.get(object);
44+
long next = current + n;
45+
// check for overflow
46+
if (next < 0)
47+
next = Long.MAX_VALUE;
48+
if (requested.compareAndSet(object, current, next))
49+
return current;
50+
}
51+
}
52+
53+
/**
54+
* Adds {@code n} to {@code requested} and returns the value prior to addition once the
55+
* addition is successful (uses CAS semantics). If overflows then sets
56+
* {@code requested} field to {@code Long.MAX_VALUE}.
57+
*
58+
* @param requested
59+
* atomic field updater for a request count
60+
* @param object
61+
* contains the field updated by the updater
62+
* @param n
63+
* the number of requests to add to the requested count
64+
* @return requested value just prior to successful addition
65+
*/
66+
static <T> long getAndAddRequest(AtomicLong requested, long n) {
67+
// add n to field but check for overflow
68+
while (true) {
69+
long current = requested.get();
70+
long next = current + n;
71+
// check for overflow
72+
if (next < 0)
73+
next = Long.MAX_VALUE;
74+
if (requested.compareAndSet(current, next))
75+
return current;
76+
}
77+
}
78+
}

src/main/java/rx/internal/operators/OnSubscribeFromIterable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void request(long n) {
8080
}
8181
} else if(n > 0) {
8282
// backpressure is requested
83-
long _c = REQUESTED_UPDATER.getAndAdd(this, n);
83+
long _c = BackpressureUtils.getAndAddRequest(REQUESTED_UPDATER, this, n);
8484
if (_c == 0) {
8585
while (true) {
8686
/*

src/main/java/rx/internal/operators/OperatorMerge.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -545,16 +545,7 @@ public void request(long n) {
545545
if (n == Long.MAX_VALUE) {
546546
requested = Long.MAX_VALUE;
547547
} else {
548-
// add n to requested but check for overflow
549-
while (true) {
550-
long current = REQUESTED.get(this);
551-
long next = current + n;
552-
//check for overflow
553-
if (next < 0)
554-
next = Long.MAX_VALUE;
555-
if (REQUESTED.compareAndSet(this, current, next))
556-
break;
557-
}
548+
BackpressureUtils.getAndAddRequest(REQUESTED, this, n);
558549
if (ms.drainQueuesIfNeeded()) {
559550
boolean sendComplete = false;
560551
synchronized (ms) {

src/test/java/rx/internal/operators/OnSubscribeFromIterableTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package rx.internal.operators;
1717

18+
import static org.junit.Assert.assertTrue;
1819
import static org.mockito.Matchers.any;
1920
import static org.mockito.Mockito.mock;
2021
import static org.mockito.Mockito.times;
@@ -24,14 +25,18 @@
2425
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.Iterator;
28+
import java.util.concurrent.CountDownLatch;
29+
import java.util.concurrent.TimeUnit;
2730

2831
import org.junit.Test;
2932
import org.mockito.Mockito;
3033

3134
import rx.Observable;
3235
import rx.Observer;
36+
import rx.Subscriber;
3337
import rx.internal.util.RxRingBuffer;
3438
import rx.observers.TestSubscriber;
39+
import rx.schedulers.Schedulers;
3540

3641
public class OnSubscribeFromIterableTest {
3742

@@ -157,5 +162,36 @@ public void testSubscribeMultipleTimes() {
157162
o.call(ts);
158163
ts.assertReceivedOnNext(Arrays.asList(1, 2, 3));
159164
}
165+
166+
@Test
167+
public void testFromIterableRequestOverflow() throws InterruptedException {
168+
Observable<Integer> o = Observable.from(Arrays.asList(1,2,3,4));
169+
final int expectedCount = 4;
170+
final CountDownLatch latch = new CountDownLatch(expectedCount);
171+
o.subscribeOn(Schedulers.computation()).subscribe(new Subscriber<Integer>() {
172+
173+
@Override
174+
public void onStart() {
175+
request(2);
176+
}
177+
178+
@Override
179+
public void onCompleted() {
180+
//ignore
181+
}
182+
183+
@Override
184+
public void onError(Throwable e) {
185+
throw new RuntimeException(e);
186+
}
187+
188+
@Override
189+
public void onNext(Integer t) {
190+
latch.countDown();
191+
request(Long.MAX_VALUE-1);
192+
}});
193+
assertTrue(latch.await(10, TimeUnit.SECONDS));
194+
}
195+
160196

161197
}

0 commit comments

Comments
 (0)