bm2ilabs/rate-limiter
Composer 安装命令:
composer require bm2ilabs/rate-limiter
包简介
Multi-period rate limiting for Laravel applications with minute, hour, and day limits
README 文档
README
A comprehensive Laravel package for implementing multi-period rate limiting in your application. This package allows you to easily set and enforce rate limits for different time intervals (minute, hour, day) and automatically includes these limits in your API response headers.
Features
- Rate limiting for multiple time periods (minute, hour, day)
- Custom response headers for each period
- Configurable limits through environment variables or config file
- Easy to use middleware
- Support for different identifiers (IP, authenticated user, custom parameter)
- Compatible with Laravel 8.x, 9.x, 10.x, and 11.x
Installation
You can install the package via composer:
composer require amineware/rate-limiter
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="Amineware\RateLimiter\RateLimiterServiceProvider"
This will create a rate-limiter.php file in your config directory. You can customize the rate limits and other settings in this file.
Basic Usage
Add the middleware to your routes:
// In routes/api.php Route::middleware('multi-rate-limit')->group(function () { // Your API routes here });
Or apply it to specific routes:
Route::get('/endpoint', 'YourController@method')->middleware('multi-rate-limit');
Response Headers
When using this middleware, your API responses will include the following headers:
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
X-RateLimit-Limit-Hour: 1000
X-RateLimit-Remaining-Hour: 999
X-RateLimit-Limit-Day: 10000
X-RateLimit-Remaining-Day: 9999
If a rate limit is exceeded, the response will include:
Retry-After: <seconds until reset>
X-RateLimit-Reset: <timestamp>
Advanced Usage
Using the Facade
You can use the facade to manually check or manipulate rate limits:
use Amineware\RateLimiter\Facades\MultiPeriodRateLimiter; // Check remaining attempts $remaining = MultiPeriodRateLimiter::remaining('minute', $userId); // Check if too many attempts if (MultiPeriodRateLimiter::tooManyAttempts('hour', $userId, 100)) { // Handle rate limit exceeded } // Increment the counter MultiPeriodRateLimiter::hit('day', $userId); // Clear rate limits for a key MultiPeriodRateLimiter::clear('minute', $userId);
Custom Identifiers
By default, the package uses the authenticated user ID or IP address as the identifier. You can customize this in the config file:
// In config/rate-limiter.php 'identifiers' => [ 'default' => 'custom', 'custom_parameter' => 'api_key', ],
Environment Variables
You can set rate limits through environment variables:
RATE_LIMIT_PER_MINUTE=60
RATE_LIMIT_PER_HOUR=1000
RATE_LIMIT_PER_DAY=10000
License
The MIT License (MIT). Please see License File for more information.
bm2ilabs/rate-limiter 适用场景与选型建议
bm2ilabs/rate-limiter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bm2ilabs/rate-limiter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bm2ilabs/rate-limiter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-07