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