PHPackages                             razonyang/token-bucket - 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. razonyang/token-bucket

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

razonyang/token-bucket
======================

PHP Token Bucket

1.0.0(6y ago)725.1k↓48.1%12BSD-3-ClausePHPPHP ^7.1

Since Aug 21Pushed 6y ago1 watchersCompare

[ Source](https://github.com/razonyang/php-token-bucket)[ Packagist](https://packagist.org/packages/razonyang/token-bucket)[ RSS](/packages/razonyang-token-bucket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (2)

PHP Token Bucket
================

[](#php-token-bucket)

[![Build Status](https://camo.githubusercontent.com/f30fc8709ed46ccaaff360193a056ccc3ed9c4c0ef46c1d9cc213d1beae2f011/68747470733a2f2f7472617669732d63692e6f72672f72617a6f6e79616e672f7068702d746f6b656e2d6275636b65742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/razonyang/php-token-bucket)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/14b550d1acde2dd2a9e0ea42b0c4f03467dc1c4e8e193ec4546e2f7c6f2a4292/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f72617a6f6e79616e672f7068702d746f6b656e2d6275636b65742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/razonyang/php-token-bucket/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/015ae04eed0a3711dcc356a191d7145408b46438276206f8b113a8f02fdd5028/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f72617a6f6e79616e672f7068702d746f6b656e2d6275636b65742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/razonyang/php-token-bucket/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/ce576d7a8faf31e54fab42b2df9e7d745d697496b11802653dbf63434e7ed019/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72617a6f6e79616e672f746f6b656e2d6275636b65742e737667)](https://packagist.org/packages/razonyang/token-bucket)[![Total Downloads](https://camo.githubusercontent.com/8b876e91cec0de600ecc769a54de55b53b875857aa65efdecdb5b8ba48097e54/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72617a6f6e79616e672f746f6b656e2d6275636b65742e737667)](https://packagist.org/packages/razonyang/token-bucket)[![LICENSE](https://camo.githubusercontent.com/45b0b3760e1373dfb63dce74eb011d18c25d650df663e3e169da8353c7d1ed83/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72617a6f6e79616e672f7068702d746f6b656e2d6275636b6574)](LICENSE)

It is an implementation of [Token Bucket](https://en.wikipedia.org/wiki/Token_bucket) algorithm that for HTTP rate limiter.

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

[](#installation)

```
$ composer require razonyang/token-bucket
```

Integration
-----------

[](#integration)

- [PSR-15 Rate Limiter Middleware](https://github.com/razonyang/psr-rate-limiter)
- [Yii2 Rate Limiter](https://github.com/razonyang/yii2-rate-limiter)

You can also build your own, let's take 5000 requests per hours as example:

```
// create a token bucket manager
$capacity = 5000; // each bucket capacity, in other words, maximum number of tokens.
$rate = 0.72; // 3600/5200, how offen the token will be added to bucket
$logger = new \Psr\Log\NullLogger(); // PSR logger
$ttl = 3600; // time to live.
$prefix = 'rateLimiter:'; // prefix.

$manager = new \RazonYang\TokenBucket\Manager\RedisManager($capacity, $rate, $logger, $redis, $ttl, $prefix);

// implements rate limiter, comsumes a token from the bucket which called $name.
$name = 'uid:route'; // the name of bucket.
$comsumed = $manager->consume($name, $remaining, $reset);

// set header
header('X-Rate-Limit-Limit: ' . $manager->getLimit());
header('X-Rate-Limit-Remaining: ' . $remaining); // remaining number of tokens.
header('X-Rate-Limit-Reset: ' . $reset);

if (!$comsumed) {
    throw new \Exception('Too many requests', 429);
}

// continue handling
```

Credit
------

[](#credit)

It was inspired from the follow documents and code:

- [Token Bucket](https://en.wikipedia.org/wiki/Token_bucket)
- [What's a good rate limiting algorithm?](https://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm)
- [Yii2 Rate Limiter](https://github.com/yiisoft/yii2/blob/master/framework/filters/RateLimiter.php)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2463d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17720932?v=4)[Razon Yang](/maintainers/RazonYang)[@razonyang](https://github.com/razonyang)

---

Top Contributors

[![razonyang](https://avatars.githubusercontent.com/u/17720932?v=4)](https://github.com/razonyang "razonyang (1 commits)")

---

Tags

rate-limitertoken-bucketrate-limitertoken bucket

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/razonyang-token-bucket/health.svg)

```
[![Health](https://phpackages.com/badges/razonyang-token-bucket/health.svg)](https://phpackages.com/packages/razonyang-token-bucket)
```

###  Alternatives

[symfony/rate-limiter

Provides a Token Bucket implementation to rate limit input and output in your application

27047.2M147](/packages/symfony-rate-limiter)[bandwidth-throttle/token-bucket

Implementation of the Token Bucket algorithm.

5121.9M10](/packages/bandwidth-throttle-token-bucket)[j0k3r/php-readability

Automatic article extraction from HTML

186808.8k6](/packages/j0k3r-php-readability)[maba/gentle-force-bundle

Symfony bundle that integrates gentle-force library for limiting both brute-force attempts and ordinary requests, using leaky/token bucket algorithm, based on Redis

53517.6k1](/packages/maba-gentle-force-bundle)[spomky-labs/pwa-bundle

Progressive Web App Manifest Generator Bundle for Symfony.

6144.4k1](/packages/spomky-labs-pwa-bundle)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
