9 #ifndef FAIR_MQ_TOOLS_RATELIMIT_H 10 #define FAIR_MQ_TOOLS_RATELIMIT_H 41 using clock = std::chrono::steady_clock;
51 RateLimiter(
float rate) : tw_req(std::chrono::seconds(1)), start_time(clock::now())
54 tw_req = std::chrono::nanoseconds(1);
56 tw_req = std::chrono::duration_cast<clock::duration>(tw_req / rate);
58 skip_check_count = std::max(1,
int(std::chrono::milliseconds(5) / tw_req));
59 count = skip_check_count;
72 using namespace std::chrono;
74 auto now = clock::now();
75 if (tw == clock::duration::zero()) {
76 tw = (now - start_time) / skip_check_count;
78 tw = (1 * tw + 3 * (now - start_time) / skip_check_count) / 4;
81 if (tw > tw_req * 65 / 64) {
85 if (ts > clock::duration::zero()) {
86 ts = std::max(clock::duration::zero(),
87 ts - (tw - tw_req) * skip_check_count * 1 / 2);
91 std::min(
int(seconds(1) / tw_req),
92 (skip_check_count * 5 + 3) / 4);
95 }
else if (tw < tw_req * 63 / 64) {
104 const int min_skip_count = std::max(1,
int(milliseconds(5) / tw_req));
105 if (skip_check_count > min_skip_count) {
106 assert(ts == clock::duration::zero());
107 skip_check_count = std::max(min_skip_count, skip_check_count * 3 / 4);
110 ts += (tw_req - tw) * (skip_check_count * 7) / 8;
116 count = skip_check_count;
117 if (ts > clock::duration::zero()) {
118 std::this_thread::sleep_for(ts);
124 clock::duration tw{},
127 clock::time_point start_time;
129 int skip_check_count = 1;
136 #endif // FAIR_MQ_TOOLS_RATELIMIT_H
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23