Skip to main content

CIRCUIT_BREAKER

Constant CIRCUIT_BREAKER 

Source
pub const CIRCUIT_BREAKER: &str = "machine CircuitBreaker<T> {\r\n    state Closed(failures: i64, threshold: i64)\r\n    state Open(opened_at: i64, timeout_ms: i64)\r\n    state HalfOpen(successes: i64, needed: i64)\r\n\r\n    transition fail: Closed -> Closed | Open\r\n    transition check_open: Open -> Open | HalfOpen\r\n    transition succeed_half: HalfOpen -> HalfOpen | Closed\r\n\r\n    effect current_time_ms() -> i64\r\n\r\n    on fail() {\r\n        let next_failures = failures + 1;\r\n        if next_failures >= threshold {\r\n            goto Open(perform current_time_ms(), 60000);\r\n        } else {\r\n            goto Closed(next_failures, threshold);\r\n        }\r\n    }\r\n\r\n    on check_open() {\r\n        let elapsed = perform current_time_ms() - opened_at;\r\n        if elapsed >= timeout_ms {\r\n            goto HalfOpen(0, 3);\r\n        } else {\r\n            goto Open(opened_at, timeout_ms);\r\n        }\r\n    }\r\n\r\n    on succeed_half() {\r\n        let next = successes + 1;\r\n        if next >= needed {\r\n            goto Closed(0, 5);\r\n        } else {\r\n            goto HalfOpen(next, needed);\r\n        }\r\n    }\r\n}\r\n";
Expand description

The Gust source for the CircuitBreaker machine.

Implements the circuit breaker pattern with three states:

  • Closed – requests pass through; failures are counted against a threshold.
  • Open – requests are blocked; a timeout controls when to probe again.
  • HalfOpen – a limited number of probe requests are allowed to test recovery.

Generic over T for the protected call’s context type.