Implementation
Circuit states:. . CLOSED (normal). - Requests pass through to backend. - Track failures in a sliding window. - If failures > threshold → OPEN. OPEN (failing). - All requests fail fast (no backend attempts). - After timeout → HALF_OPEN. HALF_OPEN (testing). - Allow one probe request through. - If success → CLOSED. - If failure → OPEN. . Failure tracking:. typescript. state: "CLOSED" | "OPEN" | "HALF_OPEN";. failureCount: number;. lastFailureTime: number;. successCount: number;. // Configuration. failureThreshold: number = 5; // Open after 5 failures. timeout: number = 60000; // Try again after 60s. halfOpenMaxAttempts: number = 1; // One probe request. . Example circuit breaking:. json. // Backend is healthy (circuit closed):. // Backend starts failing (5 failures in 30s):. // After 60s timeout (half-open):. . Circuit breaker metrics:. json. "backend": "api-1",. "state": "OPEN",. "failure_count": 7,. "last_failure_time": 1680123456,. "opened_at": 1680123450,. "next_attempt_at": 1680123510.
Sample Test Cases
{"src":"client","dest":"l7_proxy","body":{"type":"http_request","msg_id":1,"method":"GET","path":"/api/users","simulate_failures":5}}
{"src":"client","dest":"l7_proxy","body":{"type":"http_request","msg_id":2,"method":"GET","path":"/api/users"}}
{"src": "l7_proxy", "dest": "client", "body": {"type": "http_response", "in_reply_to": 1, "status": 500, "backend": "api-1"}}
{
"src": "client",
"dest": "l7_proxy",
"body": {
"type": "http_request",
"msg_id": 1,
"method": "GET",
"path": "/api/users",
"circuit_state": "HALF_OPEN",
"probe_result": "success"
}
}{"src": "l7_proxy", "dest": "client", "body": {"type": "http_response", "in_reply_to": 1, "status": 200, "backend": "api-1", "circuit_state": "CLOSED"}}
Hints
Hint 1▾
Hint 2▾
Hint 3▾
Hint 4▾
Hint 5▾
Theoretical Hub
Concept overview coming soon