|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Thread-safe circuit breaker for fault tolerance. More...
#include <circuit_breaker.h>


Classes | |
| class | guard |
| RAII guard for automatic success/failure recording. More... | |
Public Types | |
| using | clock_type = std::chrono::steady_clock |
| using | time_point = clock_type::time_point |
Public Member Functions | |
| circuit_breaker (circuit_breaker_config config={}) | |
| Construct circuit breaker with specified configuration. | |
| auto | allow_request () -> bool |
| Check if request should be allowed through the circuit. | |
| auto | record_success () -> void |
| Record a successful operation. May trigger state transition from HALF_OPEN to CLOSED. | |
| auto | record_failure (const std::exception *e=nullptr) -> void |
| Record a failed operation. May trigger state transition to OPEN or back to OPEN from HALF_OPEN. | |
| auto | get_state () const -> circuit_state |
| Get current circuit state. | |
| auto | make_guard () -> guard |
| Create RAII guard for automatic recording. | |
| auto | get_stats () const -> std::unordered_map< std::string, interfaces::stats_value > override |
| Get current statistics as key-value pairs. | |
| auto | to_json () const -> std::string override |
| Get statistics as JSON string. | |
| auto | name () const -> std::string_view override |
| Get component name for identification. | |
Public Member Functions inherited from kcenon::common::interfaces::IStats | |
| virtual | ~IStats ()=default |
| virtual auto | get_snapshot () const -> stats_snapshot |
| Get a complete statistics snapshot with metadata. | |
Private Member Functions | |
| auto | should_attempt_reset () const -> bool |
| Check if circuit should attempt reset to HALF_OPEN. Must be called with mutex locked. | |
| auto | transition_to_closed () -> void |
| Transition to CLOSED state. Must be called with mutex locked. | |
| auto | transition_to_open () -> void |
| Transition to OPEN state. Must be called with mutex locked. | |
| auto | transition_to_half_open () -> void |
| Transition to HALF_OPEN state. Must be called with mutex locked. | |
Private Attributes | |
| circuit_breaker_config | config_ |
| std::atomic< circuit_state > | state_ |
| failure_window | failure_window_ |
| std::size_t | consecutive_successes_ |
| std::size_t | half_open_requests_ |
| time_point | last_state_change_ |
| std::mutex | mutex_ |
Thread-safe circuit breaker for fault tolerance.
Usage Example:
Thread Safety:
Definition at line 68 of file circuit_breaker.h.
| using kcenon::common::resilience::circuit_breaker::clock_type = std::chrono::steady_clock |
Definition at line 70 of file circuit_breaker.h.
| using kcenon::common::resilience::circuit_breaker::time_point = clock_type::time_point |
Definition at line 71 of file circuit_breaker.h.
|
inlineexplicit |
Construct circuit breaker with specified configuration.
| config | Configuration parameters (thresholds, timeouts) |
Definition at line 77 of file circuit_breaker.h.
|
inlinenodiscard |
Check if request should be allowed through the circuit.
Definition at line 91 of file circuit_breaker.h.
References kcenon::common::resilience::CLOSED, config_, kcenon::common::resilience::HALF_OPEN, kcenon::common::resilience::circuit_breaker_config::half_open_max_requests, half_open_requests_, mutex_, kcenon::common::resilience::OPEN, should_attempt_reset(), state_, and transition_to_half_open().
Referenced by main().


|
inlinenodiscard |
Get current circuit state.
Definition at line 165 of file circuit_breaker.h.
References state_.
Referenced by main().

|
inlinenodiscardoverridevirtual |
Get current statistics as key-value pairs.
Implements kcenon::common::interfaces::IStats.
Definition at line 224 of file circuit_breaker.h.
References config_, consecutive_successes_, kcenon::common::resilience::circuit_breaker_config::failure_threshold, failure_window_, kcenon::common::resilience::failure_window::get_failure_count(), half_open_requests_, mutex_, kcenon::common::resilience::OPEN, state_, and kcenon::common::resilience::to_string().
Referenced by main().


|
inlinenodiscard |
Create RAII guard for automatic recording.
Definition at line 216 of file circuit_breaker.h.
Referenced by main().

|
inlinenodiscardoverridevirtual |
Get component name for identification.
Implements kcenon::common::interfaces::IStats.
Definition at line 255 of file circuit_breaker.h.
|
inline |
Record a failed operation. May trigger state transition to OPEN or back to OPEN from HALF_OPEN.
| e | Optional exception pointer for logging/metrics |
Definition at line 142 of file circuit_breaker.h.
References kcenon::common::resilience::CLOSED, config_, kcenon::common::resilience::circuit_breaker_config::failure_threshold, failure_window_, kcenon::common::resilience::failure_window::get_failure_count(), kcenon::common::resilience::HALF_OPEN, mutex_, kcenon::common::resilience::failure_window::record_failure(), state_, and transition_to_open().
Referenced by kcenon::common::resilience::circuit_breaker::guard::~guard().


|
inline |
Record a successful operation. May trigger state transition from HALF_OPEN to CLOSED.
Definition at line 125 of file circuit_breaker.h.
References config_, consecutive_successes_, kcenon::common::resilience::HALF_OPEN, mutex_, state_, kcenon::common::resilience::circuit_breaker_config::success_threshold, and transition_to_closed().
Referenced by kcenon::common::resilience::circuit_breaker::guard::record_success().


|
inlinenodiscardprivate |
Check if circuit should attempt reset to HALF_OPEN. Must be called with mutex locked.
Definition at line 265 of file circuit_breaker.h.
References config_, last_state_change_, and kcenon::common::resilience::circuit_breaker_config::timeout.
Referenced by allow_request().

|
inlinenodiscardoverridevirtual |
Get statistics as JSON string.
Implements kcenon::common::interfaces::IStats.
Definition at line 245 of file circuit_breaker.h.
References kcenon::common::interfaces::IStats::get_snapshot().

|
inlineprivate |
Transition to CLOSED state. Must be called with mutex locked.
Definition at line 277 of file circuit_breaker.h.
References kcenon::common::resilience::CLOSED, consecutive_successes_, failure_window_, half_open_requests_, last_state_change_, kcenon::common::resilience::failure_window::reset(), and state_.
Referenced by record_success().


|
inlineprivate |
Transition to HALF_OPEN state. Must be called with mutex locked.
Definition at line 302 of file circuit_breaker.h.
References consecutive_successes_, kcenon::common::resilience::HALF_OPEN, half_open_requests_, last_state_change_, and state_.
Referenced by allow_request().

|
inlineprivate |
Transition to OPEN state. Must be called with mutex locked.
Definition at line 290 of file circuit_breaker.h.
References consecutive_successes_, half_open_requests_, last_state_change_, kcenon::common::resilience::OPEN, and state_.
Referenced by record_failure().

|
private |
Definition at line 310 of file circuit_breaker.h.
Referenced by allow_request(), get_stats(), record_failure(), record_success(), and should_attempt_reset().
|
private |
Definition at line 313 of file circuit_breaker.h.
Referenced by get_stats(), record_success(), transition_to_closed(), transition_to_half_open(), and transition_to_open().
|
private |
Definition at line 312 of file circuit_breaker.h.
Referenced by get_stats(), record_failure(), and transition_to_closed().
|
private |
Definition at line 314 of file circuit_breaker.h.
Referenced by allow_request(), get_stats(), transition_to_closed(), transition_to_half_open(), and transition_to_open().
|
private |
Definition at line 315 of file circuit_breaker.h.
Referenced by should_attempt_reset(), transition_to_closed(), transition_to_half_open(), and transition_to_open().
|
mutableprivate |
Definition at line 316 of file circuit_breaker.h.
Referenced by allow_request(), get_stats(), record_failure(), and record_success().
|
private |
Definition at line 311 of file circuit_breaker.h.
Referenced by allow_request(), get_state(), get_stats(), record_failure(), record_success(), transition_to_closed(), transition_to_half_open(), and transition_to_open().