9 #ifndef FAIR_MQ_TOOLS_RATELIMIT_H 10 #define FAIR_MQ_TOOLS_RATELIMIT_H 41 using clock = std::chrono::steady_clock;
52 : tw_req(
std::chrono::seconds(1))
53 , start_time(clock::now())
56 tw_req = std::chrono::nanoseconds(1);
58 tw_req = std::chrono::duration_cast<clock::duration>(tw_req / rate);
60 skip_check_count = std::max(1,
int(std::chrono::milliseconds(5) / tw_req));
61 count = skip_check_count;
74 using namespace std::chrono;
76 auto now = clock::now();
77 if (tw == clock::duration::zero()) {
78 tw = (now - start_time) / skip_check_count;
80 tw = (1 * tw + 3 * (now - start_time) / skip_check_count) / 4;
83 if (tw > tw_req * 65 / 64) {
87 if (ts > clock::duration::zero()) {
88 ts = std::max(clock::duration::zero(),
89 ts - (tw - tw_req) * skip_check_count * 1 / 2);
93 std::min(
int(seconds(1) / tw_req),
94 (skip_check_count * 5 + 3) / 4);
97 }
else if (tw < tw_req * 63 / 64) {
106 const int min_skip_count = std::max(1,
int(milliseconds(5) / tw_req));
107 if (skip_check_count > min_skip_count) {
108 assert(ts == clock::duration::zero());
109 skip_check_count = std::max(min_skip_count, skip_check_count * 3 / 4);
112 ts += (tw_req - tw) * (skip_check_count * 7) / 8;
118 count = skip_check_count;
119 if (ts > clock::duration::zero()) {
120 std::this_thread::sleep_for(ts);
126 clock::duration tw{},
129 clock::time_point start_time;
131 int skip_check_count = 1;
138 #endif // FAIR_MQ_TOOLS_RATELIMIT_H
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23