Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Guzzle Rate Limiter Middleware

Guzzle Rate Limiter Middleware Laravel Package

spatie/guzzle-rate-limiter-middleware

Guzzle middleware to rate-limit HTTP requests by requests/second or requests/minute. When the limit is hit, it sleeps until a slot is available. Includes an in-memory store and supports custom persistence for sharing limits across processes.

View on GitHub
Deep Wiki
Context7

A rate limiter middleware for Guzzle

Frequently asked questions about Guzzle Rate Limiter Middleware
How do I integrate this rate limiter with Laravel’s HTTP client facade?
Use the `withRateLimiter` macro provided in the package. Register it once in a service provider or boot file, then apply it to requests like this: `Http::withRateLimiter(['/api' => 10])->get('https://example.com')`. The middleware will enforce limits per endpoint or globally.
What Laravel and Guzzle versions does this package support?
The package works with Laravel 8+ (Guzzle 7) and Laravel 7.x (Guzzle 6). Check the `composer.json` constraints for exact version ranges. For Laravel 9+, ensure compatibility with Guzzle 7.x, as the package aligns with its middleware stack.
Can I use Redis for distributed rate limiting across multiple Laravel instances?
Yes, implement a custom `RateLimiterStore` driver using Redis. The package ships with an `InMemoryStore`, but you can extend it to use Laravel’s cache or Redis directly. This ensures consistent limits across microservices or load-balanced setups.
How do I set different rate limits for different API endpoints?
Pass an associative array to the middleware constructor or macro, mapping endpoints to limits. For example, `['/users' => 5, '/posts' => 10]` will enforce 5 requests/minute for `/users` and 10 for `/posts`. Wildcards or regex patterns aren’t supported natively.
What happens if a rate limit is exceeded? Does it block requests?
By default, the middleware uses `sleep()` to block the process until the limit resets. For non-blocking behavior, implement a custom driver (e.g., Redis) that returns a `429 Too Many Requests` response instead. This avoids latency spikes in production.
Is this package suitable for Laravel queue workers making HTTP requests?
Yes, but ensure your rate-limiting store is thread-safe if using Redis or a database. The `InMemoryStore` won’t work across queue workers. Bind a custom driver in Laravel’s service container to share state consistently.
How do I test rate-limiting logic in PHPUnit?
Mock the `RateLimiterStore` or use Laravel’s `fakeTime()` helper to simulate time progression. Avoid relying on real delays in tests; instead, verify the middleware’s response (e.g., `429`) or sleep behavior with conditional logic.
Are there alternatives to this package for Laravel rate limiting?
For Laravel HTTP clients, consider `spatie/laravel-http-rate-limiter` (higher-level facade) or `symfony/rate-limiter` (standalone). For Guzzle, `guzzlehttp/ringphp` or custom middleware with Redis are options, but this package offers a dedicated, Laravel-friendly solution.
How do I implement user-specific rate limits (e.g., per-authenticated user)?
Extend the `RateLimiterStore` to include user context (e.g., from request headers or Laravel’s auth). Override methods like `shouldSleep()` to calculate limits dynamically. Example: `new RateLimiterMiddleware(['*' => $user->rateLimit])`.
What’s the performance impact of using this middleware in production?
The `InMemoryStore` adds minimal overhead (~1ms per request). Redis-backed stores introduce ~5–20ms latency per request. For high-throughput APIs, benchmark with your expected request volume and adjust limits or use async drivers to reduce blocking.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport