Rate Limiting
Rate Limiting
Section titled “Rate Limiting”Bifrost provides comprehensive rate limiting to control request rates and bandwidth usage. Rate limiting is configured on the server and applies to all proxy traffic.
Configuration
Section titled “Configuration”Rate limiting is configured in the server config under the rate_limit section:
rate_limit: enabled: true requests_per_second: 100 # Sustained rate limit burst_size: 200 # Maximum burst size per_ip: true # Rate limit per IP address per_user: false # Rate limit per authenticated user bandwidth: enabled: true upload: "10Mbps" # Per-connection upload limit download: "100Mbps" # Per-connection download limitRate Limiting Options
Section titled “Rate Limiting Options”Global Settings
Section titled “Global Settings”| Setting | Type | Default | Description |
|---|---|---|---|
enabled | bool | false | Enable rate limiting |
requests_per_second | float | 100 | Sustained request rate |
burst_size | int | 200 | Maximum burst capacity |
per_ip | bool | true | Track limits per client IP |
per_user | bool | false | Track limits per authenticated user |
Bandwidth Throttling
Section titled “Bandwidth Throttling”| Setting | Type | Default | Description |
|---|---|---|---|
bandwidth.enabled | bool | false | Enable bandwidth throttling |
bandwidth.upload | string | - | Upload speed limit (e.g., 10Mbps) |
bandwidth.download | string | - | Download speed limit (e.g., 100Mbps) |
Bandwidth Units
Section titled “Bandwidth Units”Supported units for bandwidth settings:
bps- bits per secondKbps- kilobits per secondMbps- megabits per secondGbps- gigabits per secondBps- bytes per secondKBps- kilobytes per secondMBps- megabytes per secondGBps- gigabytes per second
How Rate Limiting Works
Section titled “How Rate Limiting Works”Token Bucket Algorithm
Section titled “Token Bucket Algorithm”Bifrost uses a token bucket algorithm for rate limiting:
- Each bucket has a maximum capacity (
burst_size) - Tokens are added at
requests_per_secondrate - Each request consumes one token
- If no tokens are available, the request is rejected
Per-IP vs Per-User Tracking
Section titled “Per-IP vs Per-User Tracking”Per-IP (per_ip: true):
- Each unique client IP gets its own rate limit bucket
- Suitable for unauthenticated or mixed traffic
- May need higher limits for NAT’d clients
Per-User (per_user: true):
- Each authenticated user gets their own rate limit bucket
- Requires authentication to be enabled
- More accurate for multi-tenant environments
Both enabled:
- Requests are checked against both limits
- Request is rejected if either limit is exceeded
Example Configurations
Section titled “Example Configurations”Basic Rate Limiting
Section titled “Basic Rate Limiting”Simple rate limit for general use:
rate_limit: enabled: true requests_per_second: 100 burst_size: 150 per_ip: trueHigh-Traffic API Server
Section titled “High-Traffic API Server”For servers handling many API requests:
rate_limit: enabled: true requests_per_second: 1000 burst_size: 2000 per_ip: true per_user: trueBandwidth-Constrained Environment
Section titled “Bandwidth-Constrained Environment”For limited bandwidth environments:
rate_limit: enabled: true requests_per_second: 50 burst_size: 100 per_ip: true bandwidth: enabled: true upload: "5Mbps" download: "25Mbps"LAN Caching Server
Section titled “LAN Caching Server”For a local game download cache:
rate_limit: enabled: true requests_per_second: 500 burst_size: 1000 per_ip: true bandwidth: enabled: true upload: "100Mbps" download: "1Gbps"OpenWrt/Embedded Device
Section titled “OpenWrt/Embedded Device”Conservative settings for low-power devices:
rate_limit: enabled: true requests_per_second: 20 burst_size: 50 per_ip: true bandwidth: enabled: true upload: "10Mbps" download: "50Mbps"Hot Reload Support
Section titled “Hot Reload Support”Rate limiting configuration supports hot reload. Changes take effect without restarting the server:
# Modify rate_limit section in config# Then reload:curl -X POST \ -H "Authorization: Bearer your-token" \ -H "X-Requested-With: XMLHttpRequest" \ http://localhost:7082/api/v1/config/reloadOr via the Web UI config editor.
Monitoring Rate Limits
Section titled “Monitoring Rate Limits”Request Logs
Section titled “Request Logs”Rate-limited requests appear in the request log:
{ "id": 12345, "timestamp": "2024-01-15T10:00:00Z", "method": "GET", "host": "api.example.com", "status_code": 429, "error": "rate limit exceeded", "client_ip": "192.168.1.100"}Connection Statistics
Section titled “Connection Statistics”Monitor rate limit impact via the stats API:
curl -H "Authorization: Bearer token" \ http://localhost:7082/api/v1/statsError Responses
Section titled “Error Responses”When rate limit is exceeded, clients receive:
HTTP/1.1 429 Too Many RequestsContent-Type: text/plain
Rate limit exceededBest Practices
Section titled “Best Practices”Setting Appropriate Limits
Section titled “Setting Appropriate Limits”- Start conservative - Begin with lower limits and increase as needed
- Monitor actual traffic - Use request logs to understand patterns
- Consider burst traffic - Set
burst_sizehigher thanrequests_per_secondfor spiky workloads - Account for NAT - Multiple users behind NAT share an IP; adjust per-IP limits accordingly
Bandwidth Limits
Section titled “Bandwidth Limits”- Match your infrastructure - Don’t set higher than your actual bandwidth
- Leave headroom - Don’t allocate 100% of bandwidth
- Consider client count - Per-connection limits × max connections = total bandwidth
Security Considerations
Section titled “Security Considerations”-
Rate limiting helps prevent:
- Denial of service attacks
- Brute force attempts
- Resource exhaustion
-
Rate limiting doesn’t replace:
- Authentication
- Input validation
- Proper access control
Troubleshooting
Section titled “Troubleshooting”Requests Being Rejected Unexpectedly
Section titled “Requests Being Rejected Unexpectedly”-
Check current rate limit settings:
Terminal window curl -H "Authorization: Bearer token" \http://localhost:7082/api/v1/config | jq '.rate_limit' -
Review request logs for rate limit errors
-
Consider if multiple clients share an IP (NAT)
-
Increase
burst_sizefor spiky traffic
Slow Connections
Section titled “Slow Connections”- Check bandwidth throttling settings
- Verify bandwidth units (Mbps vs MBps)
- Monitor active connections to see bandwidth distribution
Rate Limits Not Applying
Section titled “Rate Limits Not Applying”- Verify
enabled: truein config - Check if config was reloaded after changes
- Confirm requests are going through the proxy (not bypassing)
API Reference
Section titled “API Reference”View Current Rate Limit Config
Section titled “View Current Rate Limit Config”curl -H "Authorization: Bearer token" \ http://localhost:7082/api/v1/config | jq '.rate_limit'Update Rate Limit Config
Section titled “Update Rate Limit Config”curl -X PUT \ -H "Authorization: Bearer token" \ -H "Content-Type: application/json" \ -H "X-Requested-With: XMLHttpRequest" \ -d '{ "config": { "rate_limit": { "enabled": true, "requests_per_second": 200, "burst_size": 400 } } }' \ http://localhost:7082/api/v1/configThe response indicates if the change was applied via hot reload:
{ "success": true, "message": "Configuration saved", "requires_restart": false, "changed_sections": ["rate_limit"]}