Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
graceful_degradation_example.cpp File Reference

Demonstration of reliability patterns and graceful degradation. More...

#include <atomic>
#include <chrono>
#include <iostream>
#include <random>
#include <thread>
#include "kcenon/monitoring/core/error_codes.h"
#include "kcenon/monitoring/core/result_types.h"
#include "kcenon/monitoring/reliability/circuit_breaker.h"
#include "kcenon/monitoring/reliability/fault_tolerance_manager.h"
#include "kcenon/monitoring/reliability/retry_policy.h"
Include dependency graph for graceful_degradation_example.cpp:

Go to the source code of this file.

Classes

class  unreliable_service
 

Functions

void demonstrate_circuit_breaker ()
 
void demonstrate_retry_policy ()
 
void demonstrate_combined_patterns ()
 
int main ()
 

Detailed Description

Demonstration of reliability patterns and graceful degradation.

Definition in file graceful_degradation_example.cpp.

Function Documentation

◆ demonstrate_circuit_breaker()

void demonstrate_circuit_breaker ( )
Examples
graceful_degradation_example.cpp, and health_reliability_example.cpp.

Definition at line 68 of file graceful_degradation_example.cpp.

68 {
69 std::cout << "=== Circuit Breaker Pattern ===" << std::endl;
70 std::cout << std::endl;
71
72 unreliable_service service(0.7); // 70% failure rate
73
75 config.failure_threshold = 3;
76 config.timeout = 5000ms;
77
78 circuit_breaker breaker(config);
79
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;
84
85 std::cout << "Making calls to unreliable service (70% failure rate):" << std::endl;
86 std::cout << std::endl;
87
88 for (int i = 0; i < 10; ++i) {
89 std::cout << "Call " << (i + 1) << ": ";
90
91 auto result = execute_with_circuit_breaker<std::string>(breaker, "external_service", [&service]() {
92 return service.call();
93 });
94
95 if (result.is_ok()) {
96 std::cout << "SUCCESS" << std::endl;
97 } else {
98 std::cout << "FAILED" << std::endl;
99 }
100
101 std::this_thread::sleep_for(200ms);
102 }
103
104 std::cout << std::endl;
105
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;
111 }, val);
112 }
113 std::cout << std::endl;
114}
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

References kcenon::monitoring::execute_with_circuit_breaker(), and kcenon::monitoring::service.

Referenced by main().

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

◆ demonstrate_combined_patterns()

void demonstrate_combined_patterns ( )
Examples
graceful_degradation_example.cpp.

Definition at line 166 of file graceful_degradation_example.cpp.

166 {
167 std::cout << "=== Combined Reliability Patterns ===" << std::endl;
168 std::cout << std::endl;
169
170 unreliable_service primary_service(0.5);
171
172 circuit_breaker_config cb_config;
173 cb_config.failure_threshold = 3;
174 circuit_breaker breaker(cb_config);
175
176 retry_config retry_cfg2;
177 retry_cfg2.max_attempts = 3;
178 retry_cfg2.strategy = retry_strategy::exponential_backoff;
179 retry_cfg2.initial_delay = 100ms;
180
181 retry_executor<std::string> policy2("combined_retry", retry_cfg2);
182
183 std::cout << "Combining Circuit Breaker + Retry Policy" << std::endl;
184 std::cout << std::endl;
185
186 for (int i = 0; i < 10; ++i) {
187 std::cout << "Request " << (i + 1) << ": ";
188
189 auto result = execute_with_circuit_breaker<std::string>(breaker, "primary", [&]() {
190 return policy2.execute([&]() {
191 return primary_service.call();
192 });
193 });
194
195 if (result.is_ok()) {
196 std::cout << "SUCCESS" << std::endl;
197 } else {
198 std::cout << "FAILED" << std::endl;
199 }
200
201 std::this_thread::sleep_for(300ms);
202 }
203
204 std::cout << std::endl;
205
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;
211 }, val);
212 }
213 std::cout << std::endl;
214
215 auto retry_metrics = policy2.get_metrics();
216 std::cout << "Retry Policy:" << std::endl;
217 std::cout << "- Total executions: " << retry_metrics.total_executions << std::endl;
218 std::cout << "- Total retries: " << retry_metrics.total_retries << std::endl;
219 std::cout << std::endl;
220}
Retry executor template class.
std::chrono::milliseconds initial_delay

References unreliable_service::call(), kcenon::monitoring::retry_executor< T >::execute(), kcenon::monitoring::execute_with_circuit_breaker(), kcenon::monitoring::retry_executor< T >::get_metrics(), kcenon::monitoring::retry_config::initial_delay, kcenon::monitoring::retry_config::max_attempts, kcenon::monitoring::retry_config::strategy, kcenon::monitoring::retry_metrics::total_executions, and kcenon::monitoring::retry_metrics::total_retries.

Referenced by main().

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

◆ demonstrate_retry_policy()

void demonstrate_retry_policy ( )
Examples
graceful_degradation_example.cpp, and health_reliability_example.cpp.

Definition at line 117 of file graceful_degradation_example.cpp.

117 {
118 std::cout << "=== Retry Policy with Exponential Backoff ===" << std::endl;
119 std::cout << std::endl;
120
121 unreliable_service service(0.4); // 40% failure rate
122
123 retry_config retry_cfg;
124 retry_cfg.max_attempts = 5;
125 retry_cfg.strategy = retry_strategy::exponential_backoff;
126 retry_cfg.initial_delay = 100ms;
127 retry_cfg.backoff_multiplier = 2.0;
128
129 retry_executor<std::string> policy("service_retry", retry_cfg);
130
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;
136
137 std::cout << "Making calls with retry policy:" << std::endl;
138 std::cout << std::endl;
139
140 for (int i = 0; i < 5; ++i) {
141 std::cout << "Request " << (i + 1) << ": ";
142
143 auto result = policy.execute([&service]() {
144 return service.call();
145 });
146
147 if (result.is_ok()) {
148 std::cout << "SUCCESS" << std::endl;
149 } else {
150 std::cout << "FAILED after retries" << std::endl;
151 }
152 }
153
154 std::cout << std::endl;
155
156 auto metrics = policy.get_metrics();
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;
163}

References kcenon::monitoring::retry_config::backoff_multiplier, kcenon::monitoring::retry_executor< T >::execute(), kcenon::monitoring::retry_executor< T >::get_metrics(), kcenon::monitoring::retry_config::initial_delay, kcenon::monitoring::retry_config::max_attempts, kcenon::monitoring::service, kcenon::monitoring::retry_config::strategy, and kcenon::monitoring::retry_metrics::total_executions.

Referenced by main().

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

◆ main()

int main ( )

Definition at line 222 of file graceful_degradation_example.cpp.

222 {
223 std::cout << "=== Graceful Degradation and Reliability Patterns ===" << std::endl;
224 std::cout << std::endl;
225
226 try {
228 std::cout << std::string(70, '=') << std::endl;
229 std::cout << std::endl;
230
232 std::cout << std::string(70, '=') << std::endl;
233 std::cout << std::endl;
234
236 std::cout << std::string(70, '=') << std::endl;
237 std::cout << std::endl;
238
239 std::cout << "=== All Reliability Patterns Demonstrated Successfully ===" << std::endl;
240
241 } catch (const std::exception& e) {
242 std::cerr << "Exception: " << e.what() << std::endl;
243 return 1;
244 }
245
246 return 0;
247}
void demonstrate_retry_policy()
void demonstrate_circuit_breaker()
void demonstrate_combined_patterns()

References demonstrate_circuit_breaker(), demonstrate_combined_patterns(), and demonstrate_retry_policy().

Here is the call graph for this function: