24#include <unordered_map>
81template<
typename T,
typename Func>
83 if (!cb.allow_request()) {
85 "Circuit breaker '" + name +
"' is open");
88 auto op_result = func();
89 if (op_result.is_ok()) {
128 template<
typename Func>
140 if (op_result.is_ok()) {
157 template<
typename Func>
161 auto future_result = std::async(std::launch::async, [
this, func = std::forward<Func>(func)]()
mutable {
165 if (future_result.wait_for(timeout) == std::future_status::timeout) {
169 "Operation timed out after " + std::to_string(timeout.count()) +
"ms");
172 auto op_result = future_result.get();
173 if (op_result.is_ok()) {
189 if (state == circuit_state::OPEN) {
190 return common::ok(
false);
193 return common::ok(
true);
220 template<
typename Func>
228 template<
typename Func>
244 template<
typename Func>
273 std::lock_guard<std::mutex> lock(
mutex_);
278 std::lock_guard<std::mutex> lock(
mutex_);
287 std::lock_guard<std::mutex> lock(
mutex_);
292 std::lock_guard<std::mutex> lock(
mutex_);
293 std::vector<std::string> names;
296 names.push_back(pair.first);
302 std::lock_guard<std::mutex> lock(
mutex_);
308 std::unordered_map<std::string, std::shared_ptr<circuit_breaker>>
registry_;
318 std::lock_guard<std::mutex> lock(
mutex_);
323 std::shared_ptr<retry_executor<T>>
get_executor(
const std::string& name) {
324 std::lock_guard<std::mutex> lock(
mutex_);
327 return std::any_cast<std::shared_ptr<retry_executor<T>>>(it->second);
333 std::lock_guard<std::mutex> lock(
mutex_);
338 std::lock_guard<std::mutex> lock(
mutex_);
339 std::vector<std::string> names;
342 names.push_back(pair.first);
348 std::lock_guard<std::mutex> lock(
mutex_);
364 std::lock_guard<std::mutex> lock(
mutex_);
369 std::shared_ptr<fault_tolerance_manager<T>>
get_manager(
const std::string& name) {
370 std::lock_guard<std::mutex> lock(
mutex_);
373 return std::any_cast<std::shared_ptr<fault_tolerance_manager<T>>>(it->second);
379 std::lock_guard<std::mutex> lock(
mutex_);
384 std::lock_guard<std::mutex> lock(
mutex_);
385 std::vector<std::string> names;
388 names.push_back(pair.first);
394 std::lock_guard<std::mutex> lock(
mutex_);
Circuit breaker integration for monitoring_system.
Circuit breaker registry.
void register_circuit_breaker(const std::string &name, std::shared_ptr< circuit_breaker > breaker)
std::vector< std::string > get_all_names() const
void remove_circuit_breaker(const std::string &name)
std::shared_ptr< circuit_breaker > get_circuit_breaker(const std::string &name)
std::unordered_map< std::string, std::shared_ptr< circuit_breaker > > registry_
Fault tolerance manager template class.
fault_tolerance_manager(const std::string &name, const fault_tolerance_config &cfg)
common::Result< T > execute_with_timeout(Func &&func, std::chrono::milliseconds timeout)
Execute a function with timeout.
common::Result< T > execute_retry_first(Func &&func)
common::Result< bool > is_healthy()
Check if fault tolerance manager is healthy.
fault_tolerance_manager()
fault_tolerance_metrics get_metrics() const
Get fault tolerance metrics.
common::Result< T > execute_circuit_breaker_first(Func &&func)
fault_tolerance_manager(const std::string &name)
common::Result< T > execute(Func &&func)
Execute a function with fault tolerance.
const std::string & get_name() const
Get manager name.
std::unique_ptr< circuit_breaker > circuit_breaker_
std::unique_ptr< retry_executor< T > > retry_executor_
common::Result< T > execute_internal(Func &&func)
fault_tolerance_config config_
fault_tolerance_metrics metrics_
Fault tolerance manager registry.
std::vector< std::string > get_all_names() const
void register_manager(const std::string &name, std::shared_ptr< fault_tolerance_manager< T > > manager)
std::unordered_map< std::string, std::any > registry_
std::shared_ptr< fault_tolerance_manager< T > > get_manager(const std::string &name)
void remove_manager(const std::string &name)
std::unordered_map< std::string, std::any > registry_
std::shared_ptr< retry_executor< T > > get_executor(const std::string &name)
std::vector< std::string > get_all_names() const
void remove_executor(const std::string &name)
void register_executor(const std::string &name, std::shared_ptr< retry_executor< T > > executor)
Retry executor template class.
Error boundary with degradation levels for fault isolation.
common::Result< T > execute_with_circuit_breaker(circuit_breaker &cb, const std::string &name, Func &&func)
Execute an operation through a circuit breaker.
common::resilience::circuit_breaker circuit_breaker
circuit_breaker_registry & global_circuit_breaker_registry()
Get global circuit breaker registry.
fault_tolerance_registry & global_fault_tolerance_registry()
Get global fault tolerance manager registry.
common::resilience::circuit_breaker_config circuit_breaker_config
retry_executor_registry & global_retry_executor_registry()
Get global retry executor registry.
Retry strategies with backoff for monitoring operations.
Extended error information with context.
Fault tolerance configuration.
bool enable_circuit_breaker
circuit_breaker_config circuit_config
bool circuit_breaker_first
size_t circuit_breaker_rejections
double get_overall_success_rate() const
size_t successful_operations
bool validate() const
Validate configuration.