|
| 1 | +package javaslang.ratelimiter; |
| 2 | + |
| 3 | +import javaslang.control.Try; |
| 4 | + |
| 5 | +import java.time.Duration; |
| 6 | +import java.util.function.Consumer; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.function.Supplier; |
| 9 | + |
| 10 | +/** |
| 11 | + * A RateLimiter distributes permits at a configurable rate. {@link #getPermission} blocks if necessary |
| 12 | + * until a permit is available, and then takes it. Once acquired, permits need not be released. |
| 13 | + */ |
| 14 | +public interface RateLimiter { |
| 15 | + |
| 16 | + /** |
| 17 | + * Acquires a permission from this rate limiter, blocking until one is |
| 18 | + * available. |
| 19 | + * <p> |
| 20 | + * <p>If the current thread is {@linkplain Thread#interrupt interrupted} |
| 21 | + * while waiting for a permit then it won't throw {@linkplain InterruptedException}, |
| 22 | + * but its interrupt status will be set. |
| 23 | + * |
| 24 | + * @return {@code true} if a permit was acquired and {@code false} |
| 25 | + * if waiting time elapsed before a permit was acquired |
| 26 | + */ |
| 27 | + boolean getPermission(Duration timeoutDuration); |
| 28 | + |
| 29 | + /** |
| 30 | + * Get the name of this RateLimiter |
| 31 | + * |
| 32 | + * @return the name of this RateLimiter |
| 33 | + */ |
| 34 | + String getName(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the RateLimiterConfig of this RateLimiter. |
| 38 | + * |
| 39 | + * @return the RateLimiterConfig of this RateLimiter |
| 40 | + */ |
| 41 | + RateLimiterConfig getRateLimiterConfig(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the Metrics of this RateLimiter. |
| 45 | + * |
| 46 | + * @return the Metrics of this RateLimiter |
| 47 | + */ |
| 48 | + Metrics getMetrics(); |
| 49 | + |
| 50 | + interface Metrics { |
| 51 | + /** |
| 52 | + * Returns an estimate of the number of threads waiting for permission |
| 53 | + * in this JVM process. |
| 54 | + * |
| 55 | + * @return estimate of the number of threads waiting for permission. |
| 56 | + */ |
| 57 | + int getNumberOfWaitingThreads(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a supplier which is restricted by a RateLimiter. |
| 62 | + * |
| 63 | + * @param rateLimiter the RateLimiter |
| 64 | + * @param supplier the original supplier |
| 65 | + * @return a supplier which is restricted by a RateLimiter. |
| 66 | + */ |
| 67 | + static <T> Try.CheckedSupplier<T> decorateCheckedSupplier(RateLimiter rateLimiter, Try.CheckedSupplier<T> supplier) { |
| 68 | + Try.CheckedSupplier<T> decoratedSupplier = () -> { |
| 69 | + waitForPermission(rateLimiter); |
| 70 | + T result = supplier.get(); |
| 71 | + return result; |
| 72 | + }; |
| 73 | + return decoratedSupplier; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates a runnable which is restricted by a RateLimiter. |
| 78 | + * |
| 79 | + * @param rateLimiter the RateLimiter |
| 80 | + * @param runnable the original runnable |
| 81 | + * @return a runnable which is restricted by a RateLimiter. |
| 82 | + */ |
| 83 | + static Try.CheckedRunnable decorateCheckedRunnable(RateLimiter rateLimiter, Try.CheckedRunnable runnable) { |
| 84 | + |
| 85 | + Try.CheckedRunnable decoratedRunnable = () -> { |
| 86 | + waitForPermission(rateLimiter); |
| 87 | + runnable.run(); |
| 88 | + }; |
| 89 | + return decoratedRunnable; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Creates a function which is restricted by a RateLimiter. |
| 94 | + * |
| 95 | + * @param rateLimiter the RateLimiter |
| 96 | + * @param function the original function |
| 97 | + * @return a function which is restricted by a RateLimiter. |
| 98 | + */ |
| 99 | + static <T, R> Try.CheckedFunction<T, R> decorateCheckedFunction(RateLimiter rateLimiter, Try.CheckedFunction<T, R> function) { |
| 100 | + Try.CheckedFunction<T, R> decoratedFunction = (T t) -> { |
| 101 | + waitForPermission(rateLimiter); |
| 102 | + R result = function.apply(t); |
| 103 | + return result; |
| 104 | + }; |
| 105 | + return decoratedFunction; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a supplier which is restricted by a RateLimiter. |
| 110 | + * |
| 111 | + * @param rateLimiter the RateLimiter |
| 112 | + * @param supplier the original supplier |
| 113 | + * @return a supplier which is restricted by a RateLimiter. |
| 114 | + */ |
| 115 | + static <T> Supplier<T> decorateSupplier(RateLimiter rateLimiter, Supplier<T> supplier) { |
| 116 | + Supplier<T> decoratedSupplier = () -> { |
| 117 | + waitForPermission(rateLimiter); |
| 118 | + T result = supplier.get(); |
| 119 | + return result; |
| 120 | + }; |
| 121 | + return decoratedSupplier; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Creates a consumer which is restricted by a RateLimiter. |
| 126 | + * |
| 127 | + * @param rateLimiter the RateLimiter |
| 128 | + * @param consumer the original consumer |
| 129 | + * @return a consumer which is restricted by a RateLimiter. |
| 130 | + */ |
| 131 | + static <T> Consumer<T> decorateConsumer(RateLimiter rateLimiter, Consumer<T> consumer) { |
| 132 | + Consumer<T> decoratedConsumer = (T t) -> { |
| 133 | + waitForPermission(rateLimiter); |
| 134 | + consumer.accept(t); |
| 135 | + }; |
| 136 | + return decoratedConsumer; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Creates a runnable which is restricted by a RateLimiter. |
| 141 | + * |
| 142 | + * @param rateLimiter the RateLimiter |
| 143 | + * @param runnable the original runnable |
| 144 | + * @return a runnable which is restricted by a RateLimiter. |
| 145 | + */ |
| 146 | + static Runnable decorateRunnable(RateLimiter rateLimiter, Runnable runnable) { |
| 147 | + Runnable decoratedRunnable = () -> { |
| 148 | + waitForPermission(rateLimiter); |
| 149 | + runnable.run(); |
| 150 | + }; |
| 151 | + return decoratedRunnable; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Creates a function which is restricted by a RateLimiter. |
| 156 | + * |
| 157 | + * @param rateLimiter the RateLimiter |
| 158 | + * @param function the original function |
| 159 | + * @return a function which is restricted by a RateLimiter. |
| 160 | + */ |
| 161 | + static <T, R> Function<T, R> decorateFunction(RateLimiter rateLimiter, Function<T, R> function) { |
| 162 | + Function<T, R> decoratedFunction = (T t) -> { |
| 163 | + waitForPermission(rateLimiter); |
| 164 | + R result = function.apply(t); |
| 165 | + return result; |
| 166 | + }; |
| 167 | + return decoratedFunction; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Will wait for permission within default timeout duration. |
| 172 | + * Throws {@link RequestNotPermitted} if waiting time elapsed before a permit was acquired. |
| 173 | + * |
| 174 | + * @param rateLimiter the RateLimiter to get permission from |
| 175 | + */ |
| 176 | + static void waitForPermission(final RateLimiter rateLimiter) { |
| 177 | + RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig(); |
| 178 | + Duration timeoutDuration = rateLimiterConfig.getTimeoutDuration(); |
| 179 | + boolean permission = rateLimiter.getPermission(timeoutDuration); |
| 180 | + if (!permission) { |
| 181 | + throw new RequestNotPermitted("Request not permitted for limiter: " + rateLimiter.getName()); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments