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

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

bentools/guzzle-throttle-middleware
===================================

A GuzzleHTTP Middleware that can delay requests before sending them.

0.4.1(8y ago)15256.9k↓19.6%9[1 issues](https://github.com/bpolaszek/guzzle-throttle-middleware/issues)MITPHPPHP &gt;=7.0

Since Jul 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/bpolaszek/guzzle-throttle-middleware)[ Packagist](https://packagist.org/packages/bentools/guzzle-throttle-middleware)[ RSS](/packages/bentools-guzzle-throttle-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (10)Versions (7)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/903368606299d4751cf68ca80286ae49449363caf90e2f3636ceb95152731b79/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f762f737461626c65)](https://packagist.org/packages/bentools/guzzle-throttle-middleware)[![License](https://camo.githubusercontent.com/bd747ab84f67b4fc147ca74216d174553f4b26be95b9caf436c57959dc7ac81c/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f6c6963656e7365)](https://packagist.org/packages/bentools/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/88f82bb9a63f127a58875856ff3d09013558a2e164e9696f2b427d88962aa6c2/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f67757a7a6c652d7468726f74746c652d6d6964646c65776172652f646f776e6c6f616473)](https://packagist.org/packages/bentools/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 bentools/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

35

—

LowBetter than 80% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~44 days

Recently: every ~55 days

Total

6

Last Release

3013d ago

PHP version history (2 changes)0.4PHP &gt;=7.1

0.4.1PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/acdd1a8ee0e657ddd06cf11f98a32100ef7121afb8aa270a5b295f5c29c038b3?d=identicon)[bpolaszek](/maintainers/bpolaszek)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[akamai-open/edgegrid-client

Implements the Akamai {OPEN} EdgeGrid Authentication specified by https://developer.akamai.com/introduction/Client\_Auth.html

482.5M6](/packages/akamai-open-edgegrid-client)[api-platform/metadata

API Resource-oriented metadata attributes and factories

243.5M96](/packages/api-platform-metadata)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mimmi20/browser-detector

Library to detect Browsers and Devices

48153.5k3](/packages/mimmi20-browser-detector)[discord-php/http

Handles HTTP requests to Discord servers

25318.7k8](/packages/discord-php-http)

PHPackages © 2026

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