perimeter/rate-limiter-php

A very simple Rate Limiter for PHP

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

This package is not auto-updated.

Last update: 2024-12-21 17:58:42 UTC


README

Build Status

Rate Limit those APIs!

Installation

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

This library can be used alongside the perimeter/RateLimitBundle for Symfony2 applications. See the repository for more instructions.

Get Started

Create your throttler:

include_once('vendor/autoload.php');

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

Ensure redis is running by executing the redis-server command on the web server where the library is running:

$ redis-server

Now you can throttle your users accordingly!

// 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");
}

And that's it!