PHPackages                             agussuroyo/rate-limiter - 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. agussuroyo/rate-limiter

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

agussuroyo/rate-limiter
=======================

A simple and flexible rate limiter for PHP

1.0.0(1y ago)03MITPHPPHP &gt;=7.4CI passing

Since Mar 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/agussuroyo/rate-limiter)[ Packagist](https://packagist.org/packages/agussuroyo/rate-limiter)[ RSS](/packages/agussuroyo-rate-limiter/feed)WikiDiscussions master Synced 1mo ago

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

RateLimiter
===========

[](#ratelimiter)

RateLimiter is a simple and flexible PHP package for rate limiting requests using different storage backends such as file storage and Redis.

Features
--------

[](#features)

- Set request limits dynamically.
- Supports multiple storage backends (File, Redis).
- Easy-to-use API.
- Complies with SOLID principles for extensibility.

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

[](#installation)

You can install the package via Composer:

```
composer require agussuroyo/rate-limiter
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use AgusSuroyo\RateLimiter\RateLimiter;

$limiter = new RateLimiter('guest', 5, 60); // Key, Max Attempts, Decay Seconds

if ($limiter->tooManyAttempts()) {
    echo "Too many requests. Please try again later.";
    return;
}

$limiter->hit();
echo "Request allowed.";
```

### Constructor Parameters

[](#constructor-parameters)

The `RateLimiter` constructor accepts four parameters:

1. **Key (string)** - A unique identifier for the rate limit (e.g., user ID or IP address).
2. **Max Attempts (int)** - The maximum number of allowed requests within the decay period.
3. **Decay Seconds (int)** - The time window (in seconds) before the attempt count resets.
4. **Storage (StorageInterface, optional)** - The storage mechanism to persist request attempts. If not provided, it defaults to `FileStorage`.

### Using Redis Storage

[](#using-redis-storage)

```
use AgusSuroyo\RateLimiter\RateLimiter;
use AgusSuroyo\RateLimiter\Storage\RedisStorage;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$storage = new RedisStorage($redis);

$limiter = new RateLimiter('guest', 5, 60, $storage);

if ($limiter->tooManyAttempts()) {
    echo "Too many requests. Please try again later.";
    return;
}

$limiter->hit();
echo "Request allowed.";
```

### Clearing Rate Limit

[](#clearing-rate-limit)

```
$limiter->clear();
```

### Creating a Custom Storage

[](#creating-a-custom-storage)

To implement a custom storage, create a class that implements `StorageInterface`:

```
use AgusSuroyo\RateLimiter\Contracts\StorageInterface;

class CustomStorage implements StorageInterface
{
    private array $data = [];

    public function getAttempts(string $key): array
    {
        return $this->data[$key] ?? [];
    }

    public function saveAttempts(string $key, array $attempts, int $decaySeconds): void
    {
        $this->data[$key] = $attempts;
    }

    public function clear(string $key): void
    {
        unset($this->data[$key]);
    }
}

$customStorage = new CustomStorage();
$limiter = new RateLimiter('guest', 5, 60, $customStorage);
```

Testing
-------

[](#testing)

Run unit tests using PHPUnit:

```
vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please submit a pull request or open an issue.

License
-------

[](#license)

This package is open-source software licensed under the [MIT license](LICENSE).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance48

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 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

415d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2c4ff9c71f1c9fa742c2c852482bb72be37a5f32768434b8f4ce2a828e8fe41d?d=identicon)[agussuroyo](/maintainers/agussuroyo)

---

Top Contributors

[![agussuroyo](https://avatars.githubusercontent.com/u/5734710?v=4)](https://github.com/agussuroyo "agussuroyo (8 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/agussuroyo-rate-limiter/health.svg)

```
[![Health](https://phpackages.com/badges/agussuroyo-rate-limiter/health.svg)](https://phpackages.com/packages/agussuroyo-rate-limiter)
```

PHPackages © 2026

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