# think-throttle **Repository Path**: willrook/think-throttle ## Basic Information - **Project Name**: think-throttle - **Description**: tp5.0接口限流 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 3 - **Created**: 2023-04-29 - **Last Updated**: 2025-08-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: tp5, Composer, 接口限流, throttle ## README ### 介绍 ``` 用于tp5.0的接口限流工具类 ``` ### 作用 可限定用户在一段时间内的访问次数,可用于保护接口防爬防爆破的目的。 ### 使用 例如: ``` $rate = new Ratelimit([]); $rate->exec(); ``` ### 安装 ``` composer require creatcode/think-throttle ``` 安装后会自动为项目生成 `extra/throttle.php` 配置文件 ### 配置说明 在 `extra/throttle.php` 配置选项: ``` 'throttle_', // 缓存的键,true 表示使用来源ip 'key' => true, // 要被限制的请求类型, eg: GET POST PUT DELETE HEAD 'visit_method' => ['GET'], // 设置访问频率,例如 '10/m' 指的是允许每分钟请求10次。值 null 表示不限制, eg: null 10/m 20/h 300/d 200/300 'visit_rate' => '100/m', // 访问受限时返回的响应 'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds) { return Response::create('Too many requests, try again after ' . $wait_seconds . ' seconds.')->code(429); }, ]; ``` 当配置项满足以下条件任何一个时,不会限制访问频率: 1. `key` 值为 `false` 或 `null`; 2. `visit_rate` 值为 `null`。 其中 `key` 用来设置缓存键的;而 `visit_rate` 用来设置访问频率,单位可以是秒,分,时,天,例如:`1/s`, `10/m`, `98/h`, `100/d` , 也可以是 `100/600` (600 秒内最多 100 次请求)。 ### 灵活定制 示例一:针对用户个体做限制, `key` 的值可以设为函数,该函数返回新的缓存键值(需要Session支持),例如: ``` 'key' => function($throttle, $request) { $user_id = $request->session->get('user_id'); return $user_id ; }, ``` 示例二:也可以在回调函数里针对不同控制器和方法定制生成key,会自动进行转换: ``` 'key' => function($throttle, $request) { return '__CONTROLLER__/__ACTION__/__IP__'; }, 或者直接设置: 'key' => '__CONTROLLER__/__ACTION__/__IP__', ``` 示例三:允许在闭包内修改本次访问频率或临时更换限流策略: ``` 'key' => function($throttle, $request) { $throttle->setRate('5/m'); // 设置频率 $throttle->setDriverClass(CounterSlider::class);// 设置限流策略 return true; }, ```