Skip to content

Commit 5d2eeb9

Browse files
authored
支持获取限流桶内可用数量 (#588)
* 新增获取限流桶内可用数量 * 修复
1 parent 82787dc commit 5d2eeb9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/Components/rate-limit/src/RateLimiter.php

+19
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ public static function limitBlock(string $name, int $capacity, ?callable $callba
103103
}
104104
}
105105

106+
/**
107+
* 获取可用数量.
108+
*
109+
* @param string $name 限流器名称
110+
* @param int $capacity 总容量
111+
* @param int|null $fill 单位时间内生成填充的数量,不设置或为null时,默认值与 $capacity 相同
112+
* @param string $unit 单位时间,默认为:秒(second),支持:microsecond、millisecond、second、minute、hour、day、week、month、year
113+
* @param string|null $poolName 连接池名称,留空取默认 redis 连接池
114+
*/
115+
public static function getTokens(string $name, int $capacity, ?int $fill = null, string $unit = 'second', ?string $poolName = null): int
116+
{
117+
$storage = new ImiRedisStorage($name, RedisManager::getInstance($poolName));
118+
$rate = new Rate($fill ?? $capacity, $unit);
119+
$bucket = new TokenBucket($capacity, $rate, $storage);
120+
$bucket->bootstrap($capacity);
121+
122+
return $bucket->getTokens();
123+
}
124+
106125
/**
107126
* 默认限流回调.
108127
*

0 commit comments

Comments
 (0)