easyswoole/atomic-limit
最新稳定版本:2.0.0
Composer 安装命令:
composer require easyswoole/atomic-limit
包简介
An efficient swoole framework
README 文档
README
Easyswoole提供了一个基于Atomic计数器的限流器。
原理
通过限制某一个时间周期内某个token的总请求数,从而实现基础限流。
安装
composer require easyswoole/atomic-limit
示例代码
以经典的暴力CC攻击防护为例子。我们可以限制一个ip-url的qps访问。
use EasySwoole\AtomicLimit\AtomicLimit; $limit = new AtomicLimit(); $http = new swoole_http_server("127.0.0.1", 9501); /** 为方便测试,限制设置为3 */ $limit->setLimitQps(3); $limit->attachServer($http); $http->on("request", function ($request, $response)use($http,$limit) { $ip = $http->getClientInfo($request->fd)['remote_ip']; $requestUri = $request->server['request_uri']; $token = $ip.$requestUri; /** access函数允许单独对某个token指定qps */ if($limit->access($token)){ $response->write('request accept'); }else{ $response->write('request refuse'); } $response->end(); }); $http->start();
统计信息
- 总下载量: 8.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2019-06-23