Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::retry_policy Class Reference

Encapsulates retry behavior configuration for jobs. More...

#include <retry_policy.h>

Collaboration diagram for kcenon::thread::retry_policy:
Collaboration graph

Public Member Functions

 retry_policy ()
 Default constructor creates a "no retry" policy.
 
auto get_strategy () const -> retry_strategy
 Gets the retry strategy type.
 
auto get_max_attempts () const -> std::size_t
 Gets the maximum number of attempts.
 
auto get_initial_delay () const -> std::chrono::milliseconds
 Gets the initial delay between retries.
 
auto get_multiplier () const -> double
 Gets the multiplier used for exponential backoff.
 
auto get_max_delay () const -> std::chrono::milliseconds
 Gets the maximum delay cap.
 
auto uses_jitter () const -> bool
 Checks if jitter is enabled.
 
auto is_retry_enabled () const -> bool
 Checks if retry is enabled.
 
auto get_current_attempt () const -> std::size_t
 Gets the current attempt number (0-based).
 
auto has_attempts_remaining () const -> bool
 Checks if more retry attempts are available.
 
auto record_attempt () -> void
 Increments the attempt counter.
 
auto reset () -> void
 Resets the attempt counter to zero.
 
auto get_delay_for_current_attempt () const -> std::chrono::milliseconds
 Calculates the delay for the current retry attempt.
 
auto to_string () const -> std::string
 Provides a string representation of the policy.
 

Static Public Member Functions

static auto no_retry () -> retry_policy
 Creates a policy that disables retry.
 
static auto fixed (std::size_t max_attempts, std::chrono::milliseconds delay) -> retry_policy
 Creates a fixed delay retry policy.
 
static auto linear (std::size_t max_attempts, std::chrono::milliseconds initial_delay, std::chrono::milliseconds max_delay=std::chrono::milliseconds::max()) -> retry_policy
 Creates a linear backoff retry policy.
 
static auto exponential_backoff (std::size_t max_attempts, std::chrono::milliseconds initial_delay=std::chrono::milliseconds(100), double multiplier=2.0, std::chrono::milliseconds max_delay=std::chrono::milliseconds(30000), bool use_jitter=false) -> retry_policy
 Creates an exponential backoff retry policy.
 

Private Attributes

retry_strategy strategy_
 
std::size_t max_attempts_
 
std::chrono::milliseconds initial_delay_
 
double multiplier_
 
std::chrono::milliseconds max_delay_
 
bool use_jitter_
 
std::size_t current_attempt_
 

Detailed Description

Encapsulates retry behavior configuration for jobs.

The retry_policy class provides a flexible way to configure how failed jobs should be retried. It supports multiple retry strategies:

  • none: No retry, fail immediately
  • fixed: Constant delay between each retry
  • linear: Delay increases linearly (delay * attempt_number)
  • exponential_backoff: Delay doubles with each attempt (with optional jitter)

Thread Safety

  • All getter methods are const and thread-safe
  • The policy is typically configured once before job submission

Usage Example

// No retry
auto policy = retry_policy::none();
// Fixed delay of 100ms, max 3 attempts
auto policy = retry_policy::fixed(3, std::chrono::milliseconds(100));
// Exponential backoff: 100ms, 200ms, 400ms, 800ms (max 4 attempts)
// Custom exponential with jitter
auto policy = retry_policy::exponential_backoff(5, 50ms, 2.0, 10000ms, true);
static auto fixed(std::size_t max_attempts, std::chrono::milliseconds delay) -> retry_policy
Creates a fixed delay retry policy.
static auto exponential_backoff(std::size_t max_attempts, std::chrono::milliseconds initial_delay=std::chrono::milliseconds(100), double multiplier=2.0, std::chrono::milliseconds max_delay=std::chrono::milliseconds(30000), bool use_jitter=false) -> retry_policy
Creates an exponential backoff retry policy.

Definition at line 66 of file retry_policy.h.

Constructor & Destructor Documentation

◆ retry_policy()

kcenon::thread::retry_policy::retry_policy ( )
inline

Default constructor creates a "no retry" policy.

Definition at line 72 of file retry_policy.h.

74 , max_attempts_(1)
75 , initial_delay_(std::chrono::milliseconds(0))
76 , multiplier_(1.0)
77 , max_delay_(std::chrono::milliseconds(0))
78 , use_jitter_(false)
80 {
81 }
std::chrono::milliseconds max_delay_
std::chrono::milliseconds initial_delay_

Referenced by no_retry().

Here is the caller graph for this function:

Member Function Documentation

◆ exponential_backoff()

static auto kcenon::thread::retry_policy::exponential_backoff ( std::size_t max_attempts,
std::chrono::milliseconds initial_delay = std::chrono::milliseconds(100),
double multiplier = 2.0,
std::chrono::milliseconds max_delay = std::chrono::milliseconds(30000),
bool use_jitter = false ) -> retry_policy
inlinestaticnodiscard

Creates an exponential backoff retry policy.

Delay doubles with each attempt: initial_delay * (multiplier ^ attempt)

Parameters
max_attemptsMaximum number of attempts (including initial)
initial_delayBase delay for first retry (default: 100ms)
multiplierExponential multiplier (default: 2.0)
max_delayMaximum delay cap (default: 30 seconds)
use_jitterAdd randomness to prevent thundering herd
Returns
A retry_policy with exponential backoff
Note
With jitter enabled, the actual delay will be uniformly distributed between 0 and the calculated delay value.

Definition at line 153 of file retry_policy.h.

159 {
160 retry_policy policy;
161 policy.strategy_ = retry_strategy::exponential_backoff;
162 policy.max_attempts_ = max_attempts;
163 policy.initial_delay_ = initial_delay;
164 policy.multiplier_ = multiplier;
165 policy.max_delay_ = max_delay;
166 policy.use_jitter_ = use_jitter;
167 return policy;
168 }
retry_policy()
Default constructor creates a "no retry" policy.
@ exponential_backoff
Exponentially increasing delay with optional jitter.

References kcenon::thread::exponential_backoff, initial_delay_, max_attempts_, max_delay_, multiplier_, strategy_, and use_jitter_.

◆ fixed()

static auto kcenon::thread::retry_policy::fixed ( std::size_t max_attempts,
std::chrono::milliseconds delay ) -> retry_policy
inlinestaticnodiscard

Creates a fixed delay retry policy.

Parameters
max_attemptsMaximum number of attempts (including initial)
delayFixed delay between attempts
Returns
A retry_policy with fixed delay
Note
If max_attempts is 1, no retry will occur (same as none())

Definition at line 101 of file retry_policy.h.

104 {
105 retry_policy policy;
106 policy.strategy_ = retry_strategy::fixed;
107 policy.max_attempts_ = max_attempts;
108 policy.initial_delay_ = delay;
109 policy.multiplier_ = 1.0;
110 policy.max_delay_ = delay;
111 return policy;
112 }
@ delay
Delay processing (attempt later)
@ fixed
Fixed delay between retries.

References kcenon::thread::delay, kcenon::thread::fixed, initial_delay_, max_attempts_, max_delay_, multiplier_, and strategy_.

◆ get_current_attempt()

auto kcenon::thread::retry_policy::get_current_attempt ( ) const -> std::size_t
inlinenodiscard

Gets the current attempt number (0-based).

Returns
Current attempt number

Definition at line 237 of file retry_policy.h.

238 {
239 return current_attempt_;
240 }

References current_attempt_.

◆ get_delay_for_current_attempt()

auto kcenon::thread::retry_policy::get_delay_for_current_attempt ( ) const -> std::chrono::milliseconds
inlinenodiscard

Calculates the delay for the current retry attempt.

Returns
Delay duration for the current attempt
Note
This does not increment the attempt counter.

Definition at line 277 of file retry_policy.h.

278 {
280 {
281 return std::chrono::milliseconds(0);
282 }
283
284 std::chrono::milliseconds delay;
285
286 switch (strategy_)
287 {
290 break;
291
293 delay = std::chrono::milliseconds(
294 initial_delay_.count() * static_cast<long long>(current_attempt_)
295 );
296 break;
297
299 {
300 double factor = std::pow(multiplier_, static_cast<double>(current_attempt_ - 1));
301 auto calculated = static_cast<long long>(
302 static_cast<double>(initial_delay_.count()) * factor
303 );
304 delay = std::chrono::milliseconds(calculated);
305 }
306 break;
307
308 default:
309 delay = std::chrono::milliseconds(0);
310 break;
311 }
312
313 // Apply max_delay cap
314 if (delay > max_delay_)
315 {
317 }
318
319 return delay;
320 }
@ linear
Linearly increasing delay.

References current_attempt_, kcenon::thread::delay, kcenon::thread::exponential_backoff, kcenon::thread::fixed, initial_delay_, kcenon::thread::linear, max_delay_, multiplier_, kcenon::thread::none, and strategy_.

◆ get_initial_delay()

auto kcenon::thread::retry_policy::get_initial_delay ( ) const -> std::chrono::milliseconds
inlinenodiscard

Gets the initial delay between retries.

Returns
Initial delay duration

Definition at line 192 of file retry_policy.h.

193 {
194 return initial_delay_;
195 }

References initial_delay_.

◆ get_max_attempts()

auto kcenon::thread::retry_policy::get_max_attempts ( ) const -> std::size_t
inlinenodiscard

Gets the maximum number of attempts.

Returns
Maximum attempts (1 means no retry)

Definition at line 183 of file retry_policy.h.

184 {
185 return max_attempts_;
186 }

References max_attempts_.

◆ get_max_delay()

auto kcenon::thread::retry_policy::get_max_delay ( ) const -> std::chrono::milliseconds
inlinenodiscard

Gets the maximum delay cap.

Returns
Maximum delay duration

Definition at line 210 of file retry_policy.h.

211 {
212 return max_delay_;
213 }

References max_delay_.

◆ get_multiplier()

auto kcenon::thread::retry_policy::get_multiplier ( ) const -> double
inlinenodiscard

Gets the multiplier used for exponential backoff.

Returns
Exponential multiplier

Definition at line 201 of file retry_policy.h.

202 {
203 return multiplier_;
204 }

References multiplier_.

◆ get_strategy()

auto kcenon::thread::retry_policy::get_strategy ( ) const -> retry_strategy
inlinenodiscard

Gets the retry strategy type.

Returns
The current retry strategy

Definition at line 174 of file retry_policy.h.

175 {
176 return strategy_;
177 }

References strategy_.

◆ has_attempts_remaining()

auto kcenon::thread::retry_policy::has_attempts_remaining ( ) const -> bool
inlinenodiscard

Checks if more retry attempts are available.

Returns
true if current_attempt < max_attempts - 1

Definition at line 246 of file retry_policy.h.

247 {
248 return current_attempt_ < max_attempts_ - 1;
249 }

References current_attempt_, and max_attempts_.

◆ is_retry_enabled()

auto kcenon::thread::retry_policy::is_retry_enabled ( ) const -> bool
inlinenodiscard

Checks if retry is enabled.

Returns
true if max_attempts > 1 and strategy is not none

Definition at line 228 of file retry_policy.h.

229 {
231 }

References max_attempts_, kcenon::thread::none, and strategy_.

◆ linear()

static auto kcenon::thread::retry_policy::linear ( std::size_t max_attempts,
std::chrono::milliseconds initial_delay,
std::chrono::milliseconds max_delay = std::chrono::milliseconds::max() ) -> retry_policy
inlinestaticnodiscard

Creates a linear backoff retry policy.

Delay increases linearly: delay * attempt_number

Parameters
max_attemptsMaximum number of attempts (including initial)
initial_delayBase delay for first retry
max_delayMaximum delay cap (default: no cap)
Returns
A retry_policy with linear backoff

Definition at line 124 of file retry_policy.h.

128 {
129 retry_policy policy;
130 policy.strategy_ = retry_strategy::linear;
131 policy.max_attempts_ = max_attempts;
132 policy.initial_delay_ = initial_delay;
133 policy.multiplier_ = 1.0;
134 policy.max_delay_ = max_delay;
135 return policy;
136 }

References initial_delay_, kcenon::thread::linear, max_attempts_, max_delay_, multiplier_, and strategy_.

◆ no_retry()

static auto kcenon::thread::retry_policy::no_retry ( ) -> retry_policy
inlinestaticnodiscard

Creates a policy that disables retry.

Returns
A retry_policy with no retry behavior

Definition at line 87 of file retry_policy.h.

88 {
89 return retry_policy();
90 }

References retry_policy().

Here is the call graph for this function:

◆ record_attempt()

auto kcenon::thread::retry_policy::record_attempt ( ) -> void
inline

Increments the attempt counter.

Call this after each failed attempt. If the counter reaches max_attempts, has_attempts_remaining() will return false.

Definition at line 257 of file retry_policy.h.

258 {
260 }

References current_attempt_.

◆ reset()

auto kcenon::thread::retry_policy::reset ( ) -> void
inline

Resets the attempt counter to zero.

Definition at line 265 of file retry_policy.h.

266 {
268 }

References current_attempt_.

◆ to_string()

auto kcenon::thread::retry_policy::to_string ( ) const -> std::string
inlinenodiscard

Provides a string representation of the policy.

Returns
Human-readable description of the policy

Definition at line 326 of file retry_policy.h.

327 {
328 switch (strategy_)
329 {
331 return "retry_policy(none)";
333 return "retry_policy(fixed, attempts=" + std::to_string(max_attempts_) +
334 ", delay=" + std::to_string(initial_delay_.count()) + "ms)";
336 return "retry_policy(linear, attempts=" + std::to_string(max_attempts_) +
337 ", initial=" + std::to_string(initial_delay_.count()) + "ms)";
339 return "retry_policy(exponential, attempts=" + std::to_string(max_attempts_) +
340 ", initial=" + std::to_string(initial_delay_.count()) +
341 "ms, multiplier=" + std::to_string(multiplier_) + ")";
342 default:
343 return "retry_policy(unknown)";
344 }
345 }

References kcenon::thread::exponential_backoff, kcenon::thread::fixed, initial_delay_, kcenon::thread::linear, max_attempts_, multiplier_, kcenon::thread::none, and strategy_.

◆ uses_jitter()

auto kcenon::thread::retry_policy::uses_jitter ( ) const -> bool
inlinenodiscard

Checks if jitter is enabled.

Returns
true if jitter is enabled

Definition at line 219 of file retry_policy.h.

220 {
221 return use_jitter_;
222 }

References use_jitter_.

Member Data Documentation

◆ current_attempt_

std::size_t kcenon::thread::retry_policy::current_attempt_
private

◆ initial_delay_

std::chrono::milliseconds kcenon::thread::retry_policy::initial_delay_
private

◆ max_attempts_

std::size_t kcenon::thread::retry_policy::max_attempts_
private

◆ max_delay_

std::chrono::milliseconds kcenon::thread::retry_policy::max_delay_
private

◆ multiplier_

double kcenon::thread::retry_policy::multiplier_
private

◆ strategy_

retry_strategy kcenon::thread::retry_policy::strategy_
private

◆ use_jitter_

bool kcenon::thread::retry_policy::use_jitter_
private

Definition at line 353 of file retry_policy.h.

Referenced by exponential_backoff(), and uses_jitter().


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