亚洲精品成人_精品成人一区_999视频在线播放_免费黄色在线_亚洲成人久久久_久久www免费视频

AtomicLimit

EasySwoole 提供了一個基于 Atomic 計數(shù)器的限流器。

原理

通過限制某一個時間周期內(nèi)的總請求數(shù),從而實現(xiàn)基礎(chǔ)限流。舉個例子,設(shè)置5秒內(nèi),允許的最大請求量為200,那么理論平均并發(fā)為40,峰值并發(fā)為200。

組件要求

  • php: >= 7.1.0
  • easyswoole/component: ^2.0

安裝方法

composer require easyswoole/atomic-limit

倉庫地址

easy-swoole/atomic-limit

在 EasySwoole 中使用

首先在 EasySwoole 全局的 mainServerCreate 事件(即項目根目錄的 EasySwooleEvent.phpmainServerCreate 函數(shù)) 中,進行限流器注冊

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace EasySwoole\EasySwoole;

use EasySwoole\AtomicLimit\AtomicLimit;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        ###### 配置限流器 ######
        $limit = new AtomicLimit();
        /** 為方便測試,(全局的)限制設(shè)置為 10 */
        $limit->setLimitQps(10);
        $limit->attachServer(ServerManager::getInstance()->getSwooleServer());
        Di::getInstance()->set('auto_limiter', $limit);
    }
}

App\HttpController\Index.php 中調(diào)用限流器:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\HttpController;

use EasySwoole\AtomicLimit\AtomicLimit;
use EasySwoole\Component\Di;
use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    /** @var AtomicLimit $autoLimiter */
    private $autoLimiter;

    protected function onRequest(?string $action): ?bool
    {
        $this->autoLimiter = Di::getInstance()->get('auto_limiter');

        if ($action == 'test1') {
            # 調(diào)用限流器對 http://127.0.0.1:9501/test1 請求限制流量
            if ($this->autoLimiter->access($action, 1)) {
                return true;
            } else {
                $this->writeJson(200, null, 'test1 refuse!');
                return false;
            }
        } else if ($action == 'test2') {
            # 調(diào)用限流器對 http://127.0.0.1:9501/test2 請求限制流量
            if ($this->autoLimiter->access($action, 2)) {
                return true;
            } else {
                $this->writeJson(200, null, 'test2 refuse!');
                return false;
            }
        }

        return parent::onRequest($action);
    }

    public function test1()
    {
        $this->writeJson(200, null, 'test1 success!');
    }

    public function test2()
    {
        $this->writeJson(200, null, 'test2 success!');
    }
}

以上代碼表示,index/test1 這個限流器在每秒內(nèi)允許的最大流量為 1,而 index/test2 這個限流器的最大流量為 2

我們也可以在 EasySwooleBase 控制器的 onRequest 方法中,進行請求攔截。例如在全局 onRequest 事件中,先進行流量檢驗,如果校驗通過,則進行下一步操作。

在 Swoole 中使用

以經(jīng)典的暴力 CC 攻擊防護為例子。我們可以限制一個 ip-urlqps 訪問。

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

// example url: http://127.0.0.1:9501/index.html?api=1

require_once __DIR__ . '/vendor/autoload.php';

use EasySwoole\AtomicLimit\AtomicLimit;

$http = new swoole_http_server("127.0.0.1", 9501);

###### 配置限流器 ######
$limit = new AtomicLimit();
/** 為方便測試,(全局的)限制設(shè)置為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 函數(shù)允許單獨對某個 token 指定qps */
    if ($limit->access($token)) {
        $response->write('request accept');
    } else {
        $response->write('request refuse');
    }
    $response->end();
});

$http->start();

注意,本例子是用一個自定義進程內(nèi)加定時器來實現(xiàn)計數(shù)定時重置,實際上用一個進程來做這件事情有點不值得,因此實際生產(chǎn)可以指定一個 worker,設(shè)置定時器來實現(xiàn)。

主站蜘蛛池模板: 国产a级黄色片 | 国产精品嫩草影院ccm | 天天好逼综合 | 亚洲成人精品一区二区三区 | 一级毛片一级毛片一级毛片 | 欧美日韩中文一区 | 中国亚洲gvgay视频在线观看 | 成人二区 | 一区二区在线视频观看 | 成人毛片一级 | 成人精品一区二区三区视频播放 | 免费看黄色毛片 | 天堂影院在线 | 日韩毛片在线免费观看 | 国产一区二区精彩视频 | 7788色淫网站免费观看 | 一级毛片免费完整版 | 婷婷日韩 | 91麻精品国产91久久久久 | 成人妇女淫片aaaa视频 | 国产精品视频一区二区三区四区国 | 日韩1234 | 吾色视频 | 妈妈的朋友全集免费 | 欧美视频www | 好日吊| 亚洲精品午夜久久久久久久 | 久久国产欧美 | 性欧美精品久久久久久久 | 免费三区 | 国产欧美日韩中文字幕 | 日日躁夜夜摸月月添添添 | 成人短视频在线观看 | 嫩色av | 少妇一级淫片免费 | www.欧美在线观看 | 日韩高清在线 | aaaaaaa毛片 | 亚洲经典av | 黄片毛片免费看 | 色婷婷av一区 |