20 static std::atomic<std::uint64_t> counter{0};
22 static_cast<std::uint64_t
>(std::chrono::steady_clock::now().time_since_epoch().count());
24 std::uint64_t z = now + 0x9E3779B97F4A7C15ULL * (counter.fetch_add(1) + 1);
25 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
26 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
46 std::uint32_t
next_ms() noexcept {
48 if (attempt_ < 31) ++attempt_;
49 const double fixed =
static_cast<double>(cap) * (1.0 - cfg_.
jitter);
50 const double random_span =
static_cast<double>(cap) * cfg_.
jitter;
51 std::uniform_real_distribution<double> dist(0.0, 1.0);
52 const double delay = fixed + random_span * dist(rng_);
53 return static_cast<std::uint32_t
>(std::llround(delay));
56 void reset() noexcept { attempt_ = 0; }
57 std::uint32_t
attempt() const noexcept {
return attempt_; }
62 const std::uint64_t raw =
static_cast<std::uint64_t
>(cfg_.
base_ms)
64 return static_cast<std::uint32_t
>(std::min<std::uint64_t>(raw, cfg_.
max_ms));
70 std::uint32_t attempt_ = 0;
Definition backoff.hpp:38
ExponentialBackoff(BackoffConfig cfg={}) noexcept
Definition backoff.hpp:40
std::uint32_t attempt() const noexcept
Definition backoff.hpp:59
std::uint32_t next_ms() noexcept
Delay for the current attempt in milliseconds; advances the attempt counter.
Definition backoff.hpp:48
std::uint32_t capped_delay(std::uint32_t attempt) const noexcept
Upper bound of the delay for attempt n (before jitter) - exposed for tests/metrics.
Definition backoff.hpp:63
const BackoffConfig & config() const noexcept
Definition backoff.hpp:60
void reset() noexcept
Definition backoff.hpp:58
Definition reactor.hpp:37
std::uint64_t backoff_seed() noexcept
A seed no other backoff of this process, nor of another started at another time, draws.
Definition backoff.hpp:21
Definition backoff.hpp:32
double jitter
fraction of the capped delay that is randomised, in [0, 1]
Definition backoff.hpp:35
std::uint32_t base_ms
Definition backoff.hpp:33
std::uint32_t max_ms
Definition backoff.hpp:34