Line data Source code
1 : #include "mfextensions/Extensions/throttle.hh"
2 :
3 1 : throttle::throttle(std::string const& name, int limit, int64_t timespan)
4 3 : : name_(name), expr_(regex_t(name)), limit_(limit), timespan_(timespan), last_window_start_(0), count_(0), in_use_(true) {}
5 :
6 6 : bool throttle::reach_limit(std::string const& name, timeval tm)
7 : {
8 6 : if (!in_use_)
9 : {
10 1 : return false;
11 : }
12 :
13 5 : if (!boost::regex_match(name, what_, expr_))
14 : {
15 1 : return false;
16 : }
17 :
18 4 : if (limit_ == 0)
19 : {
20 0 : return true; // suppress
21 : }
22 4 : if (limit_ < 0)
23 : {
24 0 : return false; // no limit
25 : }
26 :
27 4 : if (timespan_ <= 0)
28 : {
29 : // only display first "limit_" messages
30 0 : ++count_;
31 0 : return count_ > limit_;
32 : }
33 :
34 4 : int64_t sec = tm.tv_sec;
35 4 : if (sec - last_window_start_ > timespan_)
36 : {
37 2 : last_window_start_ = sec;
38 2 : count_ = 1;
39 : }
40 : else
41 : {
42 2 : ++count_;
43 : }
44 :
45 4 : return count_ > limit_;
46 : }
|