mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
backoffs.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <chrono>
4#include <random>
5
6namespace mal_toolkit
7{
15 template <typename ChronoType>
17 {
18 public:
29 ExponentialBackoff(ChronoType initial_delay, ChronoType max_delay, double multiplier = 2.0,
30 double divisor = 2.0, double jitter_factor = 0.2)
31 : initial_delay_(initial_delay),
32 max_delay_(max_delay),
33 multiplier_(multiplier),
34 divisor_(divisor),
35 jitter_factor_(jitter_factor),
36 current_delay_(initial_delay),
37 uniform_dist_(0.0, 1.0),
38 rng_(std::random_device{}())
39 {
40 }
41
47 constexpr ChronoType get_current_delay() noexcept
48 {
49 ChronoType jitter =
50 std::chrono::duration_cast<ChronoType>(current_delay_ * jitter_factor_ * uniform_dist_(rng_));
51 return current_delay_ + jitter;
52 }
53
59 constexpr double get_current_delay_double() noexcept
60 {
62 using seconds_double = std::chrono::duration<double, std::chrono::seconds::period>;
63 return std::chrono::duration_cast<seconds_double>(current_delay_).count() + jitter;
64 }
65
69 constexpr void increase_delay() noexcept
70 {
71 current_delay_ = std::min(std::chrono::duration_cast<ChronoType>(current_delay_ * multiplier_), max_delay_);
72 }
73
77 constexpr void decrease_delay() noexcept
78 {
80 std::max(std::chrono::duration_cast<ChronoType>(current_delay_ / divisor_), initial_delay_);
81 }
82
86 constexpr void reset_delay() noexcept { current_delay_ = initial_delay_; }
87
88 private:
89 const ChronoType initial_delay_;
90 const ChronoType max_delay_;
91 const double multiplier_;
92 const double divisor_;
93
94 const double jitter_factor_;
96 std::uniform_real_distribution<double> uniform_dist_;
97
98 ChronoType current_delay_;
99 std::default_random_engine rng_;
100 };
101
109 template <typename ChronoType>
111 {
112 public:
120 constexpr LinearBackoff(ChronoType initial_delay, ChronoType max_delay, ChronoType step)
121 : initial_delay_(initial_delay), max_delay_(max_delay), step_(step), current_delay_(initial_delay)
122 {
123 }
124
130 constexpr ChronoType get_current_delay() const noexcept { return current_delay_; }
131
137 constexpr double get_current_delay_double() const noexcept {
138 using seconds_double = std::chrono::duration<double, std::chrono::seconds::period>;
139 return std::chrono::duration_cast<seconds_double>(current_delay_).count();
140 }
141
145 constexpr void increase_delay() noexcept { current_delay_ = std::min(current_delay_ + step_, max_delay_); }
146
150 constexpr void decrease_delay() noexcept { current_delay_ = std::max(current_delay_ - step_, initial_delay_); }
151
155 constexpr void reset_delay() noexcept { current_delay_ = initial_delay_; }
156
157 private:
158 const ChronoType initial_delay_;
159 const ChronoType max_delay_;
160 const ChronoType step_;
161 ChronoType current_delay_;
162 };
163
168
173
174} // namespace mal_toolkit
A utility class for implementing exponential backoff strategies.
Definition backoffs.hpp:17
std::default_random_engine rng_
Random number generator engine.
Definition backoffs.hpp:99
constexpr ChronoType get_current_delay() noexcept
Get the current backoff delay.
Definition backoffs.hpp:47
constexpr double get_current_delay_double() noexcept
Get the current backoff delay as a double.
Definition backoffs.hpp:59
const double multiplier_
The factor by which the delay is multiplied after each retry.
Definition backoffs.hpp:91
const double divisor_
The factor by which the delay is divided when decreasing the delay.
Definition backoffs.hpp:92
const ChronoType max_delay_
The maximum delay allowed between retries.
Definition backoffs.hpp:90
constexpr void reset_delay() noexcept
Reset the backoff delay to its initial value.
Definition backoffs.hpp:86
constexpr void decrease_delay() noexcept
Decrease the backoff delay using divisor factor.
Definition backoffs.hpp:77
std::uniform_real_distribution< double > uniform_dist_
Uniform distribution for generating jitter.
Definition backoffs.hpp:96
constexpr void increase_delay() noexcept
Increase the backoff delay using exponential factor.
Definition backoffs.hpp:69
ChronoType current_delay_
The current backoff delay.
Definition backoffs.hpp:98
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.
Definition backoffs.hpp:29
const ChronoType initial_delay_
The initial delay before the first retry.
Definition backoffs.hpp:89
A utility class for implementing linear backoff strategies.
Definition backoffs.hpp:111
constexpr void reset_delay() noexcept
Reset the backoff delay to its initial value.
Definition backoffs.hpp:155
const ChronoType step_
The increment step used to increase or decrease the delay.
Definition backoffs.hpp:160
constexpr double get_current_delay_double() const noexcept
Get the current backoff delay as a double.
Definition backoffs.hpp:137
constexpr ChronoType get_current_delay() const noexcept
Get the current backoff delay.
Definition backoffs.hpp:130
const ChronoType initial_delay_
The initial delay before the first retry.
Definition backoffs.hpp:158
ChronoType current_delay_
The current backoff delay.
Definition backoffs.hpp:161
constexpr void increase_delay() noexcept
Increase the backoff delay using the specified step.
Definition backoffs.hpp:145
constexpr void decrease_delay() noexcept
Decrease the backoff delay using the specified step.
Definition backoffs.hpp:150
const ChronoType max_delay_
The maximum delay allowed between retries.
Definition backoffs.hpp:159
constexpr LinearBackoff(ChronoType initial_delay, ChronoType max_delay, ChronoType step)
Constructor to initialize the linear backoff strategy.
Definition backoffs.hpp:120
Contains a collection of tools and utilities provided by the MAL Toolkit library.
Definition backoffs.hpp:7
STL namespace.