artisansdk/ratelimiter
ArtisanSDK RateLimiter is a Laravel/PHP package for adding configurable request throttling to your app. Define limits per route or key, enforce rate rules, and protect APIs from abuse with simple integration and clear control over retry/decay settings.
Strengths:
Weaknesses:
SETNX or Lua scripts).illuminate/support/Facades/RateLimiter or third-party circuit breakers (e.g., spatie/laravel-circuitbreaker).Key Use Cases:
/api/v1/users) where burst traffic is expected./login, /forgot-password)./billing/webhooks) to prevent abuse of free tiers.Laravel Ecosystem Compatibility:
Handle middleware classes and route groups.register/boot methods, adhering to best practices..env variables for rate limits (e.g., RATE_LIMIT=60/second), enabling environment-specific tuning.Dependencies:
config/database.php.predis or php-redis for Redis).Customization Points:
capacity (bucket size) and refill_rate (tokens/second) per route.429 Too Many Requests responses via middleware handle() method.High-Risk Areas:
SET key value NX PX ms), race conditions may inflate token counts.Mitigation Strategies:
SETNX or Lua scripts for atomic token updates (package may need extension).Open Questions:
Laravel Version Compatibility:
laravel/framework v10.x in composer.json.Tech Stack Synergy:
laravel-redis package for consistency.mysql/pgsql with proper indexing on the key column.Alternatives Considered:
spatie/laravel-rate-limiter: Fixed-window algorithm (less burst-friendly).digitalcreative/laravel-ratelimiter: Token bucket alternative (may lack leaky bucket features).Assessment Phase:
throttle middleware)./api/search vs. /api/health).Proof of Concept:
composer require artisansdk/ratelimiter.config/ratelimiter.php (or publish config with php artisan vendor:publish).Route::middleware([RateLimiterMiddleware::class])->get('/test', ...)).Phased Rollout:
throttle middleware with RateLimiterMiddleware for critical endpoints.capacity/refill_rate based on load tests.Configuration Example:
// config/ratelimiter.php
'limits' => [
'auth.login' => [
'driver' => 'redis',
'capacity' => 5, // Max tokens
'refill_rate' => 1, // Tokens/second
'key' => 'rate_limit_auth_login_{$ip}',
],
'api.search' => [
'driver' => 'database',
'capacity' => 100,
'refill_rate' => 10,
],
],
Middleware Conflicts:
RateLimiterMiddleware is placed before other middleware that might short-circuit (e.g., auth).Route::middleware([RateLimiterMiddleware::class, 'auth:sanctum']) to avoid false positives.Caching Layers:
Legacy Systems:
X-RateLimit-Remaining) for client-side adherence.Pre-Deployment:
ratelimiter:* keys (Redis) or rate_limits table (database).Deployment:
config('ratelimiter.enabled')).Post-Deployment:
laravel-logger integration).Configuration Drift:
config/ratelimiter.php may diverge across environments.RATE_LIMIT_AUTH_LOGIN_CAPACITY) and a config management tool (e.g., Laravel Forge, Terraform).Dependency Updates:
composer update artisansdk/ratelimiter --with-dependencies).Documentation:
config/database.php and firewall rules.APP_DEBUG=true and check middleware logs.How can I help you explore Laravel packages today?