Skip to content

Commit

Permalink
adjust token precision to double to fix rate limit precision (#52)
Browse files Browse the repository at this point in the history
* adjust token precision to double
* Apply suggestions from code review

Co-authored-by: Eli Lindsey <[email protected]>
Co-authored-by: Shannon Weyrick <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2020
1 parent 7add506 commit 77f1072
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions flame/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ int main(int argc, char *argv[])
for (auto i = 0; i < c_count; i++) {
std::shared_ptr<TokenBucket> rl;
if (config->rate_limit()) {
rl = std::make_shared<TokenBucket>(config->rate_limit() / c_count);
rl = std::make_shared<TokenBucket>(config->rate_limit() / static_cast<double>(c_count));
} else if (args["--qps-flow"]) {
rl = std::make_shared<TokenBucket>();
rl_list.push_back(rl);
Expand Down Expand Up @@ -494,7 +494,7 @@ int main(int argc, char *argv[])
std::cout << "query list randomized" << std::endl;
}
if (config->rate_limit()) {
std::cout << "rate limit @ " << config->rate_limit() << " QPS (" << (config->rate_limit() / c_count) <<
std::cout << "rate limit @ " << config->rate_limit() << " QPS (" << config->rate_limit() / static_cast<double>(c_count) <<
" QPS per concurrent sender)" << std::endl;
}
}
Expand Down
8 changes: 4 additions & 4 deletions flame/tokenbucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TokenBucket
{
}

TokenBucket(const uint64_t rate)
TokenBucket(double rate)
: _rate_qps(rate)
, _token_wallet(0)
, _last_fill_ms(0)
Expand All @@ -28,7 +28,7 @@ class TokenBucket
_last_fill_ms = now_ms;
} else if (now_ms > _last_fill_ms) {
auto elapsed_ms = (now_ms - _last_fill_ms).count();
double add = (double)_rate_qps * ((double)elapsed_ms / 1000.0);
double add = _rate_qps * elapsed_ms / 1000.0;
if (_token_wallet + add >= tokens) {
_token_wallet += add;
_last_fill_ms = now_ms;
Expand All @@ -43,8 +43,8 @@ class TokenBucket
}

private:
uint64_t _rate_qps;
uint64_t _token_wallet;
double _rate_qps;
double _token_wallet;
// milliseconds, based on uv_now() http://docs.libuv.org/en/v1.x/loop.html#c.uv_now
uvw::Loop::Time _last_fill_ms;
};

0 comments on commit 77f1072

Please sign in to comment.