Disable the throttle limit if negative

Also, invalid values result in -1 being set
This commit is contained in:
louiz’
2018-08-23 22:18:49 +02:00
parent 6c431b64d0
commit 7d0df9b6dd
7 changed files with 23 additions and 10 deletions
+2 -2
View File
@@ -86,8 +86,8 @@ class Database
struct Address: Column<std::string> { static constexpr auto name = "address_"; };
struct ThrottleLimit: Column<unsigned long int> { static constexpr auto name = "throttlelimit_";
ThrottleLimit(): Column<unsigned long int>(10) {} };
struct ThrottleLimit: Column<long int> { static constexpr auto name = "throttlelimit_";
ThrottleLimit(): Column<long int>(10) {} };
using MucLogLineTable = Table<Id, Uuid, Owner, IrcChanName, IrcServerName, Date, Body, Nick>;
using MucLogLine = MucLogLineTable::RowType;
+1 -1
View File
@@ -1291,7 +1291,7 @@ bool IrcClient::abort_on_invalid_cert() const
}
#endif
std::size_t IrcClient::get_throttle_limit() const
long int IrcClient::get_throttle_limit() const
{
#ifdef USE_DATABASE
return Database::get_irc_server_options(this->bridge.get_bare_jid(), this->hostname).col<Database::ThrottleLimit>();
+1 -1
View File
@@ -402,7 +402,7 @@ private:
*/
Resolver dns_resolver;
TokensBucket tokens_bucket;
std::size_t get_throttle_limit() const;
long int get_throttle_limit() const;
};
+8 -4
View File
@@ -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++;
}
};
+5 -1
View File
@@ -494,7 +494,11 @@ void ConfigureIrcServerStep2(XmppComponent& xmpp_component, AdhocSession& sessio
else if (field->get_tag("var") == "throttle_limit" && value)
{
options.col<Database::ThrottleLimit>() = std::stoul(value->get_inner());
try {
options.col<Database::ThrottleLimit>() = std::stol(value->get_inner());
} catch (const std::logic_error&) {
options.col<Database::ThrottleLimit>() = -1;
}
Bridge* bridge = biboumi_component.find_user_bridge(session.get_owner_jid());
if (bridge)
{