PHPackages                             lingyihang/hyperf-throttle-requests - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. lingyihang/hyperf-throttle-requests

ActiveLibrary[Framework](/categories/framework)

lingyihang/hyperf-throttle-requests
===================================

A rate limiter implemented for Hyperf framework.

02071PHP

Since Jul 2Pushed 1y agoCompare

[ Source](https://github.com/lingyihang/hyperf-throttle-requests)[ Packagist](https://packagist.org/packages/lingyihang/hyperf-throttle-requests)[ RSS](/packages/lingyihang-hyperf-throttle-requests/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

hyperf-throttle-requests
========================

[](#hyperf-throttle-requests)

> 适配 [hyperf](https://hyperf.wiki/) 框架的请求频率限流器。功能类似于 [laravel](https://laravel.com/) 框架的 [throttle](https://laravel.com/docs/7.x/middleware) 中间件。

运行环境
----

[](#运行环境)

- php &gt;= 8.1
- composer
- hyperf ~3.1.0

安装
--

[](#安装)

```
composer require pudongping/hyperf-throttle-requests -vvv
```

配置
--

[](#配置)

### 发布配置

[](#发布配置)

在你自己的项目根目录下，执行以下命令

```
php bin/hyperf.php vendor:publish pudongping/hyperf-throttle-requests
```

### 配置说明

[](#配置说明)

配置默认值说明storagePudongping\\HyperfThrottleRequests\\Storage\\RedisStorage::class数据存储驱动maxAttempts60在指定时间内允许的最大请求次数decaySeconds60单位时间（单位：s）prefix''计数器 key 前缀，不填时，默认为：`throttle:`key''具体的计数器的 key （一般只有在某些特定场景下才会使用，比如假设访问多个不同的路由时，均累加一个计数器）generateKeyCallable\[\]生成计数器 key 的方法（默认以当前请求路径加当前客户端 IP 地址作为 key）tooManyAttemptsCallback\[\]当触发到最大请求次数时的回调方法（默认会抛出 `Pudongping\HyperfThrottleRequests\Exception\ThrottleRequestsException` 异常）使用
--

[](#使用)

该组件提供了以下 3 种调用方式：

### 第一种：使用注解 `Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests`

[](#第一种使用注解-pudongpinghyperfthrottlerequestsannotationthrottlerequests)

该组件提供 `Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests` 注解，作用于类、类方法。
配置作用优先级为：类方法上的注解配置 &gt; 类上的注解配置 &gt; `config/autoload/hyperf-throttle-requests.php` &gt; 注解默认配置

> 注意：只有使用注解调用时，才会使用 `config/autoload/hyperf-throttle-requests.php` 配置文件中的配置项。

#### 使用注解 `Pudongping\HyperfThrottleRequests\Annotation\ThrottleRequests` 作用于类上，示例：

[](#使用注解-pudongpinghyperfthrottlerequestsannotationthrottlerequests-作用于类上示例)

```
