Back Pressure Features
Simulate load and test system resilience with rate limiting and chaos error injection. Rate limiting uses a token bucket algorithm; chaos errors introduce random failures.
Control Flow
graph TD
A[Receive Request] --> B[Rate Limit Check]
B -->|Within Limit| C{Check Chaos}
B -->|Exceeds Limit| D[Return 429]
C -->|X-Echo-Chaos| E[Random Failure]
C -->|X-Echo-Error| F[Specific Error]
E -->|Fail| G[Return 500/502/503/504/408]
E -->|Pass| H[Continue Processing]
F --> G
H --> I[Process Request]
Rate Limiting
Control requests-per-second globally via environment variables. Per-request headers are not supported.
- Env:
ECHO_RATE_LIMIT_RPS=50,ECHO_RATE_LIMIT_BURST=100 - Notes: Token bucket limiter applies to all routes except
/sse.
# Start server with rate limiting
ECHO_RATE_LIMIT_RPS=10 ECHO_RATE_LIMIT_BURST=20 ./advanced-echo-server
# If limit exceeded:
# HTTP 429 Too Many Requests
# Retry-After: 60
Chaos Errors
Inject random failures with a specified probability (0-100%).
- Header:
X-Echo-Chaos: 30 - Env:
ECHO_CHAOS=15 - Errors: 500, 502, 503, 504, 408, 429 (random mode may include 429)
- Example:
curl -H "X-Echo-Chaos: 30" http://localhost:8080/
# ~30% chance of HTTP 500/502/503/504/408
# Logs: "Chaos error injected: 503"
Specific Error Injection
Force a specific HTTP error code.
- Header:
X-Echo-Error: 503(e.g., 500, 502, 503, 504, 408, 429) - Example:
curl -H "X-Echo-Error: 503" http://localhost:8080/
# Response: HTTP 503 Service Unavailable
# Logs: "Forced error: 503"
Notes
- Rate limiting is skipped for SSE requests to ensure uninterrupted streaming.
- Chaos errors are tracked via
echo_chaos_errors_totalmetrics. - Headers override environment variables for per-request control.