PHPackages                             asiadevmedia/guzzle-rate-limiter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. asiadevmedia/guzzle-rate-limiter

ActiveLibrary[HTTP &amp; Networking](/categories/http)

asiadevmedia/guzzle-rate-limiter
================================

A rate limiter for Guzzle

v1.0.0(1y ago)09MITPHPPHP ^7.1|^8.0

Since Sep 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/asiadevmedia/guzzle-rate-limiter)[ Packagist](https://packagist.org/packages/asiadevmedia/guzzle-rate-limiter)[ Docs](https://github.com/spatie/guzzle-rate-limiter-middleware)[ RSS](/packages/asiadevmedia-guzzle-rate-limiter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

A rate limiter for Guzzle
=========================

[](#a-rate-limiter-for-guzzle)

[![Latest Version on Packagist](https://camo.githubusercontent.com/82af71d39005c75fc24f2ef318f938d504e4067bf66f8d337ed2b7e22f2b5c43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f617369616465766d656469612f67757a7a6c652d726174652d6c696d697465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/asiadevmedia/guzzle-rate-limiter)[![Quality Score](https://camo.githubusercontent.com/8ed1105ecb198e2bfc7648440c6406dfbed74b6bdc7690acf7a09df11bd9c982/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f617369616465766d656469612f67757a7a6c652d726174652d6c696d697465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/asiadevmedia/guzzle-rate-limiter)[![StyleCI](https://camo.githubusercontent.com/a0e366ad37446f9513aafb051e212c48c1b0969c1884bfc26c5161d26c27988a/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3835363435333335312f736869656c643f6272616e63683d6d61696e)](https://github.styleci.io/repos/856453351)[![Total Downloads](https://camo.githubusercontent.com/205b264a6bcceb8453f3fa505967f4b6b5c075033e5534be13db68e2c11b3228/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617369616465766d656469612f67757a7a6c652d726174652d6c696d697465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/asiadevmedia/guzzle-rate-limiter)

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`.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require asiadevmedia/guzzle-rate-limiter
```

Usage
-----

[](#usage)

Create a [Guzzle middleware stack](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) and register it on the client.

```
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Asiadevmedia\GuzzleRateLimiter\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.

```
RateLimiterMiddleware::perSecond(3); // Max. 3 requests per second

RateLimiterMiddleware::perMinute(5); // Max. 5 requests per minute
```

Custom stores
-------------

[](#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.

```
use MyApp\RateLimiterStore;
use Asiadevmedia\GuzzleRateLimiter\RateLimit;

RateLimiterMiddleware::perSecond(3, new RateLimiterStore());
```

A Laravel example of a custom `Store`:

```
