FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
backoff.hpp
Go to the documentation of this file.
1#pragma once
2// Exponential backoff with jitter for reconnects: delay_n = cap_n * (1 - jitter) +
3// U(0, cap_n * jitter) where cap_n = min(max_ms, base_ms * 2^n). jitter = 1.0 is the
4// "full jitter" scheme (recommended: spreads a thundering herd of reconnects after a venue
5// outage), jitter = 0 is deterministic (useful in tests).
6//
7// Without a seed every object draws its own: connections that all lost the same venue spread out
8// only when their jitter differs, and a shared seed made them retry in step.
9#include <algorithm>
10#include <atomic>
11#include <chrono>
12#include <cmath>
13#include <cstdint>
14#include <random>
15
16namespace fastmm::net {
17
18// A seed no other backoff of this process, nor of another started at another time, draws.
19inline std::uint64_t backoff_seed() noexcept {
20 static std::atomic<std::uint64_t> counter{0};
21 const auto now =
22 static_cast<std::uint64_t>(std::chrono::steady_clock::now().time_since_epoch().count());
23 // splitmix64 of the time and a per-process sequence number.
24 std::uint64_t z = now + 0x9E3779B97F4A7C15ULL * (counter.fetch_add(1) + 1);
25 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
26 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
27 return z ^ (z >> 31);
28}
29
30struct BackoffConfig {
31 std::uint32_t base_ms = 250;
32 std::uint32_t max_ms = 30'000;
33 double jitter = 1.0; // fraction of the capped delay that is randomised, in [0, 1]
34};
37 public:
38 explicit ExponentialBackoff(BackoffConfig cfg = {}) noexcept
40 ExponentialBackoff(BackoffConfig cfg, std::uint64_t seed) noexcept : cfg_(cfg), rng_(seed) {
41 cfg_.jitter = std::clamp(cfg_.jitter, 0.0, 1.0);
42 if (cfg_.max_ms < cfg_.base_ms) cfg_.max_ms = cfg_.base_ms;
43 }
44
45 // Delay for the current attempt in milliseconds; advances the attempt counter.
46 std::uint32_t next_ms() noexcept {
47 const std::uint32_t cap = capped_delay(attempt_);
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));
54 }
55
56 void reset() noexcept { attempt_ = 0; }
57 std::uint32_t attempt() const noexcept { return attempt_; }
58 const BackoffConfig& config() const noexcept { return cfg_; }
60 // Upper bound of the delay for attempt n (before jitter) - exposed for tests/metrics.
61 std::uint32_t capped_delay(std::uint32_t attempt) const noexcept {
62 const std::uint64_t raw = static_cast<std::uint64_t>(cfg_.base_ms)
63 << std::min<std::uint32_t>(attempt, 31);
64 return static_cast<std::uint32_t>(std::min<std::uint64_t>(raw, cfg_.max_ms));
65 }
66
67 private:
68 BackoffConfig cfg_;
69 std::mt19937_64 rng_;
70 std::uint32_t attempt_ = 0;
71};
72
73} // namespace fastmm::net
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