vectorifyai/guzzle-rate-limiter

Guzzle middleware for preventive rate limiting with multi-store support, progressive delays, and cross-process coordination.

1.0.1 2025-07-08 00:15 UTC

This package is auto-updated.

Last update: 2025-07-08 10:20:54 UTC


README

Latest Version Total Downloads Tests License

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.

Features

  • Intelligent Rate Limiting: Progressive delays based on remaining API quota
  • Multi-Store Support: InMemory, Laravel Cache, Symfony Cache, and more
  • Cross-Process Coordination: Share rate limit state across multiple processes
  • Automatic Recovery: Handles 429 responses with exponential backoff
  • Flexible Configuration: Customizable thresholds and delays
  • PSR-3 Logging: Built-in logging with configurable levels

Installation

composer require vectorifyai/guzzle-rate-limiter

Quick Start

use 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
]);

Usage

Basic Usage

use Vectorify\GuzzleRateLimiter\RateLimiterMiddleware;
use Vectorify\GuzzleRateLimiter\Stores\InMemoryStore;

$middleware = new RateLimiterMiddleware(new InMemoryStore());

Laravel Integration

use Vectorify\GuzzleRateLimiter\Stores\LaravelStore;

$store = new LaravelStore(cache(), 'api:rate_limit');
$middleware = new RateLimiterMiddleware($store);

Symfony Integration

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);

Advanced Configuration

$middleware = new RateLimiterMiddleware(
    store: new InMemoryStore(),
    cachePrefix: 'my_api:rate_limit',
    logger: $customLogger
);

Changelog

Please see Releases for more information on what has changed recently.

Contributing

Pull requests are more than welcome. You must follow the PSR coding standards.

Security

Please review our security policy on how to report security vulnerabilities.

License

The MIT License (MIT). Please see LICENSE for more information.