Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::retry_executor< T > Class Template Reference

Retry executor template class. More...

#include <retry_policy.h>

Collaboration diagram for kcenon::monitoring::retry_executor< T >:
Collaboration graph

Public Types

using config = retry_config
 

Public Member Functions

 retry_executor ()
 
 retry_executor (const std::string &name)
 
 retry_executor (const std::string &name, const config &cfg)
 
template<typename Func >
common::Result< T > execute (Func &&func)
 Execute a function with retry logic.
 
retry_metrics get_metrics () const
 Get retry metrics.
 
void reset_metrics ()
 Reset metrics.
 
const std::string & get_name () const
 Get executor name.
 

Private Member Functions

std::chrono::milliseconds calculate_delay (size_t attempt) const
 Calculate delay for the current attempt.
 

Static Private Member Functions

static size_t fibonacci (size_t n)
 Calculate Fibonacci number.
 

Private Attributes

std::string name_
 
config config_
 
retry_metrics metrics_
 

Detailed Description

template<typename T>
class kcenon::monitoring::retry_executor< T >

Retry executor template class.

Executes operations with configurable retry logic.

Template Parameters
TThe return value type of operations
Examples
graceful_degradation_example.cpp.

Definition at line 134 of file retry_policy.h.

Member Typedef Documentation

◆ config

template<typename T >
using kcenon::monitoring::retry_executor< T >::config = retry_config

Definition at line 136 of file retry_policy.h.

Constructor & Destructor Documentation

◆ retry_executor() [1/3]

template<typename T >
kcenon::monitoring::retry_executor< T >::retry_executor ( )
inline

Definition at line 138 of file retry_policy.h.

◆ retry_executor() [2/3]

template<typename T >
kcenon::monitoring::retry_executor< T >::retry_executor ( const std::string & name)
inlineexplicit

Definition at line 140 of file retry_policy.h.

141 : name_(name), config_() {}

◆ retry_executor() [3/3]

template<typename T >
kcenon::monitoring::retry_executor< T >::retry_executor ( const std::string & name,
const config & cfg )
inlineexplicit

Definition at line 143 of file retry_policy.h.

144 : name_(name), config_(cfg) {}

Member Function Documentation

◆ calculate_delay()

template<typename T >
std::chrono::milliseconds kcenon::monitoring::retry_executor< T >::calculate_delay ( size_t attempt) const
inlineprivate

Calculate delay for the current attempt.

Parameters
attemptCurrent attempt number (0-indexed)
Returns
Delay duration

Definition at line 213 of file retry_policy.h.

213 {
214 std::chrono::milliseconds delay;
215
216 switch (config_.strategy) {
219 break;
220
222 auto multiplier = std::pow(config_.backoff_multiplier, static_cast<double>(attempt - 1));
223 auto delay_ms = static_cast<long long>(config_.initial_delay.count() * multiplier);
224 delay = std::chrono::milliseconds(delay_ms);
225 break;
226 }
227
229 delay = config_.initial_delay * attempt;
230 break;
231
234 break;
235 }
236
237 default:
239 break;
240 }
241
242 return std::min(delay, config_.max_delay);
243 }
static size_t fibonacci(size_t n)
Calculate Fibonacci number.
@ delay
Delay requests until resources are available.
std::chrono::milliseconds initial_delay
std::chrono::milliseconds max_delay

References kcenon::monitoring::retry_config::backoff_multiplier, kcenon::monitoring::retry_executor< T >::config_, kcenon::monitoring::delay, kcenon::monitoring::exponential_backoff, kcenon::monitoring::retry_executor< T >::fibonacci(), kcenon::monitoring::fibonacci_backoff, kcenon::monitoring::fixed_delay, kcenon::monitoring::retry_config::initial_delay, kcenon::monitoring::linear_backoff, kcenon::monitoring::retry_config::max_delay, and kcenon::monitoring::retry_config::strategy.

Referenced by kcenon::monitoring::retry_executor< T >::execute().

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

◆ execute()

template<typename T >
template<typename Func >
common::Result< T > kcenon::monitoring::retry_executor< T >::execute ( Func && func)
inline

Execute a function with retry logic.

Template Parameters
FuncThe function type to execute (must return common::Result<T>)
Parameters
funcThe function to execute
Returns
common::Result<T> containing success value or error from last attempt

Definition at line 154 of file retry_policy.h.

154 {
156
157 common::Result<T> last_result = common::Result<T>::err(error_info(monitoring_error_code::operation_failed, "No attempts made").to_common_error());
158
159 for (size_t attempt = 0; attempt < config_.max_attempts; ++attempt) {
160 if (attempt > 0) {
162 auto delay = calculate_delay(attempt);
163 std::this_thread::sleep_for(delay);
164 }
165
166 last_result = func();
167
168 if (last_result.is_ok()) {
170 return last_result;
171 }
172
173 if (config_.should_retry) {
174 auto err_info = error_info::from_common_error(last_result.error());
175 if (!config_.should_retry(err_info)) {
177 return last_result;
178 }
179 }
180 }
181
183 return last_result;
184 }
std::chrono::milliseconds calculate_delay(size_t attempt) const
Calculate delay for the current attempt.
static error_info from_common_error(const common::error_info &common_err)
Create from common_system error_info.
std::function< bool(const error_info &)> should_retry

References kcenon::monitoring::retry_executor< T >::calculate_delay(), kcenon::monitoring::retry_executor< T >::config_, kcenon::monitoring::delay, kcenon::monitoring::retry_metrics::failed_executions, kcenon::monitoring::error_info::from_common_error(), kcenon::monitoring::retry_config::max_attempts, kcenon::monitoring::retry_executor< T >::metrics_, kcenon::monitoring::operation_failed, kcenon::monitoring::retry_config::should_retry, kcenon::monitoring::retry_metrics::successful_executions, kcenon::monitoring::retry_metrics::total_executions, and kcenon::monitoring::retry_metrics::total_retries.

Referenced by demonstrate_combined_patterns(), demonstrate_retry_policy(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

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

◆ fibonacci()

template<typename T >
static size_t kcenon::monitoring::retry_executor< T >::fibonacci ( size_t n)
inlinestaticprivate

Calculate Fibonacci number.

Parameters
nIndex in Fibonacci sequence
Returns
Fibonacci number at index n

Definition at line 250 of file retry_policy.h.

250 {
251 if (n <= 1) return 1;
252
253 size_t prev = 1;
254 size_t curr = 1;
255
256 for (size_t i = 2; i <= n; ++i) {
257 size_t next = prev + curr;
258 prev = curr;
259 curr = next;
260 }
261
262 return curr;
263 }

Referenced by kcenon::monitoring::retry_executor< T >::calculate_delay().

Here is the caller graph for this function:

◆ get_metrics()

template<typename T >
retry_metrics kcenon::monitoring::retry_executor< T >::get_metrics ( ) const
inline

Get retry metrics.

Definition at line 189 of file retry_policy.h.

189 {
190 return metrics_;
191 }

References kcenon::monitoring::retry_executor< T >::metrics_.

Referenced by demonstrate_combined_patterns(), demonstrate_retry_policy(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ get_name()

template<typename T >
const std::string & kcenon::monitoring::retry_executor< T >::get_name ( ) const
inline

Get executor name.

Definition at line 203 of file retry_policy.h.

203 {
204 return name_;
205 }

References kcenon::monitoring::retry_executor< T >::name_.

◆ reset_metrics()

template<typename T >
void kcenon::monitoring::retry_executor< T >::reset_metrics ( )
inline

Reset metrics.

Definition at line 196 of file retry_policy.h.

References kcenon::monitoring::retry_executor< T >::metrics_, and kcenon::monitoring::retry_metrics::reset().

Referenced by TEST_F().

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

Member Data Documentation

◆ config_

◆ metrics_

◆ name_

template<typename T >
std::string kcenon::monitoring::retry_executor< T >::name_
private

Definition at line 265 of file retry_policy.h.

Referenced by kcenon::monitoring::retry_executor< T >::get_name().


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