RateLimiter类及create方法(谷歌的Guava工具库)

文档地址 https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/util/concurrent/RateLimiter.html

Class RateLimiter

java.lang.Object
com.google.common.util.concurrent.RateLimiter


@Beta
@GwtIncompatible
public abstract class RateLimiter extends Object

速率限制器。从概念上讲,速率限制器以可配置的速率分配许可。每个 acquire() 函数都会根据需要阻塞,直到获得许可,然后获取许可。一旦获得许可,无需释放许可。
A rate limiter. Conceptually, a rate limiter distributes permits at a configurable rate. Each acquire() blocks if necessary until a permit is available, and then takes it. Once acquired, permits need not be released.

RateLimiter 可以安全地并发使用:它会限制所有线程调用的总速率。但请注意,它并不保证公平性。
RateLimiter is safe for concurrent use: It will restrict the total rate of calls from all threads. Note, however, that it does not guarantee fairness.

速率限制器通常用于限制某些物理或逻辑资源的访问速率。这与 Semaphore 不同,后者限制的是并发访问的数量,而不是速率(但请注意,并发性和速率密切相关,例如,参见利特尔定律)。
Rate limiters are often used to restrict the rate at which some physical or logical resource is accessed. This is in contrast to Semaphore which restricts the number of concurrent accesses instead of the rate (note though that concurrency and rate are closely related, e.g. see Little's Law).

RateLimiter 主要由许可的发放速率定义。如果没有其他配置,许可将以固定速率(以每秒许可数定义)分配。许可将平滑分配,并调整各个许可之间的延迟,以确保维持配置的速率。
A RateLimiter is defined primarily by the rate at which permits are issued. Absent additional configuration, permits will be distributed at a fixed rate, defined in terms of permits per second. Permits will be distributed smoothly, with the delay between individual permits being adjusted to ensure that the configured rate is maintained.

可以配置 RateLimiter 有一个预热期,在此期间每秒发出的许可证数量稳步增加,直到达到稳定速率。
It is possible to configure a RateLimiter to have a warmup period during which time the permits issued each second steadily increases until it hits the stable rate.

create(double permitsPerSecond)

public static RateLimiter create(double permitsPerSecond)

创建一个具有指定稳定吞吐量的 RateLimiter,以“每秒许可数”(通常称为 QPS,即每秒查询数)表示。
Creates a RateLimiter with the specified stable throughput, given as "permits per second" (commonly referred to as QPS, queries per second).

返回的 RateLimiter 确保在任何给定秒内平均发出的许可数不超过 permitsPerSecond 个,持续的请求将平滑地分布在每一秒内。当传入请求速率超过 permitsPerSecond 时,速率限制器将每 (1.0 / permitsPerSecond) 秒释放一个许可。当未使用速率限制器时,将允许最多 permitsPerSecond 个许可的突发请求,后续请求将以 permitsPerSecond 的稳定速率平滑限制。
The returned RateLimiter ensures that on average no more than permitsPerSecond are issued during any given second, with sustained requests being smoothly spread over each second. When the incoming request rate exceeds permitsPerSecond the rate limiter will release one permit every (1.0 / permitsPerSecond) seconds. When the rate limiter is unused, bursts of up to permitsPerSecond permits will be allowed, with subsequent requests being smoothly limited at the stable rate of permitsPerSecond.

参数:
permitsPerSecond - 返回的 RateLimiter 的速率,以每秒可用的许可数衡量
Parameters:
permitsPerSecond - the rate of the returned RateLimiter, measured in how many permits become available per second

Throws:
IllegalArgumentException - if permitsPerSecond is negative or zero

tryAcquire

public boolean tryAcquire(long timeout, TimeUnit unit)

如果可以在不超过指定超时时间的情况下获得许可,则从此 RateLimiter 获取许可;如果在超时时间到期之前无法授予许可,则立即返回 false(不等待)。
Acquires a permit from this RateLimiter if it can be obtained without exceeding the specified timeout, or returns false immediately (without waiting) if the permit would not have been granted before the timeout expired.

此方法等效于 tryAcquire(1, timeout, unit)。
This method is equivalent to tryAcquire(1, timeout, unit).

参数:

  • timeout - 等待许可的最长时间。负值被视为零。
  • unit - timeout 参数的时间单位
    Parameters:
  • timeout - the maximum time to wait for the permit. Negative values are treated as zero.
  • unit - the time unit of the timeout argument

返回:
如果已获取许可,则返回 true;否则,返回 false
Returns:
true if the permit was acquired, false otherwise

Throws:
IllegalArgumentException - if the requested number of permits is negative or zero

posted @ 2025-04-13 19:31  kuki'  阅读(157)  评论(0)    收藏  举报