Skip to content

Commit 2af42cd

Browse files
committed
rename Util to BackpressureUtils, add AtomicLong overload to BackpressureUtil, add javadoc to Producer and Subscriber
1 parent 9a2a311 commit 2af42cd

File tree

6 files changed

+89
-50
lines changed

6 files changed

+89
-50
lines changed

src/main/java/rx/Producer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ 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 the total requests overflows {@code Long.MAX_VALUE} then the total requests
28+
* is set to {@code Long.MAX_VALUE}. Arriving at {@code Long.MAX_VALUE} by addition of requests cannot be
29+
* assumed to disable backpressure.
2630
*
2731
* @param n the maximum number of items you want this Producer to produce, or {@code Long.MAX_VALUE} if you
2832
* want the Producer to produce items at its own pace

src/main/java/rx/Subscriber.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ 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 the total requests overflows {@code Long.MAX_VALUE} then the total requests
94+
* is set to {@code Long.MAX_VALUE}. Arriving at {@code Long.MAX_VALUE} by addition of requests cannot be
95+
* assumed to disable backpressure.
96+
*
9397
* @param n the maximum number of items you want the Observable to emit to the Subscriber at this time, or
9498
* {@code Long.MAX_VALUE} if you want the Observable to emit items at its own pace
9599
* @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 = Util.getAndAddRequest(REQUESTED_UPDATER, 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public void request(long n) {
545545
if (n == Long.MAX_VALUE) {
546546
requested = Long.MAX_VALUE;
547547
} else {
548-
Util.getAndAddRequest(REQUESTED, this, n);
548+
BackpressureUtils.getAndAddRequest(REQUESTED, this, n);
549549
if (ms.drainQueuesIfNeeded()) {
550550
boolean sendComplete = false;
551551
synchronized (ms) {

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)