summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2018-08-23 15:21:12 +0200
committerlouiz’ <louiz@louiz.org>2018-08-23 22:18:49 +0200
commit7d0df9b6ddee8db69ea0a511f031f32a4537a749 (patch)
tree86b1105169b6bf7ea5eea88fac3ec75597231d38 /src/utils
parent6c431b64d050a13853ebf3715fb6bf7986c0cff1 (diff)
downloadbiboumi-7d0df9b6ddee8db69ea0a511f031f32a4537a749.tar.gz
biboumi-7d0df9b6ddee8db69ea0a511f031f32a4537a749.tar.bz2
biboumi-7d0df9b6ddee8db69ea0a511f031f32a4537a749.tar.xz
biboumi-7d0df9b6ddee8db69ea0a511f031f32a4537a749.zip
Disable the throttle limit if negative
Also, invalid values result in -1 being set
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/tokens_bucket.hpp12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/utils/tokens_bucket.hpp b/src/utils/tokens_bucket.hpp
index 03af015..2992e21 100644
--- a/src/utils/tokens_bucket.hpp
+++ b/src/utils/tokens_bucket.hpp
@@ -17,7 +17,7 @@
class TokensBucket
{
public:
- TokensBucket(std::size_t max_size, std::chrono::milliseconds fill_duration, std::function<bool()> callback, std::string name):
+ TokensBucket(long int max_size, std::chrono::milliseconds fill_duration, std::function<bool()> callback, std::string name):
limit(max_size),
tokens(limit),
callback(std::move(callback))
@@ -29,6 +29,8 @@ public:
bool use_token()
{
+ if (this->limit < 0)
+ return true;
if (this->tokens > 0)
{
this->tokens--;
@@ -38,19 +40,21 @@ public:
return false;
}
- void set_limit(std::size_t limit)
+ void set_limit(long int limit)
{
this->limit = limit;
}
private:
- std::size_t limit;
+ long int limit;
std::size_t tokens;
std::function<bool()> callback;
void add_token()
{
- if (this->callback() && this->tokens != limit)
+ if (this->limit < 0)
+ return;
+ if (this->callback() && this->tokens != static_cast<decltype(this->tokens)>(this->limit))
this->tokens++;
}
};