dev-master
9999999-devStackPHP compatible class for throttling HTTP requests using Predis. Based on datagraph/rack-throttle.
MIT
The Requires
stack php throttle
StackPHP compatible class for throttling HTTP requests using Predis. Based on datagraph/rack-throttle.
Throttle is a StackPHP middleware for limiting how often individual IPs can make requests to an application. It's very simple and loosely inspired by Ruby / Rack's datagraph/rack-throttle library., (*1)
Throttle can only use Predis at the moment. I would like to write some adapters to allow the use of other clients soon., (*2)
Sample usage with a Silex App:, (*3)
require_once __DIR__.'/../vendor/autoload.php'; $app = new \Silex\Application(); $stack = (new Stack\Builder()) ->push('pno\Throttle', new \Predis\Client()); $app = $stack->resolve($app);
It works by tracking the number of requests that come from a particular IP during a given interval of time. The default limit is 360 requests per 3600 seconds - i.e., one request per 10 seconds for each hour. If an IP has hit the limit, Throttle responds by sending a Symfony 403 response with the message "Rate limit exceeded"., (*4)
You can override any of those defaults by passing them to Throttle's constructor., (*5)
If you're using the Stack\Builder class to apply your middlewares, and you would like to limit IPs to 2 requests every 10 seconds, and give a more colorful response message, you could do something like this:, (*6)
$response = new Symfony\Component\HttpFoundation\Response('STAY OFF MY LAWN!!! >:(', 403); $stack = (new Stack\Builder()) ->push('pno\Throttle', new Predis\Client(), 2, 10, $response);
And ah yes, lest I forget: this has been added to packagist, for the convenience, wonder and bemusement of all:, (*7)
StackPHP compatible class for throttling HTTP requests using Predis. Based on datagraph/rack-throttle.
MIT
stack php throttle