Download the PHP package yaknet/rate-limiter without Composer
On this page you can find all versions of the php package yaknet/rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yaknet/rate-limiter
More information about yaknet/rate-limiter
Files in yaknet/rate-limiter
Package rate-limiter
Short Description A high-performance, zero-dependency, pure PHP rate limiting component supporting Token Bucket and Sliding Window algorithms.
License MIT
Informations about the package rate-limiter
YakNet Rate Limiter Component
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
- ⚡ 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 PHPRedisextension (Phpredis) and userlandPredisclient 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
Add this package to your project using Composer (ensure your local repository mapping is configured):
Quick Start
1. Token Bucket Limiter (Using Zero-Dependency FileStorage)
Great for allowing bursts of traffic while ensuring a steady continuous refill rate:
2. Sliding Window Limiter (Using Fast InMemoryStorage)
Perfect for sliding-log tracking inside persistent processes (e.g. your WebSocket loops):
3. Distributed Redis Storage Setup
Compatible with both Phpredis and Predis:
Verification & Testing
Verify that all algorithms and lock contention structures pass successfully:
Perform static analysis with PHPStan:
License
This project is licensed under the MIT License - see the LICENSE file for details.