|
| 1 | +/** |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 | + * the License for the specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package io.reactivesocket.internal; |
| 14 | + |
| 15 | +import java.util.concurrent.atomic.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Utility class to help with backpressure-related operations such as request aggregation. |
| 19 | + */ |
| 20 | +public enum BackpressureHelper { |
| 21 | + ; |
| 22 | + /** |
| 23 | + * Adds two long values and caps the sum at Long.MAX_VALUE. |
| 24 | + * @param a the first value |
| 25 | + * @param b the second value |
| 26 | + * @return the sum capped at Long.MAX_VALUE |
| 27 | + */ |
| 28 | + public static long addCap(long a, long b) { |
| 29 | + long u = a + b; |
| 30 | + if (u < 0L) { |
| 31 | + return Long.MAX_VALUE; |
| 32 | + } |
| 33 | + return u; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Multiplies two long values and caps the product at Long.MAX_VALUE. |
| 38 | + * @param a the first value |
| 39 | + * @param b the second value |
| 40 | + * @return the product capped at Long.MAX_VALUE |
| 41 | + */ |
| 42 | + public static long multiplyCap(long a, long b) { |
| 43 | + long u = a * b; |
| 44 | + if (((a | b) >>> 31) != 0) { |
| 45 | + if (u / a != b) { |
| 46 | + return Long.MAX_VALUE; |
| 47 | + } |
| 48 | + } |
| 49 | + return u; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Atomically adds the positive value n to the requested value in the AtomicLong and |
| 54 | + * caps the result at Long.MAX_VALUE and returns the previous value. |
| 55 | + * @param requested the AtomicLong holding the current requested value |
| 56 | + * @param n the value to add, must be positive (not verified) |
| 57 | + * @return the original value before the add |
| 58 | + */ |
| 59 | + public static long add(AtomicLong requested, long n) { |
| 60 | + for (;;) { |
| 61 | + long r = requested.get(); |
| 62 | + if (r == Long.MAX_VALUE) { |
| 63 | + return Long.MAX_VALUE; |
| 64 | + } |
| 65 | + long u = addCap(r, n); |
| 66 | + if (requested.compareAndSet(r, u)) { |
| 67 | + return r; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Atomically adds the positive value n to the value in the instance through the field updater and |
| 74 | + * caps the result at Long.MAX_VALUE and returns the previous value. |
| 75 | + * @param updater the field updater for the requested value |
| 76 | + * @param instance the instance holding the requested value |
| 77 | + * @param n the value to add, must be positive (not verified) |
| 78 | + * @return the original value before the add |
| 79 | + */ |
| 80 | + public static <T> long add(AtomicLongFieldUpdater<T> updater, T instance, long n) { |
| 81 | + for (;;) { |
| 82 | + long r = updater.get(instance); |
| 83 | + if (r == Long.MAX_VALUE) { |
| 84 | + return Long.MAX_VALUE; |
| 85 | + } |
| 86 | + long u = addCap(r, n); |
| 87 | + if (updater.compareAndSet(instance, r, u)) { |
| 88 | + return r; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments