PHP code example of cjphp / rate-limiter

1. Go to this page and download the library: Download cjphp/rate-limiter 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/ */

    

cjphp / rate-limiter example snippets


rate-limiter/
├── src/
│   ├── RateLimiter.php                     限流器类
│   ├── StorageFactory.php                  存储工厂类
│   ├── StrategyFactory.php                 策略工厂类
│   ├── Storage/                           
│   │   ├── StorageInterface.php            存储接口
│   │   ├── FileStorage.php                 文件存储实现类
│   │   └── RedisStorage.php                redis存储实现类
│   └── Strategy/
│       ├── StrategyInterface.php           策略接口
│       ├── FixedWindowStrategy.php         固定窗口策略类
│       ├── SlidingWindowStrategy.php       滑动窗口策略类
│       └── LeakyBucketStrategy.php         漏桶策略类
├── composer.json
└── README.md

    public function needRateLimitFunction()
    {
        ...
        
        $key = 'user_123'; //key 为特定对象,此处假设用户id
        //以下可以写入配置文件 动态读取
        $storeType = "file";  //可选:file|redis
        $strategyType = "fixed_window"; //可选:fixed_window|sliding_window|leaky_bucket
        $limit = 10; // 每60秒 10 次请求
        $window = 60; // 60 秒
        $rate = 1; // 每秒漏5滴
        $capacity = 3; // 桶容量10
        $isGlobal = true; //是否全局限流
        $params = ["limit"=>$limit, "window"=> $window, "capacity"=>$capacity, "rate"=>$rate];

        $ext = match ($storeType) {
            'file' => '文件存储路径...',
            'redis' => redis实例,
        };
        $storage = StorageFactory::create($storeType, $ext);
        $strategy = StrategyFactory::create($strategyType, $storage, $params);

        $rateLimiter = new RateLimiter($storage, $strategy);

        if ($rateLimiter->isRateLimited($key, $isGlobal)) {
            echo "不好意思,您被流控了";
        } else {
            echo "允许通行.";
        }
        
        ...
    }