PHP code example of vipkwd / webman-throttle

1. Go to this page and download the library: Download vipkwd/webman-throttle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vipkwd / webman-throttle example snippets



//cat config/middleware.php

return [
    // 全局中间件
    '' => [
        // ... 这里省略其它中间件
        app\middleware\Throttle::class,
    ]
];



// cat config/throttle.php

/**
 * $routes_rate 应用场景:
 *
 *   -- 截至(Webman 1.2.5, Webman-framework v1.2.7) 不支持路由中间件默认传参。
 *
 *   故在此主动申明各路由绑定的 限流规则
 *
 */
/*
$routes_rate = [
    [
        //maps 内配置的route PATH复用本组配置
        'maps' => ['/v1/api/user/info', '/v1/api/user/assets'],

        // 要被限制的请求类型, eg: GET POST PUT DELETE HEAD
        'visit_method' => ['GET','POST'],

        // 设置访问频率,例如 '10/m' 指的是允许每分钟请求10次。值 null 表示不限制,
        // eg: null 10/m  20/h  300/d 200/300
        'visit_rate' => '2/10',

        //'visit_fail_response' =>  function(){}
    ],
];
*/

return [
    //特殊路由限流规则
    'routes_rate' => $routes_rate ?? [],

    // 缓存键前缀,防止键值与其他应用冲突
    'prefix' => 'throttle_',

    // 缓存的键,true 表示使用来源ip (request->getRealIp(true))
    'key' => true,

    // 响应体中设置速率限制的头部信息,含义见:https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
    'visit_enable_show_rate_limit' => 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' => '2/10',

    // 访问受限时返回的响应( type: null|callable )
    'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds): Response {
        $msg = 'Too many requests, try again after ' . $wait_seconds . ' seconds.';
        return !$request->isAjax() ? response($msg) : json([
            'code' => 429,
            'msg' => $msg,
            'data' => null
        ]);
    },
    // ----------·----------·----------·----------·----------

    /*
     * 设置节流算法,组件提供了四种算法:
     *  - CounterFixed :计数固定窗口
     *  - CounterSlider: 滑动窗口
     *  - TokenBucket : 令牌桶算法
     *  - LeakyBucket : 漏桶限流算法
     */
    'driver_name' => CounterFixed::class,

    // Psr-16通用缓存库规范: https://blog.csdn.net/maquealone/article/details/79651111
    // Cache驱动必须符合PSR-16缓存库规范,最低实现get/set俩个方法 (且需静态化实现)
    //    static get(string $key, mixed $default=null)
    //    static set(string $key, mixed $value, int $ttl=0);

    //webman默认使用 symfony/cache作为cache组件(https://www.workerman.net/doc/webman/db/cache.html)
	'cache_drive' => support\Cache::class,

    //使用ThinkCache
    // 'cache_drive' => think\facade\Cache::class,
];


cat config/throttle.php

'key' => function($throttle, $request) {
    return $request->session()->get('user_id');
},

cat config/throttle.php

'key' => function($throttle, $request) {

    return  implode('/', [
		$request->controller,
		$request->action,
		$request->getRealIp($safe_mode=true)
	]);
},

//'key' => 'controller/action/ip' //上述配置的快捷实现


cat config/throttle.php

'key' => function($throttle, $request)
    $throttle->setRate('5/m');                      // 设置频率
    $throttle->setDriverClass(CounterSlider::class);// 设置限流策略
    return true;
},

cat config/route.php

//此种方式:将只能应用默认规则(不同路由不能自定义限流策略)
Route::any('/api/opencv/driver', [ app\api\controller\Opencv::class, 'driver'])->middleware([
    app\middleware\Throttle::class
]);

//错误使用例子( webman不支持路由中间件默认传参。PS:这是think-throttle的用法)
cat config/route.php

Route::group('/path', function() {
    //路由注册
    ...

})->middleware(\app\middleware\Throttle::class, [
    'visit_rate' => '20/m',
    ...
    ...
]);

//正确使用例子

------ 配置:
cat config/throttle.php

...
...

$routes_rate = [
    [
        //maps 内配置的route PATH复用本组配置
        'maps' => ['/v1/api/user/assets', '/v1/api/user/info'],

        // 要被限制的请求类型, eg: GET POST PUT DELETE HEAD
        'visit_method' => ['GET','POST', 'PUT'],

        // 设置访问频率,例如 '10/m' 指的是允许每分钟请求10次。值 null 表示不限制,
        // eg: null 10/m  20/h  300/d 200/300

        //10秒内最多允许2次请求
        'visit_rate' => '2/10',
    ],
];

return [

    'routes_rate' => $routes_rate,

    ...
    ...

];


------ 开启限流中间件:
cat config/route.php

...
...

Route::any('/v1/api/user/assets',[app\api\controller\User:class, 'assets'])->middleware([\app\middleware\Throttle::class]);

...
...