maatify / rate-limiter
Flexible PSR-15 rate limiter supporting Redis, MongoDB, and MySQL via real and fake drivers. Provides multi-strategy algorithms, burst control, sliding window, token bucket, and integration with Maatify Data Repository.
Installs: 157
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 1
Open Issues: 0
pkg:composer/maatify/rate-limiter
Requires
- php: >=8.4
- maatify/psr-logger: ^1.0
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- doctrine/dbal: ^4.3
- friendsofphp/php-cs-fixer: ^3.60
- maatify/data-fakes: ^1.0
- mongodb/mongodb: ^2.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
- predis/predis: ^2.3
Suggests
- ext-mongodb: Required for MongoAdapter.
- ext-pdo: Required for MySQLAdapter (PDO driver).
- ext-redis: High-performance RedisAdapter.
- doctrine/dbal: Optional advanced SQL driver for MySQLDbalAdapter.
- maatify/data-adapters: Real adapter support for Redis, MongoDB, MySQL drivers.
- maatify/data-fakes: Test the rate limiter using deterministic in-memory fake drivers.
- maatify/data-repository: Use repository abstraction across Redis, MySQL, Mongo with FakeAdapters support.
- mongodb/mongodb: Needed if using MongoAdapter.
- predis/predis: Redis fallback when ext-redis is unavailable.
This package is auto-updated.
Last update: 2025-12-08 07:11:17 UTC
README
PSR-compliant distributed rate limiting engine supporting Redis, MongoDB, and MySQL with adaptive exponential backoff.
๐ Overview
Maatify Rate Limiter is a fully decoupled, PSR-compliant rate-limiting engine designed for:
- Native PHP
- Slim Framework
- Laravel Middleware
- Custom API Gateways
It provides distributed rate-limiting with adaptive exponential backoff, unified across:
- Redis
- MongoDB
- MySQL
Why this library?
- Zero storage lock-in
- Unified attempt/status/reset API
- Global per-IP rate limit
- Adaptive Exponential Backoff (2โฟ)
- Full PSR-7 / PSR-15 middleware compatibility
- PHPStan Level Max ready
โ Supported Drivers
| Backend | Driver Type | Use Case |
|---|---|---|
| Redis | Real Driver | High-performance in-memory limits |
| MongoDB | Real Driver | Distributed analytics |
| MySQL | Real Driver | Persistent audit & compliance |
๐ฆ Installation
composer require maatify/rate-limiter
โก Quick Usage
use Maatify\RateLimiter\Resolver\RateLimiterResolver; use Maatify\RateLimiter\Enums\RateLimitActionEnum; use Maatify\RateLimiter\Enums\PlatformEnum; $resolver = new RateLimiterResolver(['driver' => 'redis']); $limiter = $resolver->resolve(); $status = $limiter->attempt( '127.0.0.1', RateLimitActionEnum::LOGIN, PlatformEnum::WEB ); echo $status->remaining;
๐ Full usage examples (Native, Slim, Laravel, API, Enums, Backoff): โก๏ธ examples/Examples.md
๐งฉ Key Features
- Unified API:
attempt(),status(),reset() - Global Per-IP Limit
- Adaptive Exponential Backoff
- DTO-based Response Model
- PSR-7 / PSR-15 Middleware Ready
- Custom Enum Contracts
- Driver Resolver
- Strict Validation & Type Safety
- PHPStan Level Max
๐ Documentation
๐ Development History & Phase Details
- Phase 1 โ Environment Setup
- Phase 2 โ Core Architecture
- Phase 3 โ Storage Drivers
- Phase 3.1 โ Enum Contracts Refactor
- Phase 4 โ Resolver & Middleware
- Phase 4.1 โ Continuous Integration
- Phase 5 โ Exponential Backoff & Global Rate Limit
๐งฑ Dependencies Overview
maatify/rate-limiter relies on PSR standards and selected open-source libraries.
๐ Direct Open-Source Dependencies
| Library | Purpose |
|---|---|
| psr/http-message | HTTP message interfaces |
| psr/http-server-middleware | PSR-15 middleware |
| psr/http-server-handler | Request handler |
| redis / predis | Redis driver |
| mongodb/mongodb | MongoDB driver |
| phpunit/phpunit | Testing |
| phpstan/phpstan | Static analysis |
๐งช Testing
composer test
Runs:
- Driver consistency tests
- Resolver tests
- Middleware header tests
- Backoff & global limit tests
- Coverage reporting
๐ชช License
MIT License ยฉ Maatify.dev โ Free to use, modify, and distribute with attribution.
๐ค Author
Engineered by Mohamed Abdulalim (@megyptm)
Backend Lead & Technical Architect โ https://www.maatify.dev
๐ค Contributors
Special thanks to the Maatify.dev engineering team and all open-source contributors. Your efforts help make this library stable, secure, and production-ready.
Before opening a Pull Request, please read:
Built with โค๏ธ by Maatify.dev โ Unified Ecosystem for Modern PHP Libraries