Fix FloodControlStrict.

GitOrigin-RevId: 664841f3e58cce5bce3ad90ef42f23537dc07c16
This commit is contained in:
levlam 2020-07-16 22:36:59 +03:00
parent 2a5c826111
commit 3f2e269162
4 changed files with 17 additions and 15 deletions

View File

@ -925,6 +925,8 @@ void ConfigManager::request_config() {
if (config_sent_cnt_ != 0) { if (config_sent_cnt_ != 0) {
return; return;
} }
lazy_request_flood_countrol_.add_event(static_cast<int32>(Timestamp::now().at()));
request_config_from_dc_impl(DcId::main()); 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) { void ConfigManager::request_config_from_dc_impl(DcId dc_id) {
config_sent_cnt_++; config_sent_cnt_++;
lazy_request_flood_countrol_.add_event(static_cast<int32>(Timestamp::now().at()));
auto query = G()->net_query_creator().create_unauth(telegram_api::help_getConfig(), dc_id); auto query = G()->net_query_creator().create_unauth(telegram_api::help_getConfig(), dc_id);
query->total_timeout_limit_ = 60 * 60 * 24; query->total_timeout_limit_ = 60 * 60 * 24;
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, 0)); G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, 0));

View File

@ -6,8 +6,8 @@
// //
#include "td/telegram/net/DcOptionsSet.h" #include "td/telegram/net/DcOptionsSet.h"
#include "td/telegram/Global.h"
#include "td/telegram/ConfigManager.h" #include "td/telegram/ConfigManager.h"
#include "td/telegram/Global.h"
#include "td/utils/format.h" #include "td/utils/format.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"

View File

@ -13,16 +13,16 @@ namespace td {
class FloodControlFast { class FloodControlFast {
public: public:
uint32 add_event(int32 now) { void add_event(int32 now) {
for (auto &limit : limits_) { for (auto &limit : limits_) {
limit.stat_.add_event(CounterStat::Event(), now); limit.stat_.add_event(CounterStat::Event(), now);
if (limit.stat_.get_stat(now).count_ > limit.count_) { if (limit.stat_.get_stat(now).count_ > limit.count_) {
wakeup_at_ = max(wakeup_at_, now + limit.duration_ * 2); wakeup_at_ = max(wakeup_at_, now + limit.duration_ * 2);
} }
} }
return wakeup_at_;
} }
uint32 get_wakeup_at() {
uint32 get_wakeup_at() const {
return wakeup_at_; return wakeup_at_;
} }

View File

@ -16,22 +16,23 @@ namespace td {
// Should be just fine for small counters. // Should be just fine for small counters.
class FloodControlStrict { class FloodControlStrict {
public: 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}); events_.push_back(Event{now});
if (without_update_ > 0) { if (without_update_ > 0) {
without_update_--; without_update_--;
} else { } else {
update(now); update(now);
} }
return wakeup_at_;
} }
// no more than count in each duration. // no more than count in each duration
void add_limit(int32 duration, int32 count) { void add_limit(int32 duration, size_t count) {
limits_.push_back(Limit{duration, count, 0}); limits_.push_back(Limit{duration, count, 0});
without_update_ = 0;
} }
int32 get_wakeup_at() { int32 get_wakeup_at() const {
return wakeup_at_; return wakeup_at_;
} }
@ -41,7 +42,7 @@ class FloodControlStrict {
limit.pos_ = 0; limit.pos_ = 0;
} }
without_update_ = 0; without_update_ = 0;
wakeup_at_ = 0; wakeup_at_ = 1;
} }
int32 update(int32 now) { int32 update(int32 now) {
@ -49,7 +50,7 @@ class FloodControlStrict {
without_update_ = std::numeric_limits<size_t>::max(); without_update_ = std::numeric_limits<size_t>::max();
for (auto &limit : limits_) { for (auto &limit : limits_) {
if (limit.pos_ + limit.count_ < events_.size()) { if (limit.count_ < events_.size() - limit.pos_) {
limit.pos_ = events_.size() - limit.count_; limit.pos_ = events_.size() - limit.count_;
} }
@ -63,7 +64,7 @@ class FloodControlStrict {
wakeup_at_ = max(wakeup_at_, events_[limit.pos_].timestamp_ + limit.duration_); wakeup_at_ = max(wakeup_at_, events_[limit.pos_].timestamp_ + limit.duration_);
without_update_ = 0; without_update_ = 0;
} else { } 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_); min_pos = min(min_pos, limit.pos_);
@ -79,13 +80,13 @@ class FloodControlStrict {
} }
private: private:
int32 wakeup_at_ = 0; int32 wakeup_at_ = 1;
struct Event { struct Event {
int32 timestamp_; int32 timestamp_;
}; };
struct Limit { struct Limit {
int32 duration_; int32 duration_;
int32 count_; size_t count_;
size_t pos_; size_t pos_;
}; };
size_t without_update_ = 0; size_t without_update_ = 0;