From 3f2e2691628f2265fec0043c8868317aec1bbd5e Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 16 Jul 2020 22:36:59 +0300 Subject: [PATCH] Fix FloodControlStrict. GitOrigin-RevId: 664841f3e58cce5bce3ad90ef42f23537dc07c16 --- td/telegram/ConfigManager.cpp | 3 ++- td/telegram/net/DcOptionsSet.cpp | 2 +- tdutils/td/utils/FloodControlFast.h | 6 +++--- tdutils/td/utils/FloodControlStrict.h | 21 +++++++++++---------- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index b8f6234d6..e9acf625d 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -925,6 +925,8 @@ void ConfigManager::request_config() { if (config_sent_cnt_ != 0) { return; } + + lazy_request_flood_countrol_.add_event(static_cast(Timestamp::now().at())); request_config_from_dc_impl(DcId::main()); } @@ -1047,7 +1049,6 @@ void ConfigManager::on_dc_options_update(DcOptions dc_options) { void ConfigManager::request_config_from_dc_impl(DcId dc_id) { config_sent_cnt_++; - lazy_request_flood_countrol_.add_event(static_cast(Timestamp::now().at())); auto query = G()->net_query_creator().create_unauth(telegram_api::help_getConfig(), dc_id); query->total_timeout_limit_ = 60 * 60 * 24; G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, 0)); diff --git a/td/telegram/net/DcOptionsSet.cpp b/td/telegram/net/DcOptionsSet.cpp index 305b43eb3..10f8469dc 100644 --- a/td/telegram/net/DcOptionsSet.cpp +++ b/td/telegram/net/DcOptionsSet.cpp @@ -6,8 +6,8 @@ // #include "td/telegram/net/DcOptionsSet.h" -#include "td/telegram/Global.h" #include "td/telegram/ConfigManager.h" +#include "td/telegram/Global.h" #include "td/utils/format.h" #include "td/utils/logging.h" diff --git a/tdutils/td/utils/FloodControlFast.h b/tdutils/td/utils/FloodControlFast.h index b7ee3c93e..c1538eb2a 100644 --- a/tdutils/td/utils/FloodControlFast.h +++ b/tdutils/td/utils/FloodControlFast.h @@ -13,16 +13,16 @@ namespace td { class FloodControlFast { public: - uint32 add_event(int32 now) { + void add_event(int32 now) { for (auto &limit : limits_) { limit.stat_.add_event(CounterStat::Event(), now); if (limit.stat_.get_stat(now).count_ > limit.count_) { wakeup_at_ = max(wakeup_at_, now + limit.duration_ * 2); } } - return wakeup_at_; } - uint32 get_wakeup_at() { + + uint32 get_wakeup_at() const { return wakeup_at_; } diff --git a/tdutils/td/utils/FloodControlStrict.h b/tdutils/td/utils/FloodControlStrict.h index fe40a2fc7..3dbfa319f 100644 --- a/tdutils/td/utils/FloodControlStrict.h +++ b/tdutils/td/utils/FloodControlStrict.h @@ -16,22 +16,23 @@ namespace td { // Should be just fine for small counters. class FloodControlStrict { public: - int32 add_event(int32 now) { + // there is no reason to return wakeup_at_, because it will be a time before the next allowed event, not current + void add_event(int32 now) { events_.push_back(Event{now}); if (without_update_ > 0) { without_update_--; } else { update(now); } - return wakeup_at_; } - // no more than count in each duration. - void add_limit(int32 duration, int32 count) { + // no more than count in each duration + void add_limit(int32 duration, size_t count) { limits_.push_back(Limit{duration, count, 0}); + without_update_ = 0; } - int32 get_wakeup_at() { + int32 get_wakeup_at() const { return wakeup_at_; } @@ -41,7 +42,7 @@ class FloodControlStrict { limit.pos_ = 0; } without_update_ = 0; - wakeup_at_ = 0; + wakeup_at_ = 1; } int32 update(int32 now) { @@ -49,7 +50,7 @@ class FloodControlStrict { without_update_ = std::numeric_limits::max(); for (auto &limit : limits_) { - if (limit.pos_ + limit.count_ < events_.size()) { + if (limit.count_ < events_.size() - limit.pos_) { limit.pos_ = events_.size() - limit.count_; } @@ -63,7 +64,7 @@ class FloodControlStrict { wakeup_at_ = max(wakeup_at_, events_[limit.pos_].timestamp_ + limit.duration_); without_update_ = 0; } else { - without_update_ = min(without_update_, limit.count_ + limit.pos_ - events_.size()); + without_update_ = min(without_update_, limit.count_ + limit.pos_ - events_.size() - 1); } min_pos = min(min_pos, limit.pos_); @@ -79,13 +80,13 @@ class FloodControlStrict { } private: - int32 wakeup_at_ = 0; + int32 wakeup_at_ = 1; struct Event { int32 timestamp_; }; struct Limit { int32 duration_; - int32 count_; + size_t count_; size_t pos_; }; size_t without_update_ = 0;