Lines Matching defs:permits

36  * A rate limiter. Conceptually, a rate limiter distributes permits at a
38 * available, and then takes it. Once acquired, permits need not be released.
46 * <p>A {@code RateLimiter} is defined primarily by the rate at which permits
47 * are issued. Absent additional configuration, permits will be distributed at a
48 * fixed rate, defined in terms of permits per second. Permits will be distributed
49 * smoothly, with the delay between individual permits being adjusted to ensure
53 * period during which time the permits issued each second steadily increases until
59 * final RateLimiter rateLimiter = RateLimiter.create(2.0); // rate is "2 permits per second"
70 * a rate of 5000 permits per second:
72 * final RateLimiter rateLimiter = RateLimiter.create(5000.0); // rate = 5000 permits per second
79 * <p>It is important to note that the number of permits requested <i>never</i>
99 * "permits per second" (commonly referred to as <i>QPS</i>, queries per second).
106 * bursts of up to {@code permitsPerSecond} permits will be allowed, with subsequent
110 * how many permits become available per second
117 * The default RateLimiter configuration can save the unused permits of up to one second.
145 * "permits per second" (commonly referred to as <i>QPS</i>, queries per second), and a
160 * how many permits become available per second
236 * Returns the stable rate (as {@code permits per seconds}) with which this
264 * Acquires the given number of permits from this {@code RateLimiter}, blocking until the
267 * @param permits the number of permits to acquire
269 * @throws IllegalArgumentException if the requested number of permits is negative or zero
272 public double acquire(int permits) {
273 long microsToWait = reserve(permits);
279 * Reserves the given number of permits from this {@code RateLimiter} for future use, returning
284 final long reserve(int permits) {
285 checkPermits(permits);
287 return reserveAndGetWaitLength(permits, stopwatch.readMicros());
302 * @throws IllegalArgumentException if the requested number of permits is negative or zero
309 * Acquires permits from this {@link RateLimiter} if it can be acquired immediately without delay.
312 * This method is equivalent to {@code tryAcquire(permits, 0, anyUnit)}.
314 * @param permits the number of permits to acquire
315 * @return {@code true} if the permits were acquired, {@code false} otherwise
316 * @throws IllegalArgumentException if the requested number of permits is negative or zero
319 public boolean tryAcquire(int permits) {
320 return tryAcquire(permits, 0, MICROSECONDS);
338 * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained
340 * immediately (without waiting) if the permits would not have been granted
343 * @param permits the number of permits to acquire
344 * @param timeout the maximum time to wait for the permits. Negative values are treated as zero.
346 * @return {@code true} if the permits were acquired, {@code false} otherwise
347 * @throws IllegalArgumentException if the requested number of permits is negative or zero
349 public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
351 checkPermits(permits);
358 microsToWait = reserveAndGetWaitLength(permits, nowMicros);
374 final long reserveAndGetWaitLength(int permits, long nowMicros) {
375 long momentAvailable = reserveEarliestAvailable(permits, nowMicros);
380 * Returns the earliest time that permits are available (with one caveat).
382 * @return the time that permits are available, or, if permits are available immediately, an
388 * Reserves the requested number of permits and returns the time that those permits can be used
391 * @return the time that the permits may be used, or, if the permits may be used immediately, an
394 abstract long reserveEarliestAvailable(int permits, long nowMicros);
431 private static int checkPermits(int permits) {
432 checkArgument(permits > 0, "Requested permits (%s) must be positive", permits);
433 return permits;