Skip to content

Instantly share code, notes, and snippets.

@InnaTarasyan
Created November 29, 2025 16:42
Show Gist options
  • Select an option

  • Save InnaTarasyan/c3c7004e4f1001614453a57483b35a25 to your computer and use it in GitHub Desktop.

Select an option

Save InnaTarasyan/c3c7004e4f1001614453a57483b35a25 to your computer and use it in GitHub Desktop.
Automatic Rate Limiter Per User
// app/Http/Middleware/SmartRateLimit.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\RateLimiter;
class SmartRateLimit
{
public function handle($request, Closure $next)
{
$key = 'rate:'.$request->user()->id;
if (RateLimiter::tooManyAttempts($key, 60)) {
return response()->json([
'message' => 'Too many requests'
], 429);
}
RateLimiter::hit($key, 60);
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment