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