perimeter/rate-limiter-php

一个非常简单的PHP速率限制器

v0.1.1 2015-03-31 21:26 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:29:19 UTC


README

Build Status

限制这些API的速率!

安装

$ composer.phar require perimeter/rate-limiter-php:dev-develop

此库可用于与perimeter/RateLimitBundle一起用于Symfony2应用程序。请参阅存储库以获取更多信息。

开始使用

创建您的限速器

include_once('vendor/autoload.php');

$redis = new Predis\Client();
$throttler = new Perimeter\RateLimiter\Throttler\RedisThrottler($redis);

通过在运行库的Web服务器上执行redis-server命令来确保redis正在运行

$ redis-server

现在您可以相应地限制您的用户了!

// Create a meter ID based on something unique to your user
// In this case we use the IP. This can also be a username,
// company, or some other authenticated property
$meterId = sprintf('ip_address:%s', $_SERVER['REMOTE_ADDR']);
$warnThreshold = 10;
$limitThreshold = 20;

// run the "consume" command
$throttler->consume($meterId, $warnThreshold, $limitThreshold);

if ($throttler->isLimitWarning()) {
    echo "slow down!";
}

if ($throttler->isLimitExceeded()) {
    exit("you have been rate limited");
}

就是这样!