|
1 | | -[](https://app.travis-ci.com/Neuron-PHP/routing) |
| 1 | +[](https://github.com/neuron-php/routing/actions) |
2 | 2 | # Neuron-PHP Routing |
3 | 3 |
|
4 | 4 | ## Overview |
@@ -80,6 +80,168 @@ several routes including one with a variable. |
80 | 80 | If present, the extra element is merged into the parameters array |
81 | 81 | before it is passed to the routes closure. |
82 | 82 |
|
| 83 | +## Rate Limiting |
| 84 | + |
| 85 | +The routing component includes a powerful rate limiting system with multiple storage backends and flexible configuration options. |
| 86 | + |
| 87 | +### Basic Usage |
| 88 | + |
| 89 | +```php |
| 90 | +use Neuron\Routing\Router; |
| 91 | +use Neuron\Routing\Filters\RateLimitFilter; |
| 92 | +use Neuron\Routing\RateLimit\RateLimitConfig; |
| 93 | + |
| 94 | +$router = Router::instance(); |
| 95 | + |
| 96 | +// Create rate limit configuration |
| 97 | +$config = new RateLimitConfig([ |
| 98 | + 'enabled' => true, |
| 99 | + 'storage' => 'redis', // Options: redis, file, memory (testing only) |
| 100 | + 'requests' => 100, // Max requests per window |
| 101 | + 'window' => 3600 // Time window in seconds (1 hour) |
| 102 | +]); |
| 103 | + |
| 104 | +// Create and register the filter |
| 105 | +$rateLimitFilter = new RateLimitFilter($config); |
| 106 | +$router->registerFilter('rate_limit', $rateLimitFilter); |
| 107 | + |
| 108 | +// Apply globally to all routes |
| 109 | +$router->addFilter('rate_limit'); |
| 110 | + |
| 111 | +// Or apply to specific routes |
| 112 | +$router->get('/api/data', $handler, 'rate_limit'); |
| 113 | +``` |
| 114 | + |
| 115 | +### Configuration Options |
| 116 | + |
| 117 | +Rate limiting can be configured via array or environment variables: |
| 118 | + |
| 119 | +```php |
| 120 | +// Array configuration |
| 121 | +$config = new RateLimitConfig([ |
| 122 | + 'enabled' => true, |
| 123 | + 'storage' => 'redis', |
| 124 | + 'requests' => 100, |
| 125 | + 'window' => 3600, |
| 126 | + 'redis_host' => '127.0.0.1', |
| 127 | + 'redis_port' => 6379, |
| 128 | + 'file_path' => 'cache/rate_limits' |
| 129 | +]); |
| 130 | + |
| 131 | +// From settings/environment variables (flat structure) |
| 132 | +// RATE_LIMIT_ENABLED=true |
| 133 | +// RATE_LIMIT_STORAGE=redis |
| 134 | +// RATE_LIMIT_REQUESTS=100 |
| 135 | +// RATE_LIMIT_WINDOW=3600 |
| 136 | +$config = RateLimitConfig::fromSettings($settingsSource); |
| 137 | +``` |
| 138 | + |
| 139 | +### Storage Backends |
| 140 | + |
| 141 | +#### Redis (Recommended for Production) |
| 142 | +Best for distributed systems and high-traffic applications: |
| 143 | + |
| 144 | +```php |
| 145 | +$config = new RateLimitConfig([ |
| 146 | + 'storage' => 'redis', |
| 147 | + 'redis_host' => '127.0.0.1', |
| 148 | + 'redis_port' => 6379, |
| 149 | + 'redis_database' => 0, |
| 150 | + 'redis_prefix' => 'rate_limit_', |
| 151 | + 'redis_auth' => 'password', // Optional |
| 152 | + 'redis_persistent' => true // Use persistent connections |
| 153 | +]); |
| 154 | +``` |
| 155 | + |
| 156 | +#### File Storage |
| 157 | +Simple solution for single-server deployments: |
| 158 | + |
| 159 | +```php |
| 160 | +$config = new RateLimitConfig([ |
| 161 | + 'storage' => 'file', |
| 162 | + 'file_path' => 'cache/rate_limits' // Directory for rate limit files |
| 163 | +]); |
| 164 | +``` |
| 165 | + |
| 166 | +#### Memory Storage (Testing Only) |
| 167 | +For unit tests and development: |
| 168 | + |
| 169 | +```php |
| 170 | +$config = new RateLimitConfig([ |
| 171 | + 'storage' => 'memory' // Data lost when PHP process ends |
| 172 | +]); |
| 173 | +``` |
| 174 | + |
| 175 | +### Advanced Features |
| 176 | + |
| 177 | +#### Key Strategies |
| 178 | +Control how rate limits are applied: |
| 179 | + |
| 180 | +```php |
| 181 | +// Limit by IP address (default) |
| 182 | +$filter = new RateLimitFilter($config, 'ip'); |
| 183 | + |
| 184 | +// Limit by authenticated user |
| 185 | +$filter = new RateLimitFilter($config, 'user'); |
| 186 | + |
| 187 | +// Limit by IP + route combination |
| 188 | +$filter = new RateLimitFilter($config, 'route'); |
| 189 | + |
| 190 | +// Custom key generation |
| 191 | +class CustomRateLimitFilter extends RateLimitFilter { |
| 192 | + protected function getCustomKey(RouteMap $route): string { |
| 193 | + // Your custom logic here |
| 194 | + return $_SESSION['tenant_id'] ?? $this->getClientIp(); |
| 195 | + } |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +#### Whitelisting and Blacklisting |
| 200 | + |
| 201 | +```php |
| 202 | +$filter = new RateLimitFilter( |
| 203 | + $config, |
| 204 | + 'ip', |
| 205 | + ['192.168.1.100', '10.0.0.1'], // Whitelist - no limits |
| 206 | + ['45.67.89.10'] // Blacklist - stricter limits (1/10th) |
| 207 | +); |
| 208 | +``` |
| 209 | + |
| 210 | +#### Custom Responses |
| 211 | +Rate limit exceeded responses include appropriate headers: |
| 212 | +- `X-RateLimit-Limit`: Maximum requests allowed |
| 213 | +- `X-RateLimit-Remaining`: Requests remaining |
| 214 | +- `X-RateLimit-Reset`: Unix timestamp when limit resets |
| 215 | +- `Retry-After`: Seconds until retry allowed |
| 216 | + |
| 217 | +The response format (JSON/HTML) is automatically determined from the Accept header. |
| 218 | + |
| 219 | +### Example: API Rate Limiting |
| 220 | + |
| 221 | +```php |
| 222 | +// Different limits for different endpoints |
| 223 | +$publicConfig = new RateLimitConfig([ |
| 224 | + 'enabled' => true, |
| 225 | + 'storage' => 'redis', |
| 226 | + 'requests' => 10, |
| 227 | + 'window' => 60 // 10 requests per minute |
| 228 | +]); |
| 229 | + |
| 230 | +$apiConfig = new RateLimitConfig([ |
| 231 | + 'enabled' => true, |
| 232 | + 'storage' => 'redis', |
| 233 | + 'requests' => 1000, |
| 234 | + 'window' => 3600 // 1000 requests per hour |
| 235 | +]); |
| 236 | + |
| 237 | +$router->registerFilter('public_limit', new RateLimitFilter($publicConfig)); |
| 238 | +$router->registerFilter('api_limit', new RateLimitFilter($apiConfig)); |
| 239 | + |
| 240 | +// Apply different limits |
| 241 | +$router->get('/public/search', $searchHandler, 'public_limit'); |
| 242 | +$router->get('/api/users', $usersHandler, 'api_limit'); |
| 243 | +``` |
| 244 | + |
83 | 245 | # More Information |
84 | 246 |
|
85 | 247 | You can read more about the Neuron components at [neuronphp.com](http://neuronphp.com) |
0 commit comments