Use double timestamps in FloodControlStrict.

This commit is contained in:
levlam 2022-11-21 21:07:56 +03:00
parent f4c1247327
commit 468edb4d61
4 changed files with 17 additions and 15 deletions

View File

@ -955,7 +955,7 @@ void ConfigManager::request_config(bool reopen_sessions) {
return;
}
lazy_request_flood_control_.add_event(static_cast<int32>(Timestamp::now().at()));
lazy_request_flood_control_.add_event(Time::now());
request_config_from_dc_impl(DcId::main(), reopen_sessions);
}

View File

@ -610,7 +610,7 @@ void ConnectionCreator::on_pong(size_t hash) {
void ConnectionCreator::on_mtproto_error(size_t hash) {
auto &client = clients_[hash];
client.hash = hash;
client.mtproto_error_flood_control.add_event(static_cast<int32>(Time::now_cached()));
client.mtproto_error_flood_control.add_event(Time::now_cached());
}
void ConnectionCreator::request_raw_connection(DcId dc_id, bool allow_media_only, bool is_media,
@ -892,12 +892,12 @@ void ConnectionCreator::client_loop(ClientInfo &client) {
wakeup_at = max(client.sanity_flood_control.get_wakeup_at(), wakeup_at);
if (!act_as_if_online) {
wakeup_at = max(wakeup_at, client.backoff.get_wakeup_at());
wakeup_at = max(wakeup_at, static_cast<double>(client.backoff.get_wakeup_at()));
}
if (wakeup_at > Time::now()) {
return client_set_timeout_at(client, wakeup_at);
}
client.sanity_flood_control.add_event(static_cast<int32>(Time::now()));
client.sanity_flood_control.add_event(Time::now());
if (!act_as_if_online) {
client.backoff.add_event(static_cast<int32>(Time::now()));
}
@ -916,7 +916,7 @@ void ConnectionCreator::client_loop(ClientInfo &client) {
}
// Events with failed socket creation are ignored
flood_control.add_event(static_cast<int32>(Time::now()));
flood_control.add_event(Time::now());
auto socket_fd = r_socket_fd.move_as_ok();
IPAddress debug_ip;

View File

@ -61,8 +61,9 @@ void PublicRsaKeyWatchdog::loop() {
if (has_query_) {
return;
}
if (Time::now_cached() < flood_control_.get_wakeup_at()) {
set_timeout_in(flood_control_.get_wakeup_at() - Time::now_cached() + 0.01);
auto now = Time::now();
if (now < flood_control_.get_wakeup_at()) {
set_timeout_at(flood_control_.get_wakeup_at() + 0.01);
return;
}
bool ok = true;
@ -74,7 +75,7 @@ void PublicRsaKeyWatchdog::loop() {
if (ok) {
return;
}
flood_control_.add_event(static_cast<int32>(Time::now_cached()));
flood_control_.add_event(now);
has_query_ = true;
auto query = G()->net_query_creator().create(telegram_api::help_getCdnConfig());
query->total_timeout_limit_ = 60 * 60 * 24;

View File

@ -17,7 +17,7 @@ namespace td {
class FloodControlStrict {
public:
// 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) {
void add_event(double now) {
events_.push_back(Event{now});
if (without_update_ > 0) {
without_update_--;
@ -32,7 +32,7 @@ class FloodControlStrict {
without_update_ = 0;
}
int32 get_wakeup_at() const {
double get_wakeup_at() const {
return wakeup_at_;
}
@ -42,11 +42,11 @@ class FloodControlStrict {
limit.pos_ = 0;
}
without_update_ = 0;
wakeup_at_ = 1;
wakeup_at_ = 0.0;
}
private:
void update(int32 now) {
void update(double now) {
size_t min_pos = events_.size();
without_update_ = std::numeric_limits<size_t>::max();
@ -56,7 +56,8 @@ class FloodControlStrict {
}
// binary-search? :D
while (limit.pos_ < events_.size() && events_[limit.pos_].timestamp_ + limit.duration_ < now) {
auto end_time = now - limit.duration_;
while (limit.pos_ < events_.size() && events_[limit.pos_].timestamp_ < end_time) {
limit.pos_++;
}
@ -79,9 +80,9 @@ class FloodControlStrict {
}
}
int32 wakeup_at_ = 1;
double wakeup_at_ = 0.0;
struct Event {
int32 timestamp_;
double timestamp_;
};
struct Limit {
int32 duration_;