00001 #include "mfextensions/Extensions/throttle.hh"
00002
00003 throttle::throttle(std::string const& name, int limit, long timespan)
00004 : name_(name)
00005 , expr_(regex_t(name))
00006 , what_()
00007 , limit_(limit)
00008 , timespan_(timespan)
00009 , last_window_start_(0)
00010 , count_(0)
00011 , in_use_(true)
00012 {}
00013
00014 bool throttle::reach_limit(std::string const& name, timeval tm)
00015 {
00016 if (!in_use_) return false;
00017
00018 if (!boost::regex_match(name, what_, expr_))
00019 return false;
00020
00021 if (limit_ == 0) return true;
00022 else if (limit_ < 0) return false;
00023
00024 if (timespan_ <= 0)
00025 {
00026
00027 ++count_;
00028 return count_ > limit_ ? true : false;
00029 }
00030
00031 long sec = tm.tv_sec;
00032 if (sec - last_window_start_ > timespan_)
00033 {
00034 last_window_start_ = sec;
00035 count_ = 1;
00036 }
00037 else
00038 {
00039 ++count_;
00040 }
00041
00042 return count_ > limit_ ? true : false;
00043 }