PHPackages                             farit-slv/guzzle-throttle-middleware - 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. farit-slv/guzzle-throttle-middleware

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

farit-slv/guzzle-throttle-middleware
====================================

A GuzzleHTTP Middleware that can delay requests before sending them.

0.5.1(2y ago)0194↓77.8%MITPHPPHP &gt;=7.0

Since Feb 4Pushed 2y agoCompare

[ Source](https://github.com/FaritSlv/guzzle-throttle-middleware)[ Packagist](https://packagist.org/packages/farit-slv/guzzle-throttle-middleware)[ RSS](/packages/farit-slv-guzzle-throttle-middleware/feed)WikiDiscussions master Synced 3w ago

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

[![Latest Stable Version](https://camo.githubusercontent.com/92e4f6c36265a85e6077313fe23fe45de3c0869ddd6dbe3806e7cecd6f2cdf22/68747470733a2f2f706f7365722e707567782e6f72672f66617269742d736c762f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f762f737461626c65)](https://packagist.org/packages/farit-slv/guzzle-throttle-middleware)[![License](https://camo.githubusercontent.com/c7587eb7b7a411f71ff50ef89a6bc926ebccf0c1fab13fdea850a73a6500550d/68747470733a2f2f706f7365722e707567782e6f72672f66617269742d736c762f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f6c6963656e7365)](https://packagist.org/packages/farit-slv/guzzle-throttle-middleware)[![Build Status](https://camo.githubusercontent.com/423e658805781cc7fa11ecd4c6cb1337e7db18a9c72202e6464ab38d178b5bda/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62706f6c61737a656b2f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/bpolaszek/guzzle-throttle-middleware)[![Coverage Status](https://camo.githubusercontent.com/70f167971d6ff25a58a31b7c904d883ad7d2b1ce5b33abfe2811bd44e122094d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f62706f6c61737a656b2f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/bpolaszek/guzzle-throttle-middleware?branch=master)[![Quality Score](https://camo.githubusercontent.com/efe8bd53db6bf36b854d3b68871207ee6f5f865da846ec3675a31c5ed135f07d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f62706f6c61737a656b2f67757a7a6c652d7468726f74746c652d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bpolaszek/guzzle-throttle-middleware)[![Total Downloads](https://camo.githubusercontent.com/fc4a26b05e056eaf1b3f3c638da0c627456c499c7c7e44e43d4fa02a668b5bf1/68747470733a2f2f706f7365722e707567782e6f72672f66617269742d736c762f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f646f776e6c6f616473)](https://packagist.org/packages/farit-slv/guzzle-throttle-middleware)

Guzzle Throttle Middleware
==========================

[](#guzzle-throttle-middleware)

This middleware adds throttling capabilities to your [Guzzle](https://github.com/guzzle/guzzle) client.

This can be useful when some hosts limits your number of requests per second / per minute.

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

[](#installation)

> composer require farit-slv/guzzle-throttle-middleware

Counter storage
---------------

[](#counter-storage)

By default, request counters are stored in an array.

But you can use the `PSR6Adapter` to store your counters within a [psr/cache](http://www.php-fig.org/psr/psr-6/) implementation, such as [symfony/cache](https://symfony.com/doc/current/components/cache.html), and use shared storages like Redis, APCu, Memcached, ...

Usage
-----

[](#usage)

For this middleware to work, you need to register some configurations.

A configuration is composed of:

- A Request matcher (to trigger or not the throttler, depending on the request content)
- A maximum number of requests
- The period, in seconds, during which the maximum number of requests apply.
- A storage key.

You can register as many configurations as you need. The 1st request matcher wins.

Example
-------

[](#example)

```
namespace App\RequestMatcher;

use BenTools\Psr7\RequestMatcherInterface;
use Psr\Http\Message\RequestInterface;

class ExampleOrgRequestMatcher implements RequestMatcherInterface
{
    /**
     * @inheritDoc
     */
    public function matchRequest(RequestInterface $request)
    {
        return false !== strpos($request->getUri()->getHost(), 'example.org');
    }
}
```

```
use App\RequestMatcher\ExampleOrgRequestMatcher;
use FaritSlv\GuzzleHttp\Middleware\Storage\Adapter\ArrayAdapter;
use FaritSlv\GuzzleHttp\Middleware\ThrottleConfiguration;
use FaritSlv\GuzzleHttp\Middleware\ThrottleMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

require_once __DIR__ . '/vendor/autoload.php';

$stack = HandlerStack::create();
$middleware = new ThrottleMiddleware(new ArrayAdapter());

// Max 1 request per second
$maxRequests = 1;
$durationInSeconds = 1;
$middleware->registerConfiguration(
    new ThrottleConfiguration(new ExampleOrgRequestMatcher(), $maxRequests, $durationInSeconds, 'example')
);

$stack->push($middleware, 'throttle');
$client = new Client([
    'handler' => $stack,
]);

$client->get('http://www.example.org'); // Will be executed immediately
$client->get('http://www.example.org'); // Will be executed in 1 second
```

Tests
-----

[](#tests)

> ./vendor/bin/phpunit

Known issues
------------

[](#known-issues)

Due to PHP's synchronous behaviour, remember that throttling means calling `sleep()` or `usleep()` functions, which will delay your entire script, and not only the current request.

This means throttling will also block Guzzle's asynchronous requests when using `CurlMultiHandler`.

To prevent this, you may have a look at [bentools/guzzle-queue-handler](https://github.com/bpolaszek/guzzle-queue-handler), a handler that delegates asynchronous requests to PHP workers (Beanstalk, RabbitMQ, Redis, ...).

You can then enable throttling only on workers.

See also
--------

[](#see-also)

- [bentools/guzzle-queue-handler](https://github.com/bpolaszek/guzzle-queue-handler) - A queue handler to process Guzzle 6+ requests within a work queue.
- [kevinrob/guzzle-cache-middleware](https://github.com/Kevinrob/guzzle-cache-middleware) - A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.
- [bentools/guzzle-duration-middleware](https://github.com/bpolaszek/guzzle-duration-middleware) - A Guzzle 6+ Middleware that adds a X-Request-Duration header to all responses to monitor response times.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 Bus Factor1

Top contributor holds 57.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

873d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/69817b83d44336101a05c78d064b8dbaa2c81c268799224620bcd7883a364d6c?d=identicon)[Farit](/maintainers/Farit)

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (8 commits)")[![FaritSlv](https://avatars.githubusercontent.com/u/7736725?v=4)](https://github.com/FaritSlv "FaritSlv (6 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/farit-slv-guzzle-throttle-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/farit-slv-guzzle-throttle-middleware/health.svg)](https://phpackages.com/packages/farit-slv-guzzle-throttle-middleware)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k532.1M19.4k](/packages/laravel-framework)[symfony/http-kernel

Provides a structured process for converting a Request into a Response

8.1k853.6M8.3k](/packages/symfony-http-kernel)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

19564.8M1.6k](/packages/drupal-core)[avalara/avataxclient

Client library for Avalara's AvaTax suite of business tax calculation and processing services. Uses the REST v2 API.

528.3M7](/packages/avalara-avataxclient)[api-platform/metadata

API Resource-oriented metadata attributes and factories

244.5M182](/packages/api-platform-metadata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
