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 bool throttle::reach_limit(std::string const& name, timeval tm)
00014 {
00015 if (!in_use_) return false;
00016
00017 if (!boost::regex_match(name, what_, expr_))
00018 return false;
00019
00020 if (limit_ == 0) return true;
00021 else if (limit_ < 0) return false;
00022
00023 if (timespan_ <= 0)
00024 {
00025
00026 ++count_;
00027 return count_ > limit_ ? true : false;
00028 }
00029
00030 long sec = tm.tv_sec;
00031 if (sec - last_window_start_ > timespan_)
00032 {
00033 last_window_start_ = sec;
00034 count_ = 1;
00035 }
00036 else
00037 {
00038 ++count_;
00039 }
00040
00041 return count_ > limit_ ? true : false;
00042 }