Download the PHP package bentools/guzzle-throttle-middleware without Composer
On this page you can find all versions of the php package bentools/guzzle-throttle-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bentools/guzzle-throttle-middleware
More information about bentools/guzzle-throttle-middleware
Files in bentools/guzzle-throttle-middleware
Package guzzle-throttle-middleware
Short Description A GuzzleHTTP Middleware that can delay requests before sending them.
License MIT
Informations about the package guzzle-throttle-middleware
Guzzle Throttle Middleware
This middleware adds throttling capabilities to your Guzzle client.
This can be useful when some hosts limits your number of requests per second / per minute.
Installation
composer require bentools/guzzle-throttle-middleware
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 implementation,
such as symfony/cache, and use shared storages like Redis, APCu, Memcached, ...
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
Tests
./vendor/bin/phpunit
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, a handler that delegates asynchronous requests to PHP workers (Beanstalk, RabbitMQ, Redis, ...).
You can then enable throttling only on workers.
See also
- bentools/guzzle-queue-handler - A queue handler to process Guzzle 6+ requests within a work queue.
- 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 - A Guzzle 6+ Middleware that adds a X-Request-Duration header to all responses to monitor response times.
All versions of guzzle-throttle-middleware with dependencies
guzzlehttp/guzzle Version ^6.0
bentools/psr7-request-matcher Version ^1.0
psr/log Version ^1.0