artdaq_mfextensions  v1_02_02
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 {
15  if (!in_use_) return false;
16 
17  if (!boost::regex_match(name, what_, expr_))
18  return false;
19 
20  if (limit_ == 0) return true; // suppress
21  else if (limit_ < 0) return false; // no limit
22 
23  if (timespan_ <= 0)
24  {
25  // only display first "limit_" messages
26  ++count_;
27  return count_ > limit_ ? true : false;
28  }
29 
30  long sec = tm.tv_sec;
31  if (sec - last_window_start_ > timespan_)
32  {
33  last_window_start_ = sec;
34  count_ = 1;
35  }
36  else
37  {
38  ++count_;
39  }
40 
41  return count_ > limit_ ? true : false;
42 }
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