twinh/rate-limiter
最新稳定版本:v1.0.0
Composer 安装命令:
composer require twinh/rate-limiter
包简介
A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.
关键字:
README 文档
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\StorageInterfaceinterface 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
StorageInterfacefor the rate limiter class. - Implement storage object.
Class Diagram
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2024-10-23