PHPackages                             yaknet/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. [Security](/categories/security)
4. /
5. yaknet/rate-limiter

ActiveLibrary[Security](/categories/security)

yaknet/rate-limiter
===================

A high-performance, zero-dependency, pure PHP rate limiting component supporting Token Bucket and Sliding Window algorithms.

v1.1.3(2w ago)07MITPHPPHP &gt;=8.2

Since May 30Pushed 2w agoCompare

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

READMEChangelogDependencies (6)Versions (6)Used By (0)

YakNet Rate Limiter Component
=============================

[](#yaknet-rate-limiter-component)

[![PHP Version Support](https://camo.githubusercontent.com/79ac6c4536a3331e3d827319dd782adf525bdd6289ee5748a7023086c9ed0930/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230382e322d626c75652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yaknet/rate-limiter)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![PHPStan Analysis](https://camo.githubusercontent.com/95ad671a386caa3fc96a820cfdd0797d0094138dddcf57ce117e8cf8a568bc81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c25323035253230636c65616e2d707572706c652e7376673f7374796c653d666c61742d737175617265)](https://phpstan.org/)[![Tests Status](https://camo.githubusercontent.com/937e105fc83966d970d459cb02a1c3da1ba44f5e9522992176e869fce65b8a52/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d70617373696e672d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://phpunit.de/)

A high-performance, zero-dependency, pure PHP rate limiting component. Supports standard rate-limiting algorithms and pluggable storage backends (vital for both persistent processes like WebSockets and shared-nothing environments like PHP-FPM).

---

Features
--------

[](#features)

- **⚡ Zero External Production Dependencies:** Extremely lightweight, fast, and easy to embed.
- **🛡️ Race-Condition Free:** Leverages unique transactional read-modify-write logic using exclusive locks (`flock`) on files and optimistic concurrency controls (`WATCH`/`MULTI`/`EXEC`) on Redis.
- **🔄 Pluggable Storage Adapters:**
    - `InMemoryStorage`: RAM-based array. Extremely fast, perfect for tests and long-running services (e.g. WebSocket servers).
    - `FileStorage`: Local disk-based storage with robust file-locking. Works out of the box on *any* server (shared hosting, basic VPS) with zero external setup or engines.
    - `RedisStorage`: Scalable storage compatible with both native PHP `Redis` extension (Phpredis) and userland `Predis` client library.
- **📈 Advanced Algorithms:**
    - **Token Bucket:** Allows quick bursts up to a capacity limit and refills tokens continuously based on high-resolution elapsed time.
    - **Sliding Window:** Tracks logs in a rolling sliding window, preventing boundaries/fixed-window request resets.

---

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

[](#installation)

Add this package to your project using Composer (ensure your local repository mapping is configured):

```
composer require yaknet/rate-limiter
```

---

Quick Start
-----------

[](#quick-start)

### 1. Token Bucket Limiter (Using Zero-Dependency FileStorage)

[](#1-token-bucket-limiter-using-zero-dependency-filestorage)

Great for allowing bursts of traffic while ensuring a steady continuous refill rate:

```
