Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::resilience::circuit_breaker Class Reference

Thread-safe circuit breaker for fault tolerance. More...

#include <circuit_breaker.h>

Inheritance diagram for kcenon::common::resilience::circuit_breaker:
Inheritance graph
Collaboration diagram for kcenon::common::resilience::circuit_breaker:
Collaboration graph

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_statestate_
 
failure_window failure_window_
 
std::size_t consecutive_successes_
 
std::size_t half_open_requests_
 
time_point last_state_change_
 
std::mutex mutex_
 

Detailed Description

Thread-safe circuit breaker for fault tolerance.

Usage Example:

.failure_threshold = 5,
.timeout = std::chrono::seconds(30)
};
circuit_breaker breaker(config);
// Check before making request
if (!breaker.allow_request()) {
// Circuit is open, handle gracefully
return error("Service unavailable");
}
// Use RAII guard for automatic recording
{
auto guard = breaker.make_guard();
// Make risky operation
auto result = risky_operation();
guard.record_success(); // Mark as success if no exception
} // Automatically records failure if exception thrown
Thread-safe circuit breaker for fault tolerance.
Configuration parameters for circuit breaker.

Thread Safety:

  • All public methods are thread-safe.
  • Safe for concurrent access from multiple threads.
  • State transitions are protected by internal synchronization.
Examples
circuit_breaker_example.cpp.

Definition at line 68 of file circuit_breaker.h.

Member Typedef Documentation

◆ clock_type

Definition at line 70 of file circuit_breaker.h.

◆ time_point

Definition at line 71 of file circuit_breaker.h.

Constructor & Destructor Documentation

◆ circuit_breaker()

kcenon::common::resilience::circuit_breaker::circuit_breaker ( circuit_breaker_config config = {})
inlineexplicit

Construct circuit breaker with specified configuration.

Parameters
configConfiguration parameters (thresholds, timeouts)

Definition at line 77 of file circuit_breaker.h.

77 {})
78 : config_(std::move(config))
80 , failure_window_(config_.failure_window)
84 {
85 }
circuit_state
Represents the current state of a circuit breaker.
@ CLOSED
Normal operation state. Requests are allowed and failures are tracked.

Member Function Documentation

◆ allow_request()

auto kcenon::common::resilience::circuit_breaker::allow_request ( ) -> bool
inlinenodiscard

Check if request should be allowed through the circuit.

Returns
true if request is allowed, false if circuit is open

Definition at line 91 of file circuit_breaker.h.

92 {
93 std::lock_guard<std::mutex> lock(mutex_);
94
95 switch (state_) {
97 return true;
98
100 // Check if timeout has elapsed to attempt recovery
101 if (should_attempt_reset()) {
103 // First request in HALF_OPEN counts toward the limit
105 return true;
106 }
107 return false;
108
110 // Allow limited requests for testing
113 return true;
114 }
115 return false;
116 }
117
118 return false; // Unreachable, but satisfies compiler
119 }
auto transition_to_half_open() -> void
Transition to HALF_OPEN state. Must be called with mutex locked.
auto should_attempt_reset() const -> bool
Check if circuit should attempt reset to HALF_OPEN. Must be called with mutex locked.
@ 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...
std::size_t half_open_max_requests
Maximum number of requests allowed in HALF_OPEN state for testing. Default: 3 requests.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_state()

auto kcenon::common::resilience::circuit_breaker::get_state ( ) const -> circuit_state
inlinenodiscard

Get current circuit state.

Returns
Current state (CLOSED, OPEN, or HALF_OPEN)

Definition at line 165 of file circuit_breaker.h.

166 {
167 return state_.load(std::memory_order_acquire);
168 }

References state_.

Referenced by main().

Here is the caller graph for this function:

◆ get_stats()

auto kcenon::common::resilience::circuit_breaker::get_stats ( ) const -> std::unordered_map<std::string, interfaces::stats_value>
inlinenodiscardoverridevirtual

Get current statistics as key-value pairs.

Returns
Map of metric names to values (state, counts, rates)

Implements kcenon::common::interfaces::IStats.

Definition at line 224 of file circuit_breaker.h.

225 {
226 std::lock_guard<std::mutex> lock(mutex_);
227
228 const auto current_state = state_.load(std::memory_order_acquire);
229 const auto failure_count = failure_window_.get_failure_count();
230
231 return {
232 {"current_state", to_string(current_state)},
233 {"failure_count", static_cast<std::int64_t>(failure_count)},
234 {"consecutive_successes", static_cast<std::int64_t>(consecutive_successes_)},
235 {"half_open_requests", static_cast<std::int64_t>(half_open_requests_)},
236 {"failure_threshold", static_cast<std::int64_t>(config_.failure_threshold)},
237 {"is_open", current_state == circuit_state::OPEN}
238 };
239 }
auto get_failure_count() const -> std::size_t
Get current failure count within window. Removes expired failures before counting.
auto to_string(circuit_state state) -> std::string
Convert circuit state to human-readable string.
std::size_t failure_threshold
Number of failures required to trip the circuit (CLOSED -> OPEN). Default: 5 failures.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_guard()

auto kcenon::common::resilience::circuit_breaker::make_guard ( ) -> guard
inlinenodiscard

Create RAII guard for automatic recording.

Returns
Guard object that records failure unless success is called

Definition at line 216 of file circuit_breaker.h.

216{ return guard(*this); }

Referenced by main().

Here is the caller graph for this function:

◆ name()

auto kcenon::common::resilience::circuit_breaker::name ( ) const -> std::string_view
inlinenodiscardoverridevirtual

Get component name for identification.

Returns
Component name "circuit_breaker"

Implements kcenon::common::interfaces::IStats.

Definition at line 255 of file circuit_breaker.h.

256 {
257 return "circuit_breaker";
258 }

◆ record_failure()

auto kcenon::common::resilience::circuit_breaker::record_failure ( const std::exception * e = nullptr) -> void
inline

Record a failed operation. May trigger state transition to OPEN or back to OPEN from HALF_OPEN.

Parameters
eOptional exception pointer for logging/metrics

Definition at line 142 of file circuit_breaker.h.

143 {
144 (void)e; // Reserved for future metrics/logging integration
145
146 std::lock_guard<std::mutex> lock(mutex_);
147
149
151 // Any failure in half-open immediately reopens circuit
153 } else if (state_ == circuit_state::CLOSED) {
154 // Check if failure threshold exceeded
157 }
158 }
159 }
auto transition_to_open() -> void
Transition to OPEN state. Must be called with mutex locked.
auto record_failure() -> void
Record a new failure at current time.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ record_success()

auto kcenon::common::resilience::circuit_breaker::record_success ( ) -> void
inline

Record a successful operation. May trigger state transition from HALF_OPEN to CLOSED.

Definition at line 125 of file circuit_breaker.h.

126 {
127 std::lock_guard<std::mutex> lock(mutex_);
128
133 }
134 }
135 }
auto transition_to_closed() -> void
Transition to CLOSED state. Must be called with mutex locked.
std::size_t success_threshold
Number of successful requests required to close the circuit (HALF_OPEN -> CLOSED)....

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ should_attempt_reset()

auto kcenon::common::resilience::circuit_breaker::should_attempt_reset ( ) const -> bool
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.

266 {
267 const auto now = clock_type::now();
268 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
269 now - last_state_change_);
270 return elapsed >= config_.timeout;
271 }
std::chrono::milliseconds timeout
Timeout before transitioning from OPEN to HALF_OPEN. Default: 30 seconds.

References config_, last_state_change_, and kcenon::common::resilience::circuit_breaker_config::timeout.

Referenced by allow_request().

Here is the caller graph for this function:

◆ to_json()

auto kcenon::common::resilience::circuit_breaker::to_json ( ) const -> std::string
inlinenodiscardoverridevirtual

Get statistics as JSON string.

Returns
JSON representation of current statistics

Implements kcenon::common::interfaces::IStats.

Definition at line 245 of file circuit_breaker.h.

246 {
247 const auto snapshot = get_snapshot();
248 return snapshot.to_json();
249 }
virtual auto get_snapshot() const -> stats_snapshot
Get a complete statistics snapshot with metadata.

References kcenon::common::interfaces::IStats::get_snapshot().

Here is the call graph for this function:

◆ transition_to_closed()

auto kcenon::common::resilience::circuit_breaker::transition_to_closed ( ) -> void
inlineprivate

Transition to CLOSED state. Must be called with mutex locked.

Definition at line 277 of file circuit_breaker.h.

278 {
279 state_.store(circuit_state::CLOSED, std::memory_order_release);
283 last_state_change_ = clock_type::now();
284 }
auto reset() -> void
Clear all recorded failures.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ transition_to_half_open()

auto kcenon::common::resilience::circuit_breaker::transition_to_half_open ( ) -> void
inlineprivate

Transition to HALF_OPEN state. Must be called with mutex locked.

Definition at line 302 of file circuit_breaker.h.

303 {
304 state_.store(circuit_state::HALF_OPEN, std::memory_order_release);
307 last_state_change_ = clock_type::now();
308 }

References consecutive_successes_, kcenon::common::resilience::HALF_OPEN, half_open_requests_, last_state_change_, and state_.

Referenced by allow_request().

Here is the caller graph for this function:

◆ transition_to_open()

auto kcenon::common::resilience::circuit_breaker::transition_to_open ( ) -> void
inlineprivate

Transition to OPEN state. Must be called with mutex locked.

Definition at line 290 of file circuit_breaker.h.

291 {
292 state_.store(circuit_state::OPEN, std::memory_order_release);
295 last_state_change_ = clock_type::now();
296 }

References consecutive_successes_, half_open_requests_, last_state_change_, kcenon::common::resilience::OPEN, and state_.

Referenced by record_failure().

Here is the caller graph for this function:

Member Data Documentation

◆ config_

circuit_breaker_config kcenon::common::resilience::circuit_breaker::config_
private

◆ consecutive_successes_

std::size_t kcenon::common::resilience::circuit_breaker::consecutive_successes_
private

◆ failure_window_

failure_window kcenon::common::resilience::circuit_breaker::failure_window_
private

Definition at line 312 of file circuit_breaker.h.

Referenced by get_stats(), record_failure(), and transition_to_closed().

◆ half_open_requests_

std::size_t kcenon::common::resilience::circuit_breaker::half_open_requests_
private

◆ last_state_change_

time_point kcenon::common::resilience::circuit_breaker::last_state_change_
private

◆ mutex_

std::mutex kcenon::common::resilience::circuit_breaker::mutex_
mutableprivate

Definition at line 316 of file circuit_breaker.h.

Referenced by allow_request(), get_stats(), record_failure(), and record_success().

◆ state_

std::atomic<circuit_state> kcenon::common::resilience::circuit_breaker::state_
private

The documentation for this class was generated from the following file: