Line data Source code
1 : #ifndef artdaq_mfextensions_extensions_throttle_hh
2 : #define artdaq_mfextensions_extensions_throttle_hh
3 :
4 : #include <string>
5 :
6 : #include <sys/time.h>
7 :
8 : #include <boost/regex.hpp>
9 :
10 : typedef boost::regex regex_t;
11 : typedef boost::smatch smatch_t;
12 :
13 : /// <summary>
14 : /// Throttle messages based on name and time limits.
15 : /// Separate from MessageFacility limiting.
16 : /// </summary>
17 : class throttle
18 : {
19 : public:
20 : /// <summary>
21 : /// Throttle messages using a regular expression if they occurr above a certain frequency
22 : /// </summary>
23 : /// <param name="name">Regular expression to match messages</param>
24 : /// <param name="limit">Number of messages before throttling is enabled</param>
25 : /// <param name="timespan">Time limit for throttling</param>
26 : throttle(std::string const& name, int limit, int64_t timespan);
27 :
28 : /// <summary>
29 : /// Determine whether the name has reached the throttling limit
30 : /// </summary>
31 : /// <param name="name">Name to check against regular expression</param>
32 : /// <param name="tm">Time of message</param>
33 : /// <returns>Whether the message should be throttled</returns>
34 : bool reach_limit(std::string const& name, timeval tm);
35 :
36 : /// <summary>
37 : /// Enable or disable this throttle
38 : /// </summary>
39 : /// <param name="flag">Whether the throttle should be enabled</param>
40 2 : void use(bool flag) { in_use_ = flag; }
41 :
42 : private:
43 : std::string name_;
44 : regex_t expr_;
45 : smatch_t what_;
46 : int limit_;
47 : int64_t timespan_;
48 : int64_t last_window_start_;
49 : int count_;
50 : bool in_use_;
51 : };
52 :
53 : #endif // artdaq_mfextensions_extensions_throttle_hh
|