A sophisticated Guzzle middleware for preventive rate limiting with multi-store support, progressive delays, and cross-process coordination. It works in accordance with the IETF standard by using the X-RateLimit-Remaining (number of requests remaining in the current rate limit window) and Retry-After (number of seconds to wait before retrying the request again) values available in the response headers.
- Intelligent Rate Limiting: Progressive delays based on remaining API quota
- Multi-Store Support: InMemory, Laravel Cache, Symfony Cache, Filesystem, and more
- Cross-Process Coordination: Share rate limit state across multiple processes (i.e. queue jobs)
- Automatic Recovery: Handles 429 responses with exponential backoff
- Flexible Configuration: Customizable thresholds and delays
- PSR-3 Logging: Built-in logging with configurable levels
composer require vectorifyai/guzzle-rate-limiteruse GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Vectorify\GuzzleRateLimiter\RateLimiterMiddleware;
use Vectorify\GuzzleRateLimiter\Stores\InMemoryStore;
$stack = HandlerStack::create();
$stack->push(new RateLimiterMiddleware(new InMemoryStore));
$client = new Client([
'handler' => $stack,
]);use Vectorify\GuzzleRateLimiter\RateLimiterMiddleware;
use Vectorify\GuzzleRateLimiter\Stores\InMemoryStore;
$middleware = new RateLimiterMiddleware(new InMemoryStore);use Vectorify\GuzzleRateLimiter\Stores\LaravelStore;
$store = new LaravelStore(cache(), 'api:rate_limit');
$middleware = new RateLimiterMiddleware($store);use Vectorify\GuzzleRateLimiter\Stores\SymfonyStore;
use Symfony\Component\Cache\Adapter\RedisAdapter;
$cache = new RedisAdapter(/* redis client */);
$store = new SymfonyStore($cache, 'api:rate_limit');
$middleware = new RateLimiterMiddleware($store);use Vectorify\GuzzleRateLimiter\Stores\FilesystemStore;
use League\Flysystem\Local\LocalFilesystemAdapter;
// Local filesystem
$adapter = new LocalFilesystemAdapter('/path/to/cache');
$store = new FilesystemStore($adapter);
$middleware = new RateLimiterMiddleware($store);
// AWS S3
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
$s3Adapter = new AwsS3V3Adapter($s3Client, $bucket, $prefix);
$store = new FilesystemStore($s3Adapter);
// SFTP
use League\Flysystem\PhpseclibV3\SftpAdapter;
$sftpAdapter = new SftpAdapter(/* SFTP connection settings */);
$store = new FilesystemStore($sftpAdapter);$middleware = new RateLimiterMiddleware(
store: new InMemoryStore,
cachePrefix: 'my_api:rate_limit',
logger: $customLogger,
);Please see Releases for more information on what has changed recently.
Pull requests are more than welcome. You must follow the PSR coding standards.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see LICENSE for more information.