franzip/throttler

简单的速率限制器和使用跟踪组件。

1.0.1 2024-05-13 18:08 UTC

This package is auto-updated.

Last update: 2024-09-13 18:47:48 UTC


README

Build Status Coverage Status

Throttler

一个简单、通用的速率限制器和使用跟踪库。

通过 Composer 安装(推荐)

在项目中安装 composer

curl -s http://getcomposer.org/installer | php

在项目根目录创建 composer.json 文件

{
    "require": {
        "franzip/throttler": "1.0.*"
    }
}

通过 composer 安装

php composer.phar install

描述

一旦实例化,对象必须始终使用提供的 start() 方法显式激活。

基本上,你设置一个时间段(可以是24小时、2秒、30分钟等),全局限制以及你想要跟踪的内容,然后就可以开始了。

你还可以设置一个额外的限制,该限制将应用于你跟踪的每个单个组件。

你只能在停止当前实例跟踪后更改给定的参数,使用提供的 stop() 方法。

Throttler 将检测给定时间段是否过期:所有内部状态将重置,并计算新的时间段。

如果你需要访问任何 Throttler 属性,只需使用提供的获取器(getName、getGlobalThreshold等)。请参阅构造函数。

构造函数

$throttler = new Throttler($name, $globalThreshold, $metric, $metricFactor = 1,
                           $componentThreshold = null, $components = array());

允许的 $metric

  • 'sec': 以(秒 * $metricFactor)计算时间段
  • 'min': 以(分钟 * $metricFactor)计算时间段
  • 'hrs': 以(小时 * $metricFactor)计算时间段

基本用法(默认参数)

示例 1:使用 Throttler 限制传入请求。每小时请求总量将限制为30。

use Franzip\Throttler\Throttler;

// Setting things up
// Max 30 total requests per hour
$throttler = new Throttler('requests', 30, 'hrs');

// Nothing happens and the call will return false since there is nothing to track
$throttler->start();

// Add remote addresses to track...
$throttler->addComponents('AddressToTrack1');
$throttler->addComponents('AddressToTrack2');
$throttler->addComponents('AddressToTrack3');
// Bulk adding
$throttler->addComponents(array('AddressToTrack4',
                                'AddressToTrack5',
                                'AddressToTrack6',
                                ...));
...

// Start tracking (timeframe starts now)
$throttler->start();

if ($throttler->updateComponent('AddressToTrack1')) {
    // handle update success
} else {
    // handle update failure
}

$numOfRequests = 31;

// will return false since the global limit is 30
$throttler->updateComponent('AddressToTrack1', $numOfRequests);

...

// Remove all stuff to track
$throttler->stop();
$throttler->setComponents(array());

用法(自定义参数)

示例 2:使用 Throttler 限制传入请求。

每天请求总量将限制为100。

来自每个地址的传入请求量将每天限制为10。

use Franzip\Throttler\Throttler;

// Setting things up
// Max 100 total requests per day
// Max 10 requests from each tracked address per day
$throttler = new Throttler('requests', 100, 'hrs', 24,
                           10, array('AddressToTrack1',
                                     'AddressToTrack2',
                                     ...));

// Start tracking (timeframe starts now)
$throttler->start();

if ($throttler->updateComponent('AddressToTrack1')) {
    // handle success
} else {
    // handle failure
}

isActive()

返回跟踪是否开启。

use Franzip\Throttler\Throttler;

$throttler = new Throttler('requests', 100, 'hrs');

// false
$throttler->isActive();

// false, there's nothing to track yet.
$throttler->start();

// false
$throttler->isActive();

$throttler->addComponents(array('foo', 'bar'));

// true, we have something to track
$throttler->start();

// true
$throttler->isActive();

// reset the instance (this will also stop tracking)
$throttler->reset();

// false
$throttler->isActive();

reset()

你可以使用 reset() 方法在任何时候将 Throttler 对象的状态还原到实例化时的状态。

use Franzip\Throttler\Throttler;

$throttler = new Throttler('requests', 100, 'hrs');
// Change time cap to 2 hours
$throttler->setMetricFactor(2);
// Change time cap to 2 minutes
$throttler->setMetric('min');
// Change global limit to 50
$throttler->setGlobalThreshold(50);

...

$throttler->reset();
// reverted to 100
$throttler->getGlobalThreshold();
// reverted to 'hrs'
$throttler->getMetric();

待办事项

  • 一个不错的异常系统。
  • 将验证移至外部类。
  • 允许批量添加组件。
  • 重构杂乱的测试。
  • 重构更新检查方法。

许可证

MIT 公共许可证。