Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持获取限流桶内可用数量 #588

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Components/rate-limit/src/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
* 默认限流回调.
*
Expand Down