twinh / rate-limiter
There is no license information available for the latest version (v1.0.0) of this package.
A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.
v1.0.0
2024-10-23 01:30 UTC
Requires
- php: >=8.0
- ext-redis: *
Requires (Dev)
- miaoxing/dev: ^9.2
- wei/wei: ^0.17.8
README
A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.
Getting started
use RateLimiter\FixedWindow; use RateLimiter\FixedWindow\MemoryStorage; $limiter = new FixedWindow(new MemoryStorage(), windowSize: 10, limit: 1); if (!$limiter->attempt('user_id')) { echo 'Too many requests'; }
Feature
- ✨ Supports multiple strategies:
FixedWindow
,SlidingWindow
,LeakyBucket
, andTokenBucket
(not yet). - 🖥️ Compatible with
Redis
,Memory
, andDatabase
(not yet) storage methods. - 💪 Robust and scalable architecture with atomic operations for high traffic and large user bases.
- ⚙️ Flexible rate limit and window size configuration.
- 🚪 Clear API for attempting, getting remaining attempts, and clearing rate limits.
- 🌐 Supports global, and user-specific rate limits.
Install
composer require twinh/rate-limiter
Document
Initialize the FixedWindow Rate Limiter
use RateLimiter\FixedWindow; use RateLimiter\FixedWindow\MemoryStorage; $limiter = new FixedWindow(new MemoryStorage(), windowSize: 60, limit: 1);
Initialize the SlidingWindow Rate Limiter
use RateLimiter\SlidingWindow; use RateLimiter\SlidingWindow\MemoryStorage; $limiter = new SlidingWindow(new MemoryStorage(), windowSize: 60, limit: 1);
Initialize the LeakyBucket Rate Limiter
use RateLimiter\LeakyBucket; use RateLimiter\LeakyBucket\MemoryStorage; $limiter = new LeakyBucket(new LeakyMemoryStorage(), rate: 10, limit: 1);
Set Redis Storage
use RateLimiter\FixedWindow; use RateLimiter\FixedWindow\RedisStorage; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $limiter = new FixedWindow(new RedisStorage($redis), windowSize: 10, limit: 1);
Attempts to execute a rate limited operation
if (!$limiter->attempt('user_id')) { echo 'Too many requests'; }
Attempts to execute a rate limited operation with global scope
$limiter->attempt();
Attempts to execute a rate limited operation with client IP
$limiter->attempt($_SERVER['REMOTE_ADDR'] ?? 'unknown');
Attempts to execute a rate limited operation with server IP
$limiter->attempt($_SERVER['SERVER_ADDR'] ?? 'unknown');
Get the remaining attempts
$remaining = $limiter->getRemaining('user_id');
Clear the request limit
$limiter->clear('user_id');
Extend more storages and rate limiters
Add Storage
- Create a new class that implements the
RateLimiter\Xxx\StorageInterface
interface of the rate limiter. - Implement the methods of the storage class.
- Create the rate limiter object with the new storage object.
Add Rate Limiter
- Create a new class that implements the
RateLimiter\RateLimiterInterface
. - Implement the methods of the rate limiter class.
- Create a
StorageInterface
for the rate limiter class. - Implement storage object.