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