FairMQ  1.4.33
C++ Message Queuing Library and Framework
RateLimit.h
1 /********************************************************************************
2  * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_TOOLS_RATELIMIT_H
10 #define FAIR_MQ_TOOLS_RATELIMIT_H
11 
12 #include <cassert>
13 #include <string>
14 // #include <iostream>
15 #include <iomanip>
16 #include <thread>
17 #include <chrono>
18 
19 namespace fair::mq::tools
20 {
21 
35 class RateLimiter
36 {
37  using clock = std::chrono::steady_clock;
38 
39  public:
47  explicit RateLimiter(float rate)
48  : tw_req(std::chrono::seconds(1))
49  , start_time(clock::now())
50  {
51  if (rate <= 0) {
52  tw_req = std::chrono::nanoseconds(1);
53  } else {
54  tw_req = std::chrono::duration_cast<clock::duration>(tw_req / rate);
55  }
56  skip_check_count = std::max(1, int(std::chrono::milliseconds(5) / tw_req));
57  count = skip_check_count;
58  // std::cerr << "skip_check_count: " << skip_check_count << '\n';
59  }
60 
68  void maybe_sleep()
69  {
70  using namespace std::chrono;
71  if (--count == 0) {
72  auto now = clock::now();
73  if (tw == clock::duration::zero()) {
74  tw = (now - start_time) / skip_check_count;
75  } else {
76  tw = (1 * tw + 3 * (now - start_time) / skip_check_count) / 4;
77  }
78  // std::ostringstream s; s << "tw = " << std::setw(10) <<
79  // duration_cast<nanoseconds>(tw).count() << "ns, req = " <<
80  // duration_cast<nanoseconds>(tw_req).count() << "ns, ";
81  if (tw > tw_req * 65 / 64) {
82  // the time between maybe_sleep calls is more than 1% too long
83  // fix it by reducing ts towards 0 and if ts = 0 doesn't suffice, increase
84  // skip_check_count
85  if (ts > clock::duration::zero()) {
86  ts = std::max(clock::duration::zero(), ts - (tw - tw_req) * skip_check_count * 1 / 2);
87  // std::cerr << s.str() << "maybe_sleep: going too slow; sleep less: " <<
88  // duration_cast<microseconds>(ts).count() << "µs\n";
89  } else {
90  skip_check_count =
91  std::min(int(seconds(1) / tw_req), // recheck at least every second
92  (skip_check_count * 5 + 3) / 4);
93  // std::cerr << s.str() << "maybe_sleep: going too slow; work more: " <<
94  // skip_check_count << "\n";
95  }
96  } else if (tw < tw_req * 63 / 64) {
97  // the time between maybe_sleep calls is more than 1% too short
98  // fix it by reducing skip_check_count towards 1 and if skip_check_count = 1
99  // doesn't suffice, increase ts
100 
101  // The minimum work count is defined such that a typical sleep time is greater
102  // than 1ms.
103  // The user requested 1/tw_req work iterations per second. Divided by 1000, that's
104  // the count per ms.
105  const int min_skip_count = std::max(1, int(milliseconds(5) / tw_req));
106  if (skip_check_count > min_skip_count) {
107  assert(ts == clock::duration::zero());
108  skip_check_count = std::max(min_skip_count, skip_check_count * 3 / 4);
109  // std::cerr << s.str() << "maybe_sleep: going too fast; work less: " <<
110  // skip_check_count << "\n";
111  } else {
112  ts += (tw_req - tw) * (skip_check_count * 7) / 8;
113  // std::cerr << s.str() << "maybe_sleep: going too fast; sleep more: " <<
114  // duration_cast<microseconds>(ts).count() << "µs\n";
115  }
116  }
117 
118  start_time = now;
119  count = skip_check_count;
120  if (ts > clock::duration::zero()) {
121  std::this_thread::sleep_for(ts);
122  }
123  }
124  }
125 
126  private:
127  clock::duration tw{},
128  ts{},
129  tw_req;
130  clock::time_point start_time;
131  int count = 1;
132  int skip_check_count = 1;
133 };
134 
135 } // namespace fair::mq::tools
136 
137 #endif // FAIR_MQ_TOOLS_RATELIMIT_H
fair::mq::tools::RateLimiter::RateLimiter
RateLimiter(float rate)
Definition: RateLimit.h:59
fair::mq::tools::RateLimiter::maybe_sleep
void maybe_sleep()
Definition: RateLimit.h:80

privacy