PHP code example of easyswoole / atomic-limit

1. Go to this page and download the library: Download easyswoole/atomic-limit 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/ */

    

easyswoole / atomic-limit example snippets


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();