leonardohipolito/laravel-http-client-rate-limiter-middleware
最新稳定版本:0.0.3
Composer 安装命令:
composer require leonardohipolito/laravel-http-client-rate-limiter-middleware
包简介
This is my package laravel-http-client-rate-limiter-middleware
README 文档
README
A rate limiter middleware for Laravel Http Client. Here's what you need to know:
- Specify a maximum amount of requests per minute or per second
- When the limit is reached, the process will
sleepuntil the request can be made - Implement your own driver to persist the rate limiter's request store. This is necessary if the rate limiter needs to
work across separate processes, the package ships with an
InMemoryStore.
Installation
You can install the package via composer:
composer require leonardohipolito/laravel-http-client-rate-limiter-middleware
Usage
Laravel 9
use GuzzleHttp\Middleware; use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\RateLimiterMiddleware; use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\CacheStore; Http::macro( 'jsonPlaceholder', fn () => Http::baseUrl('https://jsonplaceholder.typicode.com') ->withMiddleware(Middleware::mapRequest(RateLimiterMiddleware::perMinute(60, new CacheStore('jsonplaceholder-rate-limit')))) ->acceptJson() ->asJson() );
Laravel 10
use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\RateLimiterMiddleware; use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\CacheStore; Http::macro( 'jsonPlaceholder', fn () => Http::baseUrl('https://jsonplaceholder.typicode.com') ->withRequestMiddleware(RateLimiterMiddleware::perMinute(60, new CacheStore('jsonplaceholder-rate-limit') ->acceptJson() ->asJson() );
You can create a rate limiter to limit per second or per minute.
use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\RateLimiterMiddleware; RateLimiterMiddleware::perSecond(5); RateLimiterMiddleware::perMinute(60);
Custom stores
By default, the rate limiter works in Cache. This means that if you have a second PHP process but you can create your own store.
use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\RateLimiterMiddleware; use MyApp\RateLimiterStore; use \LeonardoHipolito\LaravelHttpClientRateLimiterMiddleware\RateLimit; RateLimiterMiddleware::perSecond(3, new RateLimiterStore());
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-01