Chatterino
ExponentialBackoff.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <chrono>
5 
6 namespace chatterino {
7 
8 // Yes, you can't specify the base 😎 deal with it
9 template <unsigned maxSteps>
11 {
12 public:
21  ExponentialBackoff(const std::chrono::milliseconds &start)
22  : start_(start)
23  , step_{1}
24  {
25  static_assert(maxSteps > 1, "maxSteps must be higher than 1");
26  }
27 
33  [[nodiscard]] std::chrono::milliseconds next()
34  {
35  auto next = this->start_ * (1 << (this->step_ - 1));
36 
37  this->step_ += 1;
38 
39  if (this->step_ >= maxSteps)
40  {
41  this->step_ = maxSteps;
42  }
43 
44  return next;
45  }
46 
50  void reset()
51  {
52  this->step_ = 1;
53  }
54 
55 private:
56  const std::chrono::milliseconds start_;
57  unsigned step_;
58 };
59 
60 } // namespace chatterino
std::chrono::milliseconds next()
Definition: ExponentialBackoff.hpp:33
void reset()
Definition: ExponentialBackoff.hpp:50
Definition: ExponentialBackoff.hpp:10
Definition: Application.cpp:48
ExponentialBackoff(const std::chrono::milliseconds &start)
Definition: ExponentialBackoff.hpp:21