devoralive/traffic-limit-bundle
Symfony bundle for rate limiting requests via Redis using SncRedisBundle. Define multiple limit profiles (amount/ttl) and Redis clients in config, then access the generated services from the container to enforce per-key traffic limits.
AppKernel), but Laravel can leverage its core logic (Redis rate-limiting) via custom integration or a wrapper. The bundle’s dependency on SncRedisBundle (Symfony-specific) is the primary constraint.phpredis or predis). Laravel already supports Redis for caching/queues, so this is a low-effort dependency if already in use.TooManyRequestsHttpException (Symfony’s HttpException), which Laravel can map to its own exceptions (e.g., Illuminate\Http\Exceptions\HttpResponseException) or handle via middleware.amount, ttl), but Laravel’s PHP-based config (e.g., config/traffic_limit.php) would need adaptation.processRequest()) is Symfony-dependent. Laravel would need a facade or rewritten logic.predis) may behave differently than Symfony’s phpredis.predis) or require phpredis?HandleIncomingRequest)?throttle middleware) or a maintained package like spatie/laravel-rate-limiting?predis/phpredis support overlaps with the bundle’s requirements. Test if the bundle’s Redis logic works with Laravel’s client.traffic_limit.low_limit) via a custom provider or facade.TooManyRequestsHttpException to Laravel’s abort(429) or a custom response.RateLimiter::attempt($key)) that calls the bundle’s logic.TrafficLimitMiddleware) that uses the bundle’s services.INCR/EXPIRE) into a Laravel package.predis for Laravel’s consistency).// app/Providers/TrafficLimitServiceProvider.php
public function register() {
$this->app->register(\Devoralive\TrafficLimit\TrafficLimitBundle::class);
// Override exception handling
$this->app->bind(
TooManyRequestsHttpException::class,
fn() => new HttpResponse('Too Many Requests', 429)
);
}
processRequest() on protected routes.// app/Http/Middleware/TrafficLimit.php
public function handle(Request $request, Closure $next) {
try {
app('traffic_limit.low_limit')->processRequest($request->ip());
return $next($request);
} catch (TooManyRequestsHttpException $e) {
abort(429);
}
}
INCR/EXPIRE patterns) into a Laravel package.// app/Services/RateLimiter.php
public function attempt(string $key, int $max, int $ttl): bool {
return Redis::connection()->incr($key) <= $max
&& Redis::connection()->expire($key, $ttl);
}
config/traffic_limit.php:
return [
'limits' => [
'low' => [
'max' => 600,
'ttl' => 60,
'key' => fn($request) => $request->ip(),
],
],
];
predis/phpredis compatibility. If issues arise, use a Redis abstraction layer.Symfony\Component\HttpFoundation\Request and HttpKernel. Replace with Laravel equivalents:
Request → Laravel’s Illuminate\Http\Request.TooManyRequestsHttpException → Laravel’s abort(429) or custom exception.traffic_limit.low_limit).SncRedisBundle may conflict with Laravel’s Redis setup. Use a minimal wrapper to avoid tight coupling.Redis::connection()->debug()).TooManyRequestsHttpException may not integrate seamlessly. Customize responses to match Laravel’s conventions (e.g., JSON API errors).ttl and amount based on traffic patterns (e.g., shorter `ttHow can I help you explore Laravel packages today?