- 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.