Skip to content

Commit

Permalink
支持获取限流桶内可用数量 (#588)
Browse files Browse the repository at this point in the history
* 新增获取限流桶内可用数量

* 修复
  • Loading branch information
Yurunsoft committed Oct 11, 2023
1 parent 82787dc commit 5d2eeb9
Showing 1 changed file with 19 additions and 0 deletions.
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

0 comments on commit 5d2eeb9

Please sign in to comment.