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

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

diablomedia/guzzle-throttle-middleware
======================================

A GuzzleHTTP Middleware that can delay requests before sending them.

1.0.2(1y ago)0969[1 PRs](https://github.com/diablomedia/guzzle-throttle-middleware/pulls)MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0CI failing

Since Apr 24Pushed 1mo agoCompare

[ Source](https://github.com/diablomedia/guzzle-throttle-middleware)[ Packagist](https://packagist.org/packages/diablomedia/guzzle-throttle-middleware)[ RSS](/packages/diablomedia-guzzle-throttle-middleware/feed)WikiDiscussions main Synced today

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

[![Latest Stable Version](https://camo.githubusercontent.com/9af9310c2f9924501c5e00df9ab23e32057301184ad6ac6c979791f39cc77767/68747470733a2f2f706f7365722e707567782e6f72672f646961626c6f6d656469612f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f762f737461626c65)](https://packagist.org/packages/diablomedia/guzzle-throttle-middleware)[![License](https://camo.githubusercontent.com/b7082888fe1efa6bdc889b51a37831589079d2247231a2f52e0ff20a43815bf7/68747470733a2f2f706f7365722e707567782e6f72672f646961626c6f6d656469612f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f6c6963656e7365)](https://packagist.org/packages/diablomedia/guzzle-throttle-middleware)[![Build](https://github.com/diablomedia/guzzle-throttle-middleware/actions/workflows/build.yml/badge.svg)](https://github.com/diablomedia/guzzle-throttle-middleware/actions/workflows/build.yml)[![Total Downloads](https://camo.githubusercontent.com/1da3b5bc816c70978dfd839c264e57256d258304b2c15bde9b9ef567f56dcc4e/68747470733a2f2f706f7365722e707567782e6f72672f646961626c6f6d656469612f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f646f776e6c6f616473)](https://packagist.org/packages/diablomedia/guzzle-throttle-middleware)[![codecov](https://camo.githubusercontent.com/01c6785a13b7a511c7d819b2098825e8afe26f77053e215a69098442fd2bf44a/68747470733a2f2f636f6465636f762e696f2f6769746875622f646961626c6f6d656469612f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f67726170682f62616467652e7376673f746f6b656e3d35564b756e6a49686c6c)](https://codecov.io/github/diablomedia/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 diablomedia/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 BenTools\GuzzleHttp\Middleware\Storage\Adapter\ArrayAdapter;
use BenTools\GuzzleHttp\Middleware\ThrottleConfiguration;
use BenTools\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

43

—

FairBetter than 89% of packages

Maintenance72

Regular maintenance activity

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 57.9% 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 ~197 days

Total

3

Last Release

404d ago

PHP version history (2 changes)1.0.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

1.0.2PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/136907?v=4)[Jay Klehr](/maintainers/jaydiablo)[@jaydiablo](https://github.com/jaydiablo)

---

Top Contributors

[![jaydiablo](https://avatars.githubusercontent.com/u/136907?v=4)](https://github.com/jaydiablo "jaydiablo (11 commits)")[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (8 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M19.9k](/packages/laravel-framework)[symfony/http-kernel

Provides a structured process for converting a Request into a Response

8.1k869.4M8.7k](/packages/symfony-http-kernel)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k14](/packages/tempest-framework)[drupal/core

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

21866.0M1.7k](/packages/drupal-core)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6942.5M415](/packages/drupal-core-recommended)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k40](/packages/civicrm-civicrm-core)

PHPackages © 2026

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