001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019import static com.google.common.util.concurrent.Internal.toNanosSaturated; 020import static java.lang.Math.max; 021import static java.util.concurrent.TimeUnit.MICROSECONDS; 022import static java.util.concurrent.TimeUnit.SECONDS; 023 024import com.google.common.annotations.Beta; 025import com.google.common.annotations.GwtIncompatible; 026import com.google.common.annotations.J2ktIncompatible; 027import com.google.common.annotations.VisibleForTesting; 028import com.google.common.base.Stopwatch; 029import com.google.common.util.concurrent.SmoothRateLimiter.SmoothBursty; 030import com.google.common.util.concurrent.SmoothRateLimiter.SmoothWarmingUp; 031import com.google.errorprone.annotations.CanIgnoreReturnValue; 032import java.time.Duration; 033import java.util.Locale; 034import java.util.concurrent.TimeUnit; 035import org.jspecify.annotations.Nullable; 036 037/** 038 * A rate limiter. Conceptually, a rate limiter distributes permits at a configurable rate. Each 039 * {@link #acquire()} blocks if necessary until a permit is available, and then takes it. Once 040 * acquired, permits need not be released. 041 * 042 * <p>{@code RateLimiter} is safe for concurrent use: It will restrict the total rate of calls from 043 * all threads. Note, however, that it does not guarantee fairness. 044 * 045 * <p>Rate limiters are often used to restrict the rate at which some physical or logical resource 046 * is accessed. This is in contrast to {@link java.util.concurrent.Semaphore} which restricts the 047 * number of concurrent accesses instead of the rate (note though that concurrency and rate are 048 * closely related, e.g. see <a href="http://en.wikipedia.org/wiki/Little%27s_law">Little's 049 * Law</a>). 050 * 051 * <p>A {@code RateLimiter} is defined primarily by the rate at which permits are issued. Absent 052 * additional configuration, permits will be distributed at a fixed rate, defined in terms of 053 * permits per second. Permits will be distributed smoothly, with the delay between individual 054 * permits being adjusted to ensure that the configured rate is maintained. 055 * 056 * <p>It is possible to configure a {@code RateLimiter} to have a warmup period during which time 057 * the permits issued each second steadily increases until it hits the stable rate. 058 * 059 * <p>As an example, imagine that we have a list of tasks to execute, but we don't want to submit 060 * more than 2 per second: 061 * 062 * <pre>{@code 063 * final RateLimiter rateLimiter = RateLimiter.create(2.0); // rate is "2 permits per second" 064 * void submitTasks(List<Runnable> tasks, Executor executor) { 065 * for (Runnable task : tasks) { 066 * rateLimiter.acquire(); // may wait 067 * executor.execute(task); 068 * } 069 * } 070 * }</pre> 071 * 072 * <p>As another example, imagine that we produce a stream of data, and we want to cap it at 5kb per 073 * second. This could be accomplished by requiring a permit per byte, and specifying a rate of 5000 074 * permits per second: 075 * 076 * <pre>{@code 077 * final RateLimiter rateLimiter = RateLimiter.create(5000.0); // rate = 5000 permits per second 078 * void submitPacket(byte[] packet) { 079 * rateLimiter.acquire(packet.length); 080 * networkService.send(packet); 081 * } 082 * }</pre> 083 * 084 * <p>It is important to note that the number of permits requested <i>never</i> affects the 085 * throttling of the request itself (an invocation to {@code acquire(1)} and an invocation to {@code 086 * acquire(1000)} will result in exactly the same throttling, if any), but it affects the throttling 087 * of the <i>next</i> request. I.e., if an expensive task arrives at an idle RateLimiter, it will be 088 * granted immediately, but it is the <i>next</i> request that will experience extra throttling, 089 * thus paying for the cost of the expensive task. 090 * 091 * @author Dimitris Andreou 092 * @since 13.0 093 */ 094// TODO(user): switch to nano precision. A natural unit of cost is "bytes", and a micro precision 095// would mean a maximum rate of "1MB/s", which might be small in some cases. 096@Beta 097@J2ktIncompatible 098@GwtIncompatible 099public abstract class RateLimiter { 100 /** 101 * Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per 102 * second" (commonly referred to as <i>QPS</i>, queries per second). 103 * 104 * <p>The returned {@code RateLimiter} ensures that on average no more than {@code 105 * permitsPerSecond} are issued during any given second, with sustained requests being smoothly 106 * spread over each second. When the incoming request rate exceeds {@code permitsPerSecond} the 107 * rate limiter will release one permit every {@code (1.0 / permitsPerSecond)} seconds. When the 108 * rate limiter is unused, bursts of up to {@code permitsPerSecond} permits will be allowed, with 109 * subsequent requests being smoothly limited at the stable rate of {@code permitsPerSecond}. 110 * 111 * @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many 112 * permits become available per second 113 * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero 114 */ 115 // TODO(user): "This is equivalent to 116 // {@code createWithCapacity(permitsPerSecond, 1, TimeUnit.SECONDS)}". 117 public static RateLimiter create(double permitsPerSecond) { 118 /* 119 * The default RateLimiter configuration can save the unused permits of up to one second. This 120 * is to avoid unnecessary stalls in situations like this: A RateLimiter of 1qps, and 4 threads, 121 * all calling acquire() at these moments: 122 * 123 * T0 at 0 seconds 124 * T1 at 1.05 seconds 125 * T2 at 2 seconds 126 * T3 at 3 seconds 127 * 128 * Due to the slight delay of T1, T2 would have to sleep till 2.05 seconds, and T3 would also 129 * have to sleep till 3.05 seconds. 130 */ 131 return create(permitsPerSecond, SleepingStopwatch.createFromSystemTimer()); 132 } 133 134 @VisibleForTesting 135 static RateLimiter create(double permitsPerSecond, SleepingStopwatch stopwatch) { 136 RateLimiter rateLimiter = new SmoothBursty(stopwatch, 1.0 /* maxBurstSeconds */); 137 rateLimiter.setRate(permitsPerSecond); 138 return rateLimiter; 139 } 140 141 /** 142 * Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per 143 * second" (commonly referred to as <i>QPS</i>, queries per second), and a <i>warmup period</i>, 144 * during which the {@code RateLimiter} smoothly ramps up its rate, until it reaches its maximum 145 * rate at the end of the period (as long as there are enough requests to saturate it). Similarly, 146 * if the {@code RateLimiter} is left <i>unused</i> for a duration of {@code warmupPeriod}, it 147 * will gradually return to its "cold" state, i.e. it will go through the same warming up process 148 * as when it was first created. 149 * 150 * <p>The returned {@code RateLimiter} is intended for cases where the resource that actually 151 * fulfills the requests (e.g., a remote server) needs "warmup" time, rather than being 152 * immediately accessed at the stable (maximum) rate. 153 * 154 * <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period will 155 * follow), and if it is left unused for long enough, it will return to that state. 156 * 157 * @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many 158 * permits become available per second 159 * @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its rate, 160 * before reaching its stable (maximum) rate 161 * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero or {@code 162 * warmupPeriod} is negative 163 * @since 28.0 (but only since 33.4.0 in the Android flavor) 164 */ 165 public static RateLimiter create(double permitsPerSecond, Duration warmupPeriod) { 166 return create(permitsPerSecond, toNanosSaturated(warmupPeriod), TimeUnit.NANOSECONDS); 167 } 168 169 /** 170 * Creates a {@code RateLimiter} with the specified stable throughput, given as "permits per 171 * second" (commonly referred to as <i>QPS</i>, queries per second), and a <i>warmup period</i>, 172 * during which the {@code RateLimiter} smoothly ramps up its rate, until it reaches its maximum 173 * rate at the end of the period (as long as there are enough requests to saturate it). Similarly, 174 * if the {@code RateLimiter} is left <i>unused</i> for a duration of {@code warmupPeriod}, it 175 * will gradually return to its "cold" state, i.e. it will go through the same warming up process 176 * as when it was first created. 177 * 178 * <p>The returned {@code RateLimiter} is intended for cases where the resource that actually 179 * fulfills the requests (e.g., a remote server) needs "warmup" time, rather than being 180 * immediately accessed at the stable (maximum) rate. 181 * 182 * <p>The returned {@code RateLimiter} starts in a "cold" state (i.e. the warmup period will 183 * follow), and if it is left unused for long enough, it will return to that state. 184 * 185 * @param permitsPerSecond the rate of the returned {@code RateLimiter}, measured in how many 186 * permits become available per second 187 * @param warmupPeriod the duration of the period where the {@code RateLimiter} ramps up its rate, 188 * before reaching its stable (maximum) rate 189 * @param unit the time unit of the warmupPeriod argument 190 * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero or {@code 191 * warmupPeriod} is negative 192 */ 193 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 194 public static RateLimiter create(double permitsPerSecond, long warmupPeriod, TimeUnit unit) { 195 checkArgument(warmupPeriod >= 0, "warmupPeriod must not be negative: %s", warmupPeriod); 196 return create( 197 permitsPerSecond, warmupPeriod, unit, 3.0, SleepingStopwatch.createFromSystemTimer()); 198 } 199 200 @VisibleForTesting 201 static RateLimiter create( 202 double permitsPerSecond, 203 long warmupPeriod, 204 TimeUnit unit, 205 double coldFactor, 206 SleepingStopwatch stopwatch) { 207 RateLimiter rateLimiter = new SmoothWarmingUp(stopwatch, warmupPeriod, unit, coldFactor); 208 rateLimiter.setRate(permitsPerSecond); 209 return rateLimiter; 210 } 211 212 /** 213 * The underlying timer; used both to measure elapsed time and sleep as necessary. A separate 214 * object to facilitate testing. 215 */ 216 private final SleepingStopwatch stopwatch; 217 218 // Can't be initialized in the constructor because mocks don't call the constructor. 219 private volatile @Nullable Object mutexDoNotUseDirectly; 220 221 private Object mutex() { 222 Object mutex = mutexDoNotUseDirectly; 223 if (mutex == null) { 224 synchronized (this) { 225 mutex = mutexDoNotUseDirectly; 226 if (mutex == null) { 227 mutexDoNotUseDirectly = mutex = new Object(); 228 } 229 } 230 } 231 return mutex; 232 } 233 234 RateLimiter(SleepingStopwatch stopwatch) { 235 this.stopwatch = checkNotNull(stopwatch); 236 } 237 238 /** 239 * Updates the stable rate of this {@code RateLimiter}, that is, the {@code permitsPerSecond} 240 * argument provided in the factory method that constructed the {@code RateLimiter}. Currently 241 * throttled threads will <b>not</b> be awakened as a result of this invocation, thus they do not 242 * observe the new rate; only subsequent requests will. 243 * 244 * <p>Note though that, since each request repays (by waiting, if necessary) the cost of the 245 * <i>previous</i> request, this means that the very next request after an invocation to {@code 246 * setRate} will not be affected by the new rate; it will pay the cost of the previous request, 247 * which is in terms of the previous rate. 248 * 249 * <p>The behavior of the {@code RateLimiter} is not modified in any other way, e.g. if the {@code 250 * RateLimiter} was configured with a warmup period of 20 seconds, it still has a warmup period of 251 * 20 seconds after this method invocation. 252 * 253 * @param permitsPerSecond the new stable rate of this {@code RateLimiter} 254 * @throws IllegalArgumentException if {@code permitsPerSecond} is negative or zero 255 */ 256 public final void setRate(double permitsPerSecond) { 257 checkArgument(permitsPerSecond > 0.0, "rate must be positive"); 258 synchronized (mutex()) { 259 doSetRate(permitsPerSecond, stopwatch.readMicros()); 260 } 261 } 262 263 abstract void doSetRate(double permitsPerSecond, long nowMicros); 264 265 /** 266 * Returns the stable rate (as {@code permits per seconds}) with which this {@code RateLimiter} is 267 * configured with. The initial value of this is the same as the {@code permitsPerSecond} argument 268 * passed in the factory method that produced this {@code RateLimiter}, and it is only updated 269 * after invocations to {@linkplain #setRate}. 270 */ 271 public final double getRate() { 272 synchronized (mutex()) { 273 return doGetRate(); 274 } 275 } 276 277 abstract double doGetRate(); 278 279 /** 280 * Acquires a single permit from this {@code RateLimiter}, blocking until the request can be 281 * granted. Tells the amount of time slept, if any. 282 * 283 * <p>This method is equivalent to {@code acquire(1)}. 284 * 285 * @return time spent sleeping to enforce rate, in seconds; 0.0 if not rate-limited 286 * @since 16.0 (present in 13.0 with {@code void} return type}) 287 */ 288 @CanIgnoreReturnValue 289 public double acquire() { 290 return acquire(1); 291 } 292 293 /** 294 * Acquires the given number of permits from this {@code RateLimiter}, blocking until the request 295 * can be granted. Tells the amount of time slept, if any. 296 * 297 * @param permits the number of permits to acquire 298 * @return time spent sleeping to enforce rate, in seconds; 0.0 if not rate-limited 299 * @throws IllegalArgumentException if the requested number of permits is negative or zero 300 * @since 16.0 (present in 13.0 with {@code void} return type}) 301 */ 302 @CanIgnoreReturnValue 303 public double acquire(int permits) { 304 long microsToWait = reserve(permits); 305 stopwatch.sleepMicrosUninterruptibly(microsToWait); 306 return 1.0 * microsToWait / SECONDS.toMicros(1L); 307 } 308 309 /** 310 * Reserves the given number of permits from this {@code RateLimiter} for future use, returning 311 * the number of microseconds until the reservation can be consumed. 312 * 313 * @return time in microseconds to wait until the resource can be acquired, never negative 314 */ 315 final long reserve(int permits) { 316 checkPermits(permits); 317 synchronized (mutex()) { 318 return reserveAndGetWaitLength(permits, stopwatch.readMicros()); 319 } 320 } 321 322 /** 323 * Acquires a permit from this {@code RateLimiter} if it can be obtained without exceeding the 324 * specified {@code timeout}, or returns {@code false} immediately (without waiting) if the permit 325 * would not have been granted before the timeout expired. 326 * 327 * <p>This method is equivalent to {@code tryAcquire(1, timeout)}. 328 * 329 * @param timeout the maximum time to wait for the permit. Negative values are treated as zero. 330 * @return {@code true} if the permit was acquired, {@code false} otherwise 331 * @throws IllegalArgumentException if the requested number of permits is negative or zero 332 * @since 28.0 (but only since 33.4.0 in the Android flavor) 333 */ 334 public boolean tryAcquire(Duration timeout) { 335 return tryAcquire(1, toNanosSaturated(timeout), TimeUnit.NANOSECONDS); 336 } 337 338 /** 339 * Acquires a permit from this {@code RateLimiter} if it can be obtained without exceeding the 340 * specified {@code timeout}, or returns {@code false} immediately (without waiting) if the permit 341 * would not have been granted before the timeout expired. 342 * 343 * <p>This method is equivalent to {@code tryAcquire(1, timeout, unit)}. 344 * 345 * @param timeout the maximum time to wait for the permit. Negative values are treated as zero. 346 * @param unit the time unit of the timeout argument 347 * @return {@code true} if the permit was acquired, {@code false} otherwise 348 * @throws IllegalArgumentException if the requested number of permits is negative or zero 349 */ 350 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 351 public boolean tryAcquire(long timeout, TimeUnit unit) { 352 return tryAcquire(1, timeout, unit); 353 } 354 355 /** 356 * Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay. 357 * 358 * <p>This method is equivalent to {@code tryAcquire(permits, 0, anyUnit)}. 359 * 360 * @param permits the number of permits to acquire 361 * @return {@code true} if the permits were acquired, {@code false} otherwise 362 * @throws IllegalArgumentException if the requested number of permits is negative or zero 363 * @since 14.0 364 */ 365 public boolean tryAcquire(int permits) { 366 return tryAcquire(permits, 0, MICROSECONDS); 367 } 368 369 /** 370 * Acquires a permit from this {@link RateLimiter} if it can be acquired immediately without 371 * delay. 372 * 373 * <p>This method is equivalent to {@code tryAcquire(1)}. 374 * 375 * @return {@code true} if the permit was acquired, {@code false} otherwise 376 * @since 14.0 377 */ 378 public boolean tryAcquire() { 379 return tryAcquire(1, 0, MICROSECONDS); 380 } 381 382 /** 383 * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained 384 * without exceeding the specified {@code timeout}, or returns {@code false} immediately (without 385 * waiting) if the permits would not have been granted before the timeout expired. 386 * 387 * @param permits the number of permits to acquire 388 * @param timeout the maximum time to wait for the permits. Negative values are treated as zero. 389 * @return {@code true} if the permits were acquired, {@code false} otherwise 390 * @throws IllegalArgumentException if the requested number of permits is negative or zero 391 * @since 28.0 (but only since 33.4.0 in the Android flavor) 392 */ 393 public boolean tryAcquire(int permits, Duration timeout) { 394 return tryAcquire(permits, toNanosSaturated(timeout), TimeUnit.NANOSECONDS); 395 } 396 397 /** 398 * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained 399 * without exceeding the specified {@code timeout}, or returns {@code false} immediately (without 400 * waiting) if the permits would not have been granted before the timeout expired. 401 * 402 * @param permits the number of permits to acquire 403 * @param timeout the maximum time to wait for the permits. Negative values are treated as zero. 404 * @param unit the time unit of the timeout argument 405 * @return {@code true} if the permits were acquired, {@code false} otherwise 406 * @throws IllegalArgumentException if the requested number of permits is negative or zero 407 */ 408 @SuppressWarnings("GoodTime") // should accept a java.time.Duration 409 public boolean tryAcquire(int permits, long timeout, TimeUnit unit) { 410 long timeoutMicros = max(unit.toMicros(timeout), 0); 411 checkPermits(permits); 412 long microsToWait; 413 synchronized (mutex()) { 414 long nowMicros = stopwatch.readMicros(); 415 if (!canAcquire(nowMicros, timeoutMicros)) { 416 return false; 417 } else { 418 microsToWait = reserveAndGetWaitLength(permits, nowMicros); 419 } 420 } 421 stopwatch.sleepMicrosUninterruptibly(microsToWait); 422 return true; 423 } 424 425 private boolean canAcquire(long nowMicros, long timeoutMicros) { 426 return queryEarliestAvailable(nowMicros) - timeoutMicros <= nowMicros; 427 } 428 429 /** 430 * Reserves next ticket and returns the wait time that the caller must wait for. 431 * 432 * @return the required wait time, never negative 433 */ 434 final long reserveAndGetWaitLength(int permits, long nowMicros) { 435 long momentAvailable = reserveEarliestAvailable(permits, nowMicros); 436 return max(momentAvailable - nowMicros, 0); 437 } 438 439 /** 440 * Returns the earliest time that permits are available (with one caveat). 441 * 442 * @return the time that permits are available, or, if permits are available immediately, an 443 * arbitrary past or present time 444 */ 445 abstract long queryEarliestAvailable(long nowMicros); 446 447 /** 448 * Reserves the requested number of permits and returns the time that those permits can be used 449 * (with one caveat). 450 * 451 * @return the time that the permits may be used, or, if the permits may be used immediately, an 452 * arbitrary past or present time 453 */ 454 abstract long reserveEarliestAvailable(int permits, long nowMicros); 455 456 @Override 457 public String toString() { 458 return String.format(Locale.ROOT, "RateLimiter[stableRate=%3.1fqps]", getRate()); 459 } 460 461 abstract static class SleepingStopwatch { 462 /** Constructor for use by subclasses. */ 463 protected SleepingStopwatch() {} 464 465 /* 466 * We always hold the mutex when calling this. TODO(cpovirk): Is that important? Perhaps we need 467 * to guarantee that each call to reserveEarliestAvailable, etc. sees a value >= the previous? 468 * Also, is it OK that we don't hold the mutex when sleeping? 469 */ 470 protected abstract long readMicros(); 471 472 protected abstract void sleepMicrosUninterruptibly(long micros); 473 474 public static SleepingStopwatch createFromSystemTimer() { 475 return new SleepingStopwatch() { 476 final Stopwatch stopwatch = Stopwatch.createStarted(); 477 478 @Override 479 protected long readMicros() { 480 return stopwatch.elapsed(MICROSECONDS); 481 } 482 483 @Override 484 protected void sleepMicrosUninterruptibly(long micros) { 485 if (micros > 0) { 486 Uninterruptibles.sleepUninterruptibly(micros, MICROSECONDS); 487 } 488 } 489 }; 490 } 491 } 492 493 private static void checkPermits(int permits) { 494 checkArgument(permits > 0, "Requested permits (%s) must be positive", permits); 495 } 496}