Skip to main content

CIRCUIT_BREAKER

Constant CIRCUIT_BREAKER 

Source
pub const CIRCUIT_BREAKER: &str = "// Failure-threshold breaker with a time-based recovery probe.\n//\n// Every state field and the one effect are `i64`; the type parameter `T` this\n// machine used to declare was referenced nowhere, and generated Rust that did\n// not compile (E0392). If a breaker should carry a payload, put it in a state\n// field so the parameter is actually used.\nmachine CircuitBreaker {\n    state Closed(failures: i64, threshold: i64)\n    state Open(opened_at: i64, timeout_ms: i64)\n    state HalfOpen(successes: i64, needed: i64)\n\n    transition fail: Closed -> Closed | Open\n    transition check_open: Open -> Open | HalfOpen\n    transition succeed_half: HalfOpen -> HalfOpen | Closed\n\n    effect current_time_ms() -> i64\n\n    on fail() {\n        let next_failures = failures + 1;\n        if next_failures >= threshold {\n            goto Open(perform current_time_ms(), 60000);\n        } else {\n            goto Closed(next_failures, threshold);\n        }\n    }\n\n    on check_open() {\n        let elapsed = perform current_time_ms() - opened_at;\n        if elapsed >= timeout_ms {\n            goto HalfOpen(0, 3);\n        } else {\n            goto Open(opened_at, timeout_ms);\n        }\n    }\n\n    on succeed_half() {\n        let next = successes + 1;\n        if next >= needed {\n            goto Closed(0, 5);\n        } else {\n            goto HalfOpen(next, needed);\n        }\n    }\n}\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.