黄p网站_在线看中文字幕_亚洲电影免费观看_成人激情视频_欧美成视频_中国av在线

AtomicLimit

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

原理

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

組件要求

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

安裝方法

composer require easyswoole/atomic-limit

倉(cāng)庫(kù)地址

easy-swoole/atomic-limit

在 EasySwoole 中使用

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://m.edpy57.cn
 * @document http://m.edpy57.cn
 * @contact http://m.edpy57.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();
        /** 為方便測(cè)試,(全局的)限制設(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://m.edpy57.cn
 * @document http://m.edpy57.cn
 * @contact http://m.edpy57.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)用限流器對(duì) http://127.0.0.1:9501/test1 請(qǐng)求限制流量
            if ($this->autoLimiter->access($action, 1)) {
                return true;
            } else {
                $this->writeJson(200, null, 'test1 refuse!');
                return false;
            }
        } else if ($action == 'test2') {
            # 調(diào)用限流器對(duì) http://127.0.0.1:9501/test2 請(qǐng)求限制流量
            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 這個(gè)限流器在每秒內(nèi)允許的最大流量為 1,而 index/test2 這個(gè)限流器的最大流量為 2

我們也可以在 EasySwooleBase 控制器的 onRequest 方法中,進(jìn)行請(qǐng)求攔截。例如在全局 onRequest 事件中,先進(jìn)行流量檢驗(yàn),如果校驗(yàn)通過(guò),則進(jìn)行下一步操作。

在 Swoole 中使用

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://m.edpy57.cn
 * @document http://m.edpy57.cn
 * @contact http://m.edpy57.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();
/** 為方便測(cè)試,(全局的)限制設(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ù)允許單獨(dú)對(duì)某個(gè) token 指定qps */
    if ($limit->access($token)) {
        $response->write('request accept');
    } else {
        $response->write('request refuse');
    }
    $response->end();
});

$http->start();

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

主站蜘蛛池模板: 91在线视频播放 | 亚洲成人精品久久久 | 欧美另类久久 | 狠狠撸在线视频 | 国产精品久久久久免费a∨ 欧洲精品一区 | 亚洲一区二区在线 | 国产精品久久久久久久午夜 | 国产成人精品久久二区二区 | 亚洲精品大片 | 亚洲品质自拍视频网站 | 亚洲久久| 99精品国产高清在线观看 | 亚洲欧美在线免费观看 | 久久夜色精品 | 亚洲日本欧美 | 久久综合九色综合欧美狠狠 | 99精品视频在线 | 久久综合99re88久久爱 | 亚洲精品在线国产 | 欧美成人免费网站 | 成人理论片 | 91国产精品 | 国产精品久久精品 | 日韩在线观看毛片 | 精品久久久久久亚洲精品 | 在线观看亚洲免费 | 亚洲精品一区二区三区在线 | 五月婷婷综合激情网 | 精品国产精品 | 一级色视频 | 91xx在线观看 | 99福利视频| 久久久99久久久国产自输拍 | 国产精品99 | 久久久久久久久综合 | 国产一区二区三区在线免费观看 | 国产高清在线精品一区二区三区 | 日韩av一区二区在线观看 | 日韩一区高清视频 | 日韩成人在线观看 | 狠狠操操|