承接 balambasik/throttler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

balambasik/throttler

Composer 安装命令:

composer require balambasik/throttler

包简介

Throttle any operations

README 文档

README

Throttler - simple php library.

Designed to limit any action. Minimum configuration, maximum simplicity.

Works with MySql drivers and Memory. Add your driver, implement DriverInterface.

Installation

composer require balambasik/throttler

One example of using

$driver = new MySQLDriver("localhost", "root", "", "throttler", "throttler");
$factory = new ThrottlerFactory($driver);

$registerThrottler = $factory
                ->id($_SERVER["REMOTE_ADDR"])
                ->tag("/api/register")
                ->waitSeconds(5)
                ->create();

// 1 request per 5 seconds per IP, per route - "/api/register"
if($registerThrottler->isLimit()){
    exit("Limit!")
} else {
  // handle request
}

// or -------------------------------------

$registerThrottler->isLimit(function(){
    exit("Limit!")
});

// handle request

Multiple instances

// 1 request per 5 seconds per IP, per route - "/api/register"
$registerThrottler = $factory
                ->id($_SERVER["REMOTE_ADDR"])
                ->tag("/api/register")
                ->waitSeconds(5)
                ->create();

// 1 request per 1 minute per IP, per route - "/api/forgot_password"              
$forgotThrottler = $factory
                ->id($_SERVER["REMOTE_ADDR"])
                ->tag("/api/forgot_password")
                ->waitMinutes(1)
                ->create();


$registerThrottler->isLimit(function(){
    exit("Limit!")
});


$forgotThrottler->isLimit(function(){
    exit("Limit!")
});

Manual mode

By default, method isLimit() logs a hit and checks if the limit has been reached. You can separate these operations.

// 1 request per 5 seconds per IP, per route - "/api/register"
$registerThrottler = $factory
                ->id($_SERVER["REMOTE_ADDR"])
                ->tag("/api/register")
                ->waitSeconds(5)
                ->createManualMode();

// set hit
$registerThrottler->hit();

// check limit
$registerThrottler->isLimit(function(){
    exit("Limit!")
});

The MySql driver needs a table.

CREATE TABLE IF NOT EXISTS `table_name` (
    `id` varchar(10),
    `tag` varchar(10),
    `wait` INT(11) UNSIGNED NOT NULL
    );

ALTER TABLE `table_name` ADD INDEX (`id`, `tag`);

Or call the createTable() method of the MySqlDriver object once

$MySQLDriver = new MySqlDriver("localhost", "login", "password", "dbName", "tableName");
$MySQLDriver->createTable();

InMemory driver

The InMemoryDriver does not require any configuration. For obvious reasons, it can persist state across requests. Therefore, protecting routes with it is a bad idea. It is great for limiting operations within a single request.

$driver = new InMemoryDriver();
$factory = new ThrottlerFactory($driver);
// ...

Licence - MIT.

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固