19#ifndef ORES_UTILITY_CONCURRENCY_RETRY_STRATEGY_HPP
20#define ORES_UTILITY_CONCURRENCY_RETRY_STRATEGY_HPP
25namespace ores::utility::concurrency {
47 std::chrono::seconds initial_delay{5};
48 std::chrono::seconds max_delay{300};
49 std::chrono::seconds reset_threshold{60};
62 last_start_ = std::chrono::steady_clock::now();
75 const auto uptime = std::chrono::steady_clock::now() - last_start_;
76 if (uptime >= config_.reset_threshold)
79 const auto delay = compute_delay();
90 bool exceeded(
int max)
const {
return count_ >= max; }
110 std::chrono::seconds compute_delay()
const {
113 const int exp = std::min(count_, 6);
114 const auto delay = std::chrono::seconds(
115 config_.initial_delay.count() * (1 << exp));
116 return std::min(delay, config_.max_delay);
121 std::chrono::steady_clock::time_point last_start_{};
Stateful exponential-backoff retry calculator.
Definition retry_strategy.hpp:44
void on_start()
Record a successful start.
Definition retry_strategy.hpp:61
int failure_count() const
Returns the current consecutive failure count.
Definition retry_strategy.hpp:93
bool exceeded(int max) const
Returns whether the failure count has reached or exceeded max.
Definition retry_strategy.hpp:90
void set_failure_count(int n)
Restore a previously saved failure count without resetting state.
Definition retry_strategy.hpp:101
void reset()
Reset the counter and start-time record to initial state.
Definition retry_strategy.hpp:104
std::chrono::seconds on_failure()
Record a failure and compute the next backoff delay.
Definition retry_strategy.hpp:74