mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
mal_toolkit::ExponentialBackoff< ChronoType > Class Template Reference

A utility class for implementing exponential backoff strategies. More...

#include "backoffs.hpp"

Collaboration diagram for mal_toolkit::ExponentialBackoff< ChronoType >:
[legend]

Public Member Functions

 ExponentialBackoff (ChronoType initial_delay, ChronoType max_delay, double multiplier=2.0, double divisor=2.0, double jitter_factor=0.2)
 Constructor to initialize the exponential backoff strategy.
 
constexpr ChronoType get_current_delay () noexcept
 Get the current backoff delay.
 
constexpr double get_current_delay_double () noexcept
 Get the current backoff delay as a double.
 
constexpr void increase_delay () noexcept
 Increase the backoff delay using exponential factor.
 
constexpr void decrease_delay () noexcept
 Decrease the backoff delay using divisor factor.
 
constexpr void reset_delay () noexcept
 Reset the backoff delay to its initial value.
 

Private Attributes

const ChronoType initial_delay_
 The initial delay before the first retry.
 
const ChronoType max_delay_
 The maximum delay allowed between retries.
 
const double multiplier_
 The factor by which the delay is multiplied after each retry.
 
const double divisor_
 The factor by which the delay is divided when decreasing the delay.
 
const double jitter_factor_
 
std::uniform_real_distribution< double > uniform_dist_
 Uniform distribution for generating jitter.
 
ChronoType current_delay_
 The current backoff delay.
 
std::default_random_engine rng_
 Random number generator engine.
 

Detailed Description

template<typename ChronoType>
class mal_toolkit::ExponentialBackoff< ChronoType >

A utility class for implementing exponential backoff strategies.

Exponential backoff is a strategy where the delay between retries increases exponentially. This class provides functionality to manage and manipulate the backoff delay.

Template Parameters
ChronoTypeThe type representing the time duration (e.g., std::chrono::milliseconds).

Definition at line 16 of file backoffs.hpp.

Constructor & Destructor Documentation

◆ ExponentialBackoff()

template<typename ChronoType >
mal_toolkit::ExponentialBackoff< ChronoType >::ExponentialBackoff ( ChronoType initial_delay,
ChronoType max_delay,
double multiplier = 2.0,
double divisor = 2.0,
double jitter_factor = 0.2 )
inline

Constructor to initialize the exponential backoff strategy.

Parameters
initial_delayThe initial delay before the first retry.
max_delayThe maximum delay allowed between retries.
multiplierThe factor by which the delay is multiplied after each retry.
divisorThe factor by which the delay is divided when decreasing the delay.
jitter_factorThe factor by which jitter is applied to the delay to avoid synchronized retries.

Definition at line 29 of file backoffs.hpp.

Member Function Documentation

◆ decrease_delay()

template<typename ChronoType >
void mal_toolkit::ExponentialBackoff< ChronoType >::decrease_delay ( )
inlineconstexprnoexcept

Decrease the backoff delay using divisor factor.

Definition at line 77 of file backoffs.hpp.

◆ get_current_delay()

template<typename ChronoType >
ChronoType mal_toolkit::ExponentialBackoff< ChronoType >::get_current_delay ( )
inlineconstexprnoexcept

Get the current backoff delay.

Returns
The current backoff delay with applied jitter.

Definition at line 47 of file backoffs.hpp.

Here is the caller graph for this function:

◆ get_current_delay_double()

template<typename ChronoType >
double mal_toolkit::ExponentialBackoff< ChronoType >::get_current_delay_double ( )
inlineconstexprnoexcept

Get the current backoff delay as a double.

Returns
The current backoff delay with applied jitter.

Definition at line 59 of file backoffs.hpp.

◆ increase_delay()

template<typename ChronoType >
void mal_toolkit::ExponentialBackoff< ChronoType >::increase_delay ( )
inlineconstexprnoexcept

Increase the backoff delay using exponential factor.

Definition at line 69 of file backoffs.hpp.

Here is the caller graph for this function:

◆ reset_delay()

template<typename ChronoType >
void mal_toolkit::ExponentialBackoff< ChronoType >::reset_delay ( )
inlineconstexprnoexcept

Reset the backoff delay to its initial value.

Definition at line 86 of file backoffs.hpp.

Member Data Documentation

◆ current_delay_

template<typename ChronoType >
ChronoType mal_toolkit::ExponentialBackoff< ChronoType >::current_delay_
private

The current backoff delay.

Definition at line 98 of file backoffs.hpp.

◆ divisor_

template<typename ChronoType >
const double mal_toolkit::ExponentialBackoff< ChronoType >::divisor_
private

The factor by which the delay is divided when decreasing the delay.

Definition at line 92 of file backoffs.hpp.

◆ initial_delay_

template<typename ChronoType >
const ChronoType mal_toolkit::ExponentialBackoff< ChronoType >::initial_delay_
private

The initial delay before the first retry.

Definition at line 89 of file backoffs.hpp.

◆ jitter_factor_

template<typename ChronoType >
const double mal_toolkit::ExponentialBackoff< ChronoType >::jitter_factor_
private

The factor by which jitter is applied to the delay to avoid synchronized retries.

Definition at line 94 of file backoffs.hpp.

◆ max_delay_

template<typename ChronoType >
const ChronoType mal_toolkit::ExponentialBackoff< ChronoType >::max_delay_
private

The maximum delay allowed between retries.

Definition at line 90 of file backoffs.hpp.

◆ multiplier_

template<typename ChronoType >
const double mal_toolkit::ExponentialBackoff< ChronoType >::multiplier_
private

The factor by which the delay is multiplied after each retry.

Definition at line 91 of file backoffs.hpp.

◆ rng_

template<typename ChronoType >
std::default_random_engine mal_toolkit::ExponentialBackoff< ChronoType >::rng_
private

Random number generator engine.

Definition at line 99 of file backoffs.hpp.

◆ uniform_dist_

template<typename ChronoType >
std::uniform_real_distribution<double> mal_toolkit::ExponentialBackoff< ChronoType >::uniform_dist_
private

Uniform distribution for generating jitter.

Definition at line 96 of file backoffs.hpp.


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