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