PHPackages                             detain/rate-limit - 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. [Caching](/categories/caching)
4. /
5. detain/rate-limit

ActiveLibrary[Caching](/categories/caching)

detain/rate-limit
=================

PHP rate limiting library with Token Bucket and Leaky Bucket Algorithms, based on palepurple/rate-limit, grandson to touhonoob/rate-limit, and jeroenvisser101/LeakyBucket

3.0.1(3y ago)28892MITPHPPHP &gt;=5.6CI failing

Since May 16Pushed 1w agoCompare

[ Source](https://github.com/detain/rate-limit)[ Packagist](https://packagist.org/packages/detain/rate-limit)[ RSS](/packages/detain-rate-limit/feed)WikiDiscussions master Synced 5d ago

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

rate-limit
==========

[](#rate-limit)

[![GitHub Build](https://github.com/detain/rate-limit/actions/workflows/php.yml/badge.svg)](https://github.com/detain/rate-limit/actions/workflows/php.yml)[![Travis Build](https://camo.githubusercontent.com/21eb2a1049a29b49652f7fc18e0231d7bfaefd1a521a2e89195e51cb6cf4f80e/68747470733a2f2f6170702e7472617669732d63692e636f6d2f64657461696e2f726174652d6c696d69742e7376673f6272616e63683d6d6173746572)](https://app.travis-ci.com/detain/rate-limit)[![Coverage Status](https://camo.githubusercontent.com/77470a1229a637bc7eb1b1ad6a4479c9b80e0fecb6df7874636708cfe95e15e2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f64657461696e2f726174652d6c696d69742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/detain/rate-limit?branch=master)[![Code Climate](https://camo.githubusercontent.com/09d53d28beadef36f9da71b6ee3a649316bc39a5b71efebce16a84464a747fa1/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f64657461696e2f726174652d6c696d69742f6261646765732f6770612e737667)](https://codeclimate.com/github/detain/rate-limit)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4cee511bb21181ae7cc74e019c219222795c1ee35f411df4257e7aa3469496fd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64657461696e2f726174652d6c696d69742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/detain/rate-limit/?branch=master)[![Codacy Badge](https://camo.githubusercontent.com/f822bcf4725b8dff2c106381f533096f9df24d3995de652b78b225a133567461/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3635393532336636336531363438376561373166366237363339303864303965)](https://www.codacy.com/app/detain/rate-limit)

[![Latest Stable Version](https://camo.githubusercontent.com/71ae2c6498c55c1b5cdc0570b90569d15440ce7aaec80b3716d43141f8f863fe/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f726174652d6c696d69742f76657273696f6e)](https://packagist.org/packages/detain/rate-limit)[![Total Downloads](https://camo.githubusercontent.com/7d81b26f5247109c830bfbf8b8903c27a55b9ca5120c00feef228edf47d7b222/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f726174652d6c696d69742f646f776e6c6f616473)](https://packagist.org/packages/detain/rate-limit)[![Latest Unstable Version](https://camo.githubusercontent.com/b3f9f5c9a4429b8e2c28ea71a98c7f38ee63e42474b200ea2a0be2b1ffb2e393/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f726174652d6c696d69742f762f756e737461626c65)](//packagist.org/packages/detain/rate-limit)[![License](https://camo.githubusercontent.com/0ba7586c999a0008ef9b40a84dcefeab75f06d77f0a73c5401adc5fc4f55e1ac/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f726174652d6c696d69742f6c6963656e7365)](https://packagist.org/packages/detain/rate-limit)[![Monthly Downloads](https://camo.githubusercontent.com/13ecc23ab4904062048d0f0bbcf3a5f7e19c39a3ca593e86da5b4787a15b6f56/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f726174652d6c696d69742f642f6d6f6e74686c79)](https://packagist.org/packages/detain/rate-limit)[![Daily Downloads](https://camo.githubusercontent.com/130cfd0dc505695773126c8dafb29e72975dd41b456e06592ab6a3613cf393a0/68747470733a2f2f706f7365722e707567782e6f72672f64657461696e2f726174652d6c696d69742f642f6461696c79)](https://packagist.org/packages/detain/rate-limit)

PHP Rate Limiting library with both Token Bucket and Leaky Bucket Algorithms, minimal external dependencies, and many storage backends.

Algorithms
==========

[](#algorithms)

- [Token Bucket Algorithm](https://en.wikipedia.org/wiki/Token_bucket) Token Bucket is an algorithm which works as follows:

    - There is a bucket.
    - A token is added to the bucket every 1/r seconds.
    - The bucket can hold at the most b tokens. If a token arrives when the bucket is full, it is discarded.
    - When a packet (network layer PDU) of n bytes arrives,
        - If at least n tokens are in the bucket, n tokens are removed from the bucket, and the packet is sent to the network.
        - if fewer than n tokens are available, no tokens are removed from the bucket, and the packet is considered to be non-conformant.
- [Leaky Bucket Algorithm](https://en.wikipedia.org/wiki/Leaky_bucket) Leaky Bucket is an algorithm which works as follows:

    - There is a bucket.
    - The bucket has a defined leak and defined capacity.
    - The bucket leaks at a constant rate.
    - Overflows when full, will not add other drops to the bucket.

Storage Adapters
================

[](#storage-adapters)

The RateLimiter needs to know where to get/set data.

Depending on which adapter you install, you may need to install additional libraries (predis/predis or tedivm/stash) or PHP extensions (e.g. Redis, Memcache, APCu)

- [APCu](https://pecl.php.net/package/APCu)
- [Redis](https://pecl.php.net/package/redis) or [Predis](https://github.com/nrk/predis)
- [Stash](http://www.stashphp.com) (This supports many drivers - see  )
- [Memcached](http://php.net/manual/en/intro.memcached.php)

Installing via Composer
=======================

[](#installing-via-composer)

```
curl -sS https://getcomposer.org/installer | php
composer.phar require detain/rate-limit
```

Usage
=====

[](#usage)

Token Bucket
------------

[](#token-bucket)

```
require 'vendor/autoload.php';

use \Detain\RateLimit\RateLimit;
use \Detain\RateLimit\Adapter\APCu as APCAdapter;
use \Detain\RateLimit\Adapter\Redis as RedisAdapter;
use \Detain\RateLimit\Adapter\Predis as PredisAdapter;
use \Detain\RateLimit\Adapter\Memcached as MemcachedAdapter;
use \Detain\RateLimit\Adapter\Stash as StashAdapter;

$adapter = new APCAdapter(); // Use APC as Storage
// Alternatives:
//
// $adapter = new RedisAdapter((new \Redis()->connect('localhost'))); // Use Redis as Storage
//
// $adapter = new PredisAdapter(new \Predis\Predis(['tcp://127.0.0.1:6379'])); // Use Predis as Storage
//
// $memcache = new \Memcached();
// $memcache->addServer('localhost', 11211);
// $adapter = new MemcacheAdapter($memcache);
//
// $stash = new \Stash\Pool(new \Stash\Driver\FileSystem());
// $adapter = new StashAdapter($stash);

$rateLimit = new RateLimit("myratelimit", 100, 3600, $adapter); // 100 Requests / Hour

$id = $_SERVER['REMOTE_ADDR']; // Use client IP as identity
if ($rateLimit->check($id)) {
  echo "passed";
} else {
  echo "rate limit exceeded";
}
```

Leaky Bucket
------------

[](#leaky-bucket)

### Basic usage

[](#basic-usage)

```
