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