PHPackages                             oaklabs/phalcon-throttler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. oaklabs/phalcon-throttler

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

oaklabs/phalcon-throttler
=========================

Rate limiter PHP Phalcon Framework.

v0.2(8y ago)182.7k3[1 issues](https://github.com/oak-labs-io/phalcon-throttler/issues)MITPHPPHP &gt;=7.1

Since Nov 23Pushed 8y ago2 watchersCompare

[ Source](https://github.com/oak-labs-io/phalcon-throttler)[ Packagist](https://packagist.org/packages/oaklabs/phalcon-throttler)[ RSS](/packages/oaklabs-phalcon-throttler/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Throttler
=========

[](#throttler)

[![License](https://camo.githubusercontent.com/dc2a76697ed70317a7852e8ac8f2c29790beaa29838cb20165c6fe47a8202b6b/68747470733a2f2f706f7365722e707567782e6f72672f6f616b6c6162732f7068616c636f6e2d7468726f74746c65722f6c6963656e7365)](https://packagist.org/packages/oaklabs/phalcon-throttler)[![Latest Stable Version](https://camo.githubusercontent.com/dabfeaf1e6e42907bd9a5fd6f366fc1bc2edb9e6a5b66857ab77bd2715052f6f/68747470733a2f2f706f7365722e707567782e6f72672f6f616b6c6162732f7068616c636f6e2d7468726f74746c65722f762f737461626c65)](https://packagist.org/packages/oaklabs/phalcon-throttler)[![Latest Unstable Version](https://camo.githubusercontent.com/089ba7be7cc7270fe3e66978739508d324e1837ea7633a6da7ad01c9bfedcbc3/68747470733a2f2f706f7365722e707567782e6f72672f6f616b6c6162732f7068616c636f6e2d7468726f74746c65722f762f756e737461626c65)](https://packagist.org/packages/oaklabs/phalcon-throttler)[![Build Status](https://camo.githubusercontent.com/16d40fe71fb53505951ee14f3ca8d3303e6aed225c26c92a3b4f257797c44b68/68747470733a2f2f7472617669732d63692e6f72672f6f616b2d6c6162732d696f2f7068616c636f6e2d7468726f74746c65722e737667)](https://travis-ci.org/oak-labs-io/phalcon-throttler)

Introduction
------------

[](#introduction)

Phalcon Throttler is a Rate Limiter for PHP Phalcon Framework.

It provides a simple interface to build Rate Limiters using various strategies as well as with a Redis Throttler ready out of the box.

PHP 7.1+ and Phalcon 3.1.2+ are required.

Installation
------------

[](#installation)

Throttler can be installed through Composer, just include `"oaklabs/phalcon-throttler": "^0.1"` to your composer.json and run `composer update` or `composer install`.

Usage
-----

[](#usage)

### Throttling

[](#throttling)

Phalcon Throttler comes shipped with a Redis throttler by default. It uses [PhpRedis](https://github.com/phpredis/phpredis) to communicate with the Redis server.

First of all we need a `Redis` instance. We can then add a `redis` service in the Phalcon Dependency Injection container

```
$di->setShared('redis', function () use ($config) {
    $redis = new \Redis();
    $redis->pconnect($config->redis->host, $config->redis->port);
    $redis->auth($config->redis->password);

    return $redis;
});
```

so that it can be used when we want to create an instance of the Redis Throttler. We can set it up in the Dependency Injection container as well

```
$di->setShared('throttler',function() use ($di) {
    return new OakLabs\PhalconThrottler\RedisThrottler($di->get('redis'), [
        'bucket_size'  => 20,
        'refill_time'  => 600, // 10m
        'refill_amount'  => 10
    ]);
});
```

The second parameter allows to configure the behaviour of the Throttler:

- *bucket\_size*: the number of allowed hits in the period of time of reference
- *refill\_time*: the amount of time after that the counter will completely or partially reset
- *refill\_amount*: the number of hits to be reset every time the refill\_time passes

You are now able to successfully throttle users:

```
$throttler = $this->getDI()->get('throttler');
$rateLimit = $throttler->consume($this->request->getClientAddress());

if ($rateLimit->isLimited()) {
    // Do something
}
```

### Strategies

[](#strategies)

The only question left is: which one is the appropriate place where the check should be performed?

There is of course not an uniquely valid answer, several places can be used.

**Check in the dispatcher**

A good strategy is to put the check during the Phalcon dispatcher lifecycle.

In the dependency injection we can use the Phalcon Event Manager to listen to the dispatcher event and bind it to some Security plugin

```
$di->setShared('eventsManager',function() use ($di) {
    $eventsManager = new \Phalcon\Events\Manager();
    return $eventsManager;
});

$di->set('dispatcher', function () use ($di) {
    //Create an EventsManager
    $eventsManager = $di->getShared('eventsManager');

    $security = new \MyNamespace\Security();
    $eventsManager->attach('dispatch', $security);

    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});
```

and put our Rate Limiter in it

```
