25 #ifndef DOZERG_FREQ_CONTROL_H_20120224 26 #define DOZERG_FREQ_CONTROL_H_20120224 28 #include "tools/time.hh" 71 void init(
size_t freq,
size_t bucketSz){
74 time_ = tools::MonoTimeUs();
88 nowUs = tools::MonoTimeUs();
90 delta_ += freq_ * (nowUs - time_);
95 ssize_t tok = delta_ / 1000000 + token_;
97 if(tok < token_ || tok > ssize_t(buckSz_))
109 bool check(
size_t need)
const{
return (token_ >= 0 &&
size_t(token_) >= need);}
118 ssize_t
token()
const{
return token_;}
126 bool get(
size_t need = 1){
127 if(token_ < 0 ||
size_t(token_) < need)
140 if(!valid() || ssize_t(need) < 0)
146 bool valid()
const {
return freq_ > 0;}
181 time_ = tools::MonoTimeUs();
183 freq_ = std::max(0., freq);
195 uint64_t now = tools::MonoTimeUs();
197 token_ += freq_ * (now - time_) / 1000000;
198 if(token_ > freq_ + 1)
void init(double freq)
Initialize this object.
Definition: freq_control.hh:180
Rate limiting for high frequency (>1Hz) jobs.
Definition: freq_control.hh:42
bool overdraw(size_t need)
Overdraw tokens.
Definition: freq_control.hh:139
bool check(size_t need) const
Check if there are enough tokens.
Definition: freq_control.hh:109
CFreqControl(size_t freq, size_t bucketSz)
Initialize this object.
Definition: freq_control.hh:62
void init(size_t freq, size_t bucketSz)
Initialize this object.
Definition: freq_control.hh:71
CFreqControl()
Default constructor.
Definition: freq_control.hh:48
CWideFreqControl(double freq=0)
Initialize this object.
Definition: freq_control.hh:177
Rate limiting for any frequency, including ones less than 1Hz.
Definition: freq_control.hh:169
ssize_t token() const
Get number of tokens in bucket.
Definition: freq_control.hh:118
void generate(uint64_t nowUs=0)
Generate tokens.
Definition: freq_control.hh:84