From 5d2eeb932c4b68b05fd2d2bec9f2a0cefd1a86ba Mon Sep 17 00:00:00 2001 From: Yurun Date: Wed, 11 Oct 2023 17:33:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=8E=B7=E5=8F=96=E9=99=90?= =?UTF-8?q?=E6=B5=81=E6=A1=B6=E5=86=85=E5=8F=AF=E7=94=A8=E6=95=B0=E9=87=8F?= =?UTF-8?q?=20(#588)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新增获取限流桶内可用数量 * 修复 --- src/Components/rate-limit/src/RateLimiter.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Components/rate-limit/src/RateLimiter.php b/src/Components/rate-limit/src/RateLimiter.php index de374f97a9..892561c853 100644 --- a/src/Components/rate-limit/src/RateLimiter.php +++ b/src/Components/rate-limit/src/RateLimiter.php @@ -103,6 +103,25 @@ public static function limitBlock(string $name, int $capacity, ?callable $callba } } + /** + * 获取可用数量. + * + * @param string $name 限流器名称 + * @param int $capacity 总容量 + * @param int|null $fill 单位时间内生成填充的数量,不设置或为null时,默认值与 $capacity 相同 + * @param string $unit 单位时间,默认为:秒(second),支持:microsecond、millisecond、second、minute、hour、day、week、month、year + * @param string|null $poolName 连接池名称,留空取默认 redis 连接池 + */ + public static function getTokens(string $name, int $capacity, ?int $fill = null, string $unit = 'second', ?string $poolName = null): int + { + $storage = new ImiRedisStorage($name, RedisManager::getInstance($poolName)); + $rate = new Rate($fill ?? $capacity, $unit); + $bucket = new TokenBucket($capacity, $rate, $storage); + $bucket->bootstrap($capacity); + + return $bucket->getTokens(); + } + /** * 默认限流回调. *