93 std::lock_guard<std::mutex> lock(
mutex_);
127 std::lock_guard<std::mutex> lock(
mutex_);
146 std::lock_guard<std::mutex> lock(
mutex_);
167 return state_.load(std::memory_order_acquire);
224 [[nodiscard]]
auto get_stats() const -> std::unordered_map<std::
string, interfaces::stats_value>
override
226 std::lock_guard<std::mutex> lock(
mutex_);
228 const auto current_state =
state_.load(std::memory_order_acquire);
232 {
"current_state",
to_string(current_state)},
233 {
"failure_count",
static_cast<std::int64_t
>(failure_count)},
245 [[nodiscard]]
auto to_json() const -> std::
string override
248 return snapshot.to_json();
255 [[nodiscard]]
auto name() const -> std::string_view
override
257 return "circuit_breaker";
267 const auto now = clock_type::now();
268 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
Configuration structure for circuit breaker behavior.
Circuit breaker state machine states.
Interface for components that expose statistics.
virtual auto get_snapshot() const -> stats_snapshot
Get a complete statistics snapshot with metadata.
RAII guard for automatic success/failure recording.
guard(const guard &)=delete
guard(circuit_breaker &breaker)
circuit_breaker & breaker_
auto record_success() -> void
Explicitly mark operation as successful. Prevents automatic failure recording on destruction.
guard & operator=(guard &&)=delete
guard & operator=(const guard &)=delete
Thread-safe circuit breaker for fault tolerance.
auto name() const -> std::string_view override
Get component name for identification.
auto to_json() const -> std::string override
Get statistics as JSON string.
std::size_t consecutive_successes_
std::chrono::steady_clock clock_type
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.
circuit_breaker(circuit_breaker_config config={})
Construct circuit breaker with specified configuration.
std::size_t half_open_requests_
auto transition_to_half_open() -> void
Transition to HALF_OPEN state. Must be called with mutex locked.
auto transition_to_closed() -> void
Transition to CLOSED state. Must be called with mutex locked.
auto make_guard() -> guard
Create RAII guard for automatic recording.
std::atomic< circuit_state > state_
clock_type::time_point time_point
auto allow_request() -> bool
Check if request should be allowed through the circuit.
auto get_state() const -> circuit_state
Get current circuit state.
circuit_breaker_config config_
auto transition_to_open() -> void
Transition to OPEN state. Must be called with mutex locked.
failure_window failure_window_
time_point last_state_change_
auto get_stats() const -> std::unordered_map< std::string, interfaces::stats_value > override
Get current statistics as key-value pairs.
auto record_success() -> void
Record a successful operation. May trigger state transition from HALF_OPEN to CLOSED.
auto should_attempt_reset() const -> bool
Check if circuit should attempt reset to HALF_OPEN. Must be called with mutex locked.
Thread-safe sliding window for failure tracking.
auto get_failure_count() const -> std::size_t
Get current failure count within window. Removes expired failures before counting.
auto record_failure() -> void
Record a new failure at current time.
auto reset() -> void
Clear all recorded failures.
Sliding time window for tracking failures.
circuit_state
Represents the current state of a circuit breaker.
@ CLOSED
Normal operation state. Requests are allowed and failures are tracked.
@ HALF_OPEN
Recovery testing state. Limited requests are allowed to test if service has recovered.
@ OPEN
Failure state. Requests are immediately rejected without execution. Transitions to HALF_OPEN after ti...
auto to_string(circuit_state state) -> std::string
Convert circuit state to human-readable string.
Convenience header for statistics interfaces.
Configuration parameters for circuit breaker.
std::chrono::milliseconds timeout
Timeout before transitioning from OPEN to HALF_OPEN. Default: 30 seconds.
std::size_t half_open_max_requests
Maximum number of requests allowed in HALF_OPEN state for testing. Default: 3 requests.
std::size_t failure_threshold
Number of failures required to trip the circuit (CLOSED -> OPEN). Default: 5 failures.
std::size_t success_threshold
Number of successful requests required to close the circuit (HALF_OPEN -> CLOSED)....