-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathBackpressureHelper.java
96 lines (90 loc) · 3.16 KB
/
BackpressureHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Copyright 2016 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 io.reactivex.internal.util;
import java.util.concurrent.atomic.AtomicLong;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Utility class to help with backpressure-related operations such as request aggregation.
*/
public enum BackpressureHelper {
;
/**
* Adds two long values and caps the sum at Long.MAX_VALUE.
* @param a the first value
* @param b the second value
* @return the sum capped at Long.MAX_VALUE
*/
public static long addCap(long a, long b) {
long u = a + b;
if (u < 0L) {
return Long.MAX_VALUE;
}
return u;
}
/**
* Multiplies two long values and caps the product at Long.MAX_VALUE.
* @param a the first value
* @param b the second value
* @return the product capped at Long.MAX_VALUE
*/
public static long multiplyCap(long a, long b) {
long u = a * b;
if (((a | b) >>> 31) != 0) {
if (u / a != b) {
return Long.MAX_VALUE;
}
}
return u;
}
/**
* Atomically adds the positive value n to the requested value in the AtomicLong and
* caps the result at Long.MAX_VALUE and returns the previous value.
* @param requested the AtomicLong holding the current requested value
* @param n the value to add, must be positive (not verified)
* @return the original value before the add
*/
public static long add(AtomicLong requested, long n) {
for (;;) {
long r = requested.get();
if (r == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long u = addCap(r, n);
if (requested.compareAndSet(r, u)) {
return r;
}
}
}
/**
* Atomically subtract the given number (positive, not validated) from the target field.
* @param requested the target field holding the current requested amount
* @param n the produced element count, positive (not validated)
* @return the new amount
*/
public static long produced(AtomicLong requested, long n) {
for (;;) {
long current = requested.get();
if (current == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long update = current - n;
if (update < 0L) {
RxJavaPlugins.onError(new IllegalStateException("More produced than requested: " + update));
update = 0L;
}
if (requested.compareAndSet(current, update)) {
return update;
}
}
}
}