#include <atomic>
#include <chrono>
#include <iostream>
#include <random>
#include <thread>
using namespace std::chrono_literals;
private:
std::uniform_real_distribution<>
dist_;
public:
:
rng_(std::random_device{}())
kcenon::common::Result<std::string>
call() {
std::this_thread::sleep_for(50ms);
return kcenon::common::Result<std::string>::err(
error_info{monitoring_error_code::service_unavailable,
);
}
return kcenon::common::ok(std::string("Service response: SUCCESS"));
}
}
};
std::cout << "=== Circuit Breaker Pattern ===" << std::endl;
std::cout << std::endl;
config.failure_threshold = 3;
config.timeout = 5000ms;
std::cout << "Circuit Breaker Configuration:" << std::endl;
std::cout << "- Failure threshold: " << config.failure_threshold << std::endl;
std::cout << "- Reset timeout: 5000ms" << std::endl;
std::cout << std::endl;
std::cout << "Making calls to unreliable service (70% failure rate):" << std::endl;
std::cout << std::endl;
for (int i = 0; i < 10; ++i) {
std::cout << "Call " << (i + 1) << ": ";
auto result = execute_with_circuit_breaker<std::string>(breaker, "external_service", [&service]() {
return service.call();
});
if (result.is_ok()) {
std::cout << "SUCCESS" << std::endl;
} else {
std::cout << "FAILED" << std::endl;
}
std::this_thread::sleep_for(200ms);
}
std::cout << std::endl;
auto stats = breaker.get_stats();
std::cout << "Circuit Breaker Stats:" << std::endl;
for (const auto& [key, val] : stats) {
std::visit([&key](const auto& v) {
std::cout << "- " << key << ": " << v << std::endl;
}, val);
}
std::cout << std::endl;
}
std::cout << "=== Retry Policy with Exponential Backoff ===" << std::endl;
std::cout << std::endl;
retry_cfg.
strategy = retry_strategy::exponential_backoff;
std::cout << "Retry Policy Configuration:" << std::endl;
std::cout << "- Strategy: Exponential backoff" << std::endl;
std::cout << "- Max attempts: 5" << std::endl;
std::cout << "- Initial delay: 100ms" << std::endl;
std::cout << std::endl;
std::cout << "Making calls with retry policy:" << std::endl;
std::cout << std::endl;
for (int i = 0; i < 5; ++i) {
std::cout << "Request " << (i + 1) << ": ";
auto result = policy.execute([&service]() {
});
if (result.is_ok()) {
std::cout << "SUCCESS" << std::endl;
} else {
std::cout << "FAILED after retries" << std::endl;
}
}
std::cout << std::endl;
auto metrics = policy.get_metrics();
std::cout << "Retry Policy Metrics:" << std::endl;
std::cout << "- Total executions: " << metrics.total_executions << std::endl;
std::cout << "- Successful: " << metrics.successful_executions << std::endl;
std::cout << "- Failed: " << metrics.failed_executions << std::endl;
std::cout << "- Total retries: " << metrics.total_retries << std::endl;
std::cout << std::endl;
}
std::cout << "=== Combined Reliability Patterns ===" << std::endl;
std::cout << std::endl;
cb_config.failure_threshold = 3;
retry_cfg2.
strategy = retry_strategy::exponential_backoff;
std::cout << "Combining Circuit Breaker + Retry Policy" << std::endl;
std::cout << std::endl;
for (int i = 0; i < 10; ++i) {
std::cout << "Request " << (i + 1) << ": ";
auto result = execute_with_circuit_breaker<std::string>(breaker, "primary", [&]() {
return policy2.execute([&]() {
return primary_service.call();
});
});
if (result.is_ok()) {
std::cout << "SUCCESS" << std::endl;
} else {
std::cout << "FAILED" << std::endl;
}
std::this_thread::sleep_for(300ms);
}
std::cout << std::endl;
auto cb_stats = breaker.get_stats();
std::cout << "Circuit Breaker Stats:" << std::endl;
for (const auto& [key, val] : cb_stats) {
std::visit([&key](const auto& v) {
std::cout << "- " << key << ": " << v << std::endl;
}, val);
}
std::cout << std::endl;
std::cout << "Retry Policy:" << std::endl;
std::cout << std::endl;
}
std::cout << "=== Graceful Degradation and Reliability Patterns ===" << std::endl;
std::cout << std::endl;
try {
std::cout << std::string(70, '=') << std::endl;
std::cout << std::endl;
std::cout << std::string(70, '=') << std::endl;
std::cout << std::endl;
std::cout << std::string(70, '=') << std::endl;
std::cout << std::endl;
std::cout << "=== All Reliability Patterns Demonstrated Successfully ===" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}
Circuit breaker integration for monitoring_system.
Retry executor template class.
kcenon::common::Result< std::string > call()
void set_failure_rate(double rate)
std::atomic< int > call_count_
std::uniform_real_distribution dist_
unreliable_service(double failure_rate)
Monitoring system specific error codes.
Fault tolerance manager coordinating circuit breakers and retries.
void demonstrate_retry_policy()
void demonstrate_circuit_breaker()
void demonstrate_combined_patterns()
common::resilience::circuit_breaker circuit_breaker
common::resilience::circuit_breaker_config circuit_breaker_config
Result pattern type definitions for monitoring system.
Retry strategies with backoff for monitoring operations.
Extended error information with context.
common::error_info to_common_error() const
Convert to common_system error_info.
double backoff_multiplier
std::chrono::milliseconds initial_delay