Skip to content

Commit

Permalink
rate-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Nov 2, 2023
1 parent c9a0621 commit 99ed42b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
20 changes: 6 additions & 14 deletions src/Components/rate-limit/src/Annotation/BlockingConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@
* 阻塞等待消费.
*
* 当触发限流时,自动阻塞(协程挂起)等待
*
* @Annotation
*
* @Target("METHOD")
*
* @property int|null $timeout 超时时间,单位:秒;为 null 不限制
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
class BlockingConsumer extends Base
{
/**
* {@inheritDoc}
*/
protected ?string $defaultFieldName = 'timeout';

public function __construct(?array $__data = null, ?int $timeout = null)
{
parent::__construct(...\func_get_args());
public function __construct(
/**
* 超时时间,单位:秒;为 null 不限制.
*/
public ?int $timeout = null
) {
}
}
47 changes: 32 additions & 15 deletions src/Components/rate-limit/src/Annotation/RateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,41 @@

/**
* 限流器注解.
*
* @Annotation
*
* @Target("METHOD")
*
* @property string $name 限流器名称
* @property int $capacity 总容量
* @property int|null $fill 单位时间内生成填充的数量;不设置或为null时,默认值与 $capacity 相同
* @property string $unit 单位时间,默认为:秒(second);支持:microsecond、millisecond、second、minute、hour、day、week、month、year
* @property int $deduct 每次扣除数量,默认为1
* @property callable|null $callback 触发限流的回调
* @property string|null $poolName 连接池名称,留空取默认 redis 连接池
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
class RateLimit extends Base
{
public function __construct(?array $__data = null, string $name = '', int $capacity = 0, ?int $fill = null, string $unit = 'second', int $deduct = 1, ?callable $callback = null, ?string $poolName = null)
{
parent::__construct(...\func_get_args());
public function __construct(
/**
* 限流器名称.
*/
public string $name = '',
/**
* 总容量.
*/
public int $capacity = 0,
/**
* 单位时间内生成填充的数量;不设置或为null时,默认值与 $capacity 相同.
*/
public ?int $fill = null,
/**
* 单位时间,默认为:秒(second);支持:microsecond、millisecond、second、minute、hour、day、week、month、year.
*/
public string $unit = 'second',
/**
* 每次扣除数量,默认为1.
*/
public int $deduct = 1,
/**
* 触发限流的回调.
*
* @var ?callable
*/
public $callback = null,
/**
* 连接池名称,留空取默认 redis 连接池.
*/
public ?string $poolName = null
) {
}
}
37 changes: 24 additions & 13 deletions src/Components/rate-limit/src/Annotation/WorkerLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@

/**
* 并发工作数限制注解.
*
* @Annotation
*
* @Target("METHOD")
*
* @property string $name 限流器名称
* @property int $max 最大同时工作数量
* @property float|null $timeout 工作超时时间,单位:秒,支持小数点精确到毫秒;默认为null,则不限制(不推荐不安全,有隐患)
* @property callable|null $callback 触发限流的回调
* @property string|null $poolName 连接池名称,留空取默认 redis 连接池
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
class WorkerLimit extends Base
{
public function __construct(?array $__data = null, string $name = '', int $max = 0, ?float $timeout = null, ?callable $callback = null, ?string $poolName = null)
{
parent::__construct(...\func_get_args());
public function __construct(
/**
* 限流器名称.
*/
public string $name = '',
/**
* 最大同时工作数量.
*/
public int $max = 0,
/**
* 工作超时时间,单位:秒,支持小数点精确到毫秒;默认为null,则不限制(不推荐不安全,有隐患).
*/
public ?float $timeout = null,
/**
* 触发限流的回调.
*
* @var ?callable
*/
public $callback = null,
/**
* 连接池名称,留空取默认 redis 连接池.
*/
public ?string $poolName = null
) {
}
}

0 comments on commit 99ed42b

Please sign in to comment.