32using namespace std::chrono_literals;
38 std::uniform_real_distribution<>
dist_;
44 :
rng_(std::random_device{}())
48 kcenon::common::Result<std::string>
call() {
50 std::this_thread::sleep_for(50ms);
53 return kcenon::common::Result<std::string>::err(
54 error_info{monitoring_error_code::service_unavailable,
59 return kcenon::common::ok(std::string(
"Service response: SUCCESS"));
69 std::cout <<
"=== Circuit Breaker Pattern ===" << std::endl;
70 std::cout << std::endl;
75 config.failure_threshold = 3;
76 config.timeout = 5000ms;
80 std::cout <<
"Circuit Breaker Configuration:" << std::endl;
81 std::cout <<
"- Failure threshold: " << config.failure_threshold << std::endl;
82 std::cout <<
"- Reset timeout: 5000ms" << std::endl;
83 std::cout << std::endl;
85 std::cout <<
"Making calls to unreliable service (70% failure rate):" << std::endl;
86 std::cout << std::endl;
88 for (
int i = 0; i < 10; ++i) {
89 std::cout <<
"Call " << (i + 1) <<
": ";
96 std::cout <<
"SUCCESS" << std::endl;
98 std::cout <<
"FAILED" << std::endl;
101 std::this_thread::sleep_for(200ms);
104 std::cout << std::endl;
106 auto stats = breaker.get_stats();
107 std::cout <<
"Circuit Breaker Stats:" << std::endl;
108 for (
const auto& [key, val] : stats) {
109 std::visit([&key](
const auto& v) {
110 std::cout <<
"- " << key <<
": " << v << std::endl;
113 std::cout << std::endl;
118 std::cout <<
"=== Retry Policy with Exponential Backoff ===" << std::endl;
119 std::cout << std::endl;
125 retry_cfg.
strategy = retry_strategy::exponential_backoff;
131 std::cout <<
"Retry Policy Configuration:" << std::endl;
132 std::cout <<
"- Strategy: Exponential backoff" << std::endl;
133 std::cout <<
"- Max attempts: 5" << std::endl;
134 std::cout <<
"- Initial delay: 100ms" << std::endl;
135 std::cout << std::endl;
137 std::cout <<
"Making calls with retry policy:" << std::endl;
138 std::cout << std::endl;
140 for (
int i = 0; i < 5; ++i) {
141 std::cout <<
"Request " << (i + 1) <<
": ";
147 if (result.is_ok()) {
148 std::cout <<
"SUCCESS" << std::endl;
150 std::cout <<
"FAILED after retries" << std::endl;
154 std::cout << std::endl;
157 std::cout <<
"Retry Policy Metrics:" << std::endl;
158 std::cout <<
"- Total executions: " << metrics.
total_executions << std::endl;
159 std::cout <<
"- Successful: " << metrics.successful_executions << std::endl;
160 std::cout <<
"- Failed: " << metrics.failed_executions << std::endl;
161 std::cout <<
"- Total retries: " << metrics.total_retries << std::endl;
162 std::cout << std::endl;
167 std::cout <<
"=== Combined Reliability Patterns ===" << std::endl;
168 std::cout << std::endl;
173 cb_config.failure_threshold = 3;
178 retry_cfg2.
strategy = retry_strategy::exponential_backoff;
183 std::cout <<
"Combining Circuit Breaker + Retry Policy" << std::endl;
184 std::cout << std::endl;
186 for (
int i = 0; i < 10; ++i) {
187 std::cout <<
"Request " << (i + 1) <<
": ";
191 return primary_service.
call();
195 if (result.is_ok()) {
196 std::cout <<
"SUCCESS" << std::endl;
198 std::cout <<
"FAILED" << std::endl;
201 std::this_thread::sleep_for(300ms);
204 std::cout << std::endl;
206 auto cb_stats = breaker.get_stats();
207 std::cout <<
"Circuit Breaker Stats:" << std::endl;
208 for (
const auto& [key, val] : cb_stats) {
209 std::visit([&key](
const auto& v) {
210 std::cout <<
"- " << key <<
": " << v << std::endl;
213 std::cout << std::endl;
216 std::cout <<
"Retry Policy:" << std::endl;
219 std::cout << std::endl;
223 std::cout <<
"=== Graceful Degradation and Reliability Patterns ===" << std::endl;
224 std::cout << std::endl;
228 std::cout << std::string(70,
'=') << std::endl;
229 std::cout << std::endl;
232 std::cout << std::string(70,
'=') << std::endl;
233 std::cout << std::endl;
236 std::cout << std::string(70,
'=') << std::endl;
237 std::cout << std::endl;
239 std::cout <<
"=== All Reliability Patterns Demonstrated Successfully ===" << std::endl;
241 }
catch (
const std::exception& e) {
242 std::cerr <<
"Exception: " << e.what() << std::endl;
Circuit breaker integration for monitoring_system.
Retry executor template class.
retry_metrics get_metrics() const
Get retry metrics.
common::Result< T > execute(Func &&func)
Execute a function with retry logic.
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::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
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