https://github.com/spatie/guzzle-rate-limiter-middleware
A rate limiter middleware for Guzzle
https://github.com/spatie/guzzle-rate-limiter-middleware
api guzzle http rate-limits
Last synced: 21 days ago
JSON representation
A rate limiter middleware for Guzzle
- Host: GitHub
- URL: https://github.com/spatie/guzzle-rate-limiter-middleware
- Owner: spatie
- License: mit
- Created: 2019-01-14T16:23:42.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-12-02T08:42:40.000Z (5 months ago)
- Last Synced: 2025-04-03T03:42:39.297Z (28 days ago)
- Topics: api, guzzle, http, rate-limits
- Language: PHP
- Homepage: https://spatie.be/open-source
- Size: 49.8 KB
- Stars: 156
- Watchers: 8
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
[
](https://supportukrainenow.org)
# A rate limiter middleware for Guzzle
[](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware)
[](https://travis-ci.org/spatie/guzzle-rate-limiter-middleware)
[](https://scrutinizer-ci.com/g/spatie/guzzle-rate-limiter-middleware)
[](https://github.styleci.io/repos/165693657)
[](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware)A rate limiter middleware for Guzzle. 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 `sleep` until 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`.## Support us
[
](https://spatie.be/github-ad-click/guzzle-rate-limiter-middleware)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
## Installation
You can install the package via composer:
```bash
composer require spatie/guzzle-rate-limiter-middleware
```## Usage
Create a [Guzzle middleware stack](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) and register it on the client.
```php
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;$stack = HandlerStack::create();
$stack->push(RateLimiterMiddleware::perSecond(3));$client = new Client([
'handler' => $stack,
]);
```You can create a rate limiter to limit per second or per minute.
```php
RateLimiterMiddleware::perSecond(3); // Max. 3 requests per secondRateLimiterMiddleware::perMinute(5); // Max. 5 requests per minute
```## Custom stores
By default, the rate limiter works in memory. This means that if you have a second PHP process (or Guzzle client) consuming the same API, you'd still possibly hit the rate limit. To work around this issue, the rate limiter's state should be persisted to a cache. Implement the `Store` interface with your own cache, and pass the store to the rate limiter.
```php
use MyApp\RateLimiterStore;
use Spatie\GuzzleRateLimiterMiddleware\RateLimit;RateLimiterMiddleware::perSecond(3, new RateLimiterStore());
```A Laravel example of a custom `Store`:
```php
get(), [$timestamp]));
}
}
```### Testing
``` bash
composer test
```### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
### Security
If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.
## Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).
## Credits
- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.