Download the PHP package four-bytes/four-rate-limiting without Composer
On this page you can find all versions of the php package four-bytes/four-rate-limiting. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download four-bytes/four-rate-limiting
More information about four-bytes/four-rate-limiting
Files in four-bytes/four-rate-limiting
Package four-rate-limiting
Short Description Generic rate limiting library for PHP APIs — Token Bucket, Fixed Window, Sliding Window, Leaky Bucket
License MIT
Informations about the package four-rate-limiting
four-rate-limiting
Generic rate limiting library for PHP 8.4+. Four algorithms, header-based dynamic tracking, state persistence, PSR-16 cache backend.
Philosophy
This library provides no API-specific presets. Every API client knows its own rate limit rules best — configuration belongs in the consuming client, not in a generic library.
Installation
Quick Start
Algorithms
| Algorithm | Constant | When to use |
|---|---|---|
| Token Bucket | ALGORITHM_TOKEN_BUCKET |
Burst allowed, smooth average |
| Fixed Window | ALGORITHM_FIXED_WINDOW |
Fixed time window (e.g. per minute) |
| Sliding Window | ALGORITHM_SLIDING_WINDOW |
Rolling window (e.g. QPD without midnight reset) |
| Leaky Bucket | ALGORITHM_LEAKY_BUCKET |
Steady throughput, no burst |
Configuration
Interface
Advanced Usage
createCustom
Dynamic Header Tracking
Rate Limit Status
The getTypedStatus() method returns a typed DTO with all relevant information:
PSR-16 Cache Backend
The library supports any PSR-16 (Psr\SimpleCache\CacheInterface) implementation for distributed rate limiting (Redis, APCu, Memcached, etc.).
HTTP Client Integration
RateLimitMiddleware encapsulates the complete rate limiting logic for HTTP clients:
- Pre-request: consume token via
waitForAllowed() - Post-response: synchronize header state via
updateFromHeaders() - 429 retry: exponential backoff with configurable retries
PSR-7 Header Compatibility
The library automatically normalizes PSR-7 headers (array<string, string[]> → array<string, string>).
$response->getHeaders() can be passed directly to updateFromHeaders().
Architecture
Tests
License
MIT License. See LICENSE.
All versions of four-rate-limiting with dependencies
psr/http-message Version ^2.0
psr/log Version ^3.0
psr/simple-cache Version ^3.0