Rate limiting for high frequency (>1Hz) jobs.
More...
#include <freq_control.hh>
Rate limiting for high frequency (>1Hz) jobs.
CFreqControl is a convenient tool for rate limiting, based on Token Bucket algorithm.
Given a frequency F
, CFreqControl generates F
tokens to bucket per second. Each token represents a job. When there is no token in the bucket, no new job should be created.
The size of bucket is important for burstiness suppression. It denotes the maximum number of jobs created at once. In case of network transmission control for example, a proper bucket size leads to smooth traffic flow in despite of request bursts.
CFreqControl::CFreqControl |
( |
| ) |
|
|
inline |
Default constructor.
You need to call init before you can use this object.
CFreqControl::CFreqControl |
( |
size_t |
freq, |
|
|
size_t |
bucketSz |
|
) |
| |
|
inline |
Initialize this object.
- Parameters
-
freq | A positive integer denoting frequency |
bucketSz | Max number of tokens the bucket can hold |
- Note
- You can init this object again to change frequency and bucket size.
bool CFreqControl::check |
( |
size_t |
need | ) |
const |
|
inline |
Check if there are enough tokens.
- Parameters
-
need | Number of tokens needed |
- Returns
true
if there are at least need
tokens in bucket; false
otherwise
void CFreqControl::generate |
( |
uint64_t |
nowUs = 0 | ) |
|
|
inline |
Generate tokens.
- Parameters
-
nowUs | Current monotonic time, obtained from tools::MonoTimeUs(), can be omitted |
bool CFreqControl::get |
( |
size_t |
need = 1 | ) |
|
|
inline |
Get tokens.
- Parameters
-
need | Number of tokens needed |
- Returns
true:
Succeeded. need
tokens are removed from bucket;
false:
Failed. No tokens are removed from bucket;
void CFreqControl::init |
( |
size_t |
freq, |
|
|
size_t |
bucketSz |
|
) |
| |
|
inline |
Initialize this object.
- Parameters
-
freq | A positive integer denoting frequency |
bucketSz | Max number of tokens the bucket can hold |
- Note
- You can init this object again to change frequency and bucket size.
bool CFreqControl::overdraw |
( |
size_t |
need | ) |
|
|
inline |
Overdraw tokens.
- Parameters
-
need | Number of tokens needed |
- Returns
true:
Succeeded. need
tokens are removed or overdrawn from bucket;
false:
Failed. No tokens are removed from bucket;
ssize_t CFreqControl::token |
( |
| ) |
const |
|
inline |
Get number of tokens in bucket.
This function may return a negative number when tokens have been overdrawn.
- Returns
- Positive number: Number of tokens in bucket;
- Negative number: Number of tokens overdrawn;
- See also
- overdraw
The documentation for this struct was generated from the following file: