PHPackages                             xp-forge/ratelimit - 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. xp-forge/ratelimit

ActiveLibrary

xp-forge/ratelimit
==================

Rate limiting

v3.2.0(2y ago)017BSD-3-ClausePHPPHP &gt;=7.0.0

Since Feb 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/xp-forge/ratelimit)[ Packagist](https://packagist.org/packages/xp-forge/ratelimit)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-ratelimit/feed)WikiDiscussions master Synced 1mo ago

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

Rate limiting
=============

[](#rate-limiting)

[![Build status on GitHub](https://github.com/xp-forge/ratelimit/workflows/Tests/badge.svg)](https://github.com/xp-forge/ratelimit/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/74c7e88788f394c236377281428714b32d510b0398eb4e14bc64dde9f514be50/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f726174656c696d69742f76657273696f6e2e706e67)](https://packagist.org/packages/xp-forge/ratelimit)

A rate limiting can be used to limit the rate at which physical or logical resources are accessed, e.g. to protect them from intentional or unintentional overuse.

Restricting usage
-----------------

[](#restricting-usage)

Imagine you don't want to run more than two tasks per second:

```
use util\invoke\RateLimiting;

$rateLimiter= new RateLimiting(2);
foreach ($tasks as $task) {
  $rateLimiter->acquire();    // will wait if necessary
  $task->run();
}
```

Restricting bandwidth
---------------------

[](#restricting-bandwidth)

You can implement bandwidth throttling by acquiring a permit for each byte:

```
use util\invoke\{RateLimiting, Rate, Per};

$rateLimiter= new RateLimiting(new Rate(1000000, Per::$MINUTE));
while ($bytes= $source->read()) {
  $rateLimiter->acquire(strlen($bytes));
  $target->write($bytes);
}
```

Rate-limiting users
-------------------

[](#rate-limiting-users)

Implement a filter like the following:

```
use web\{Filter, Error};
use util\invoke\{RateLimiting, Rate, Per};

class RateLimitingFilter implements Filter {
  private $rates, $rate, $timeout;

  public function __construct(KeyValueStorage $rates) {
    $this->rates= $rates;
    $this->rate= new Rate(5000, Per::$HOUR);
    $this->timeout= 0.2;
  }

  public function filter($request, $response, $invocation) {
    $remote= $request->header('Remote-Addr');

    $limits= $this->rates->get($remote) ?: new RateLimiting($this->rate);
    $permitted= $limits->tryAcquiring(1, $this->timeout);
    $this->rates->put($remote, $limits);

    $response->header('X-RateLimit-Limit', $limits->rate()->value());
    $response->header('X-RateLimit-Remaining', $limits->remaining());
    $response->header('X-RateLimit-Reset', $limits->resetTime());

    if (!$permitted) {
      throw new Error(429, 'Rate limit exceeded');
    }

    return $invocation->proceed($request, $response);
  }
}
```

Further reading
---------------

[](#further-reading)

- [RateLimiter - discovering Google Guava](http://www.nurkiewicz.com/2012/09/ratelimiter-discovering-google-guava.html) by Tomasz Nurkiewicz
- [Guava's RateLimiter class](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/RateLimiter.html) - which is what this project was inspired from.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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

Every ~415 days

Recently: every ~478 days

Total

9

Last Release

785d ago

Major Versions

v0.3.0 → v1.0.02016-02-21

v1.0.0 → v2.0.02018-12-29

v2.0.1 → v3.0.02020-04-10

PHP version history (4 changes)v0.1.0PHP &gt;=5.4.0

v0.2.0PHP &gt;=5.5.0

v2.0.0PHP &gt;=5.6.0

v3.0.0PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (56 commits)")

---

Tags

ratelimiterxp-frameworkmodulexp

### Embed Badge

![Health badge](/badges/xp-forge-ratelimit/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-ratelimit/health.svg)](https://phpackages.com/packages/xp-forge-ratelimit)
```

PHPackages © 2026

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