From 5cbf90e4a081567593247bd96adc4eec2e9156d8 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 21 Nov 2020 16:34:39 +0300 Subject: [PATCH] Safer work with negative monotonic time. --- td/telegram/AnimationsManager.cpp | 7 +++++-- td/telegram/AnimationsManager.h | 1 + td/telegram/InlineQueriesManager.cpp | 1 + td/telegram/InlineQueriesManager.h | 2 +- td/telegram/PasswordManager.cpp | 8 +++++--- td/telegram/PasswordManager.h | 2 +- td/telegram/QueryCombiner.cpp | 4 ++++ td/telegram/QueryCombiner.h | 6 ++---- td/telegram/StickersManager.cpp | 20 ++++++++++---------- td/telegram/StickersManager.h | 8 ++++---- tdnet/td/net/GetHostByNameActor.cpp | 2 +- 11 files changed, 35 insertions(+), 26 deletions(-) diff --git a/td/telegram/AnimationsManager.cpp b/td/telegram/AnimationsManager.cpp index e756d8bf8..c42e94856 100644 --- a/td/telegram/AnimationsManager.cpp +++ b/td/telegram/AnimationsManager.cpp @@ -138,6 +138,7 @@ AnimationsManager::AnimationsManager(Td *td, ActorShared<> parent) : td_(td), pa LOG(ERROR) << "Wrong saved animations limit = \"" << limit_string << "\" stored in database"; } } + next_saved_animations_load_time_ = Time::now(); } void AnimationsManager::tear_down() { @@ -523,10 +524,10 @@ void AnimationsManager::reload_saved_animations(bool force) { return; } - if (!td_->auth_manager_->is_bot() && next_saved_animations_load_time_ >= 0 && + if (!td_->auth_manager_->is_bot() && !are_saved_animations_being_loaded_ && (next_saved_animations_load_time_ < Time::now() || force)) { LOG_IF(INFO, force) << "Reload saved animations"; - next_saved_animations_load_time_ = -1; + are_saved_animations_being_loaded_ = true; td_->create_handler()->send(false, get_saved_animations_hash("reload_saved_animations")); } } @@ -613,6 +614,7 @@ void AnimationsManager::on_get_saved_animations( bool is_repair, tl_object_ptr &&saved_animations_ptr) { CHECK(!td_->auth_manager_->is_bot()); if (!is_repair) { + are_saved_animations_being_loaded_ = false; next_saved_animations_load_time_ = Time::now_cached() + Random::fast(30 * 60, 50 * 60); } @@ -667,6 +669,7 @@ void AnimationsManager::on_get_saved_animations( void AnimationsManager::on_get_saved_animations_failed(bool is_repair, Status error) { CHECK(error.is_error()); if (!is_repair) { + are_saved_animations_being_loaded_ = false; next_saved_animations_load_time_ = Time::now_cached() + Random::fast(5, 10); } auto &queries = is_repair ? repair_saved_animations_queries_ : load_saved_animations_queries_; diff --git a/td/telegram/AnimationsManager.h b/td/telegram/AnimationsManager.h index b52c2c0b7..9701eb861 100644 --- a/td/telegram/AnimationsManager.h +++ b/td/telegram/AnimationsManager.h @@ -151,6 +151,7 @@ class AnimationsManager : public Actor { vector saved_animation_ids_; vector saved_animation_file_ids_; double next_saved_animations_load_time_ = 0; + bool are_saved_animations_being_loaded_ = false; bool are_saved_animations_loaded_ = false; vector> load_saved_animations_queries_; vector> repair_saved_animations_queries_; diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index 989ce5356..d2d60bc49 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -163,6 +163,7 @@ class SetInlineBotResultsQuery : public Td::ResultHandler { InlineQueriesManager::InlineQueriesManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { drop_inline_query_result_timeout_.set_callback(on_drop_inline_query_result_timeout_callback); drop_inline_query_result_timeout_.set_callback_data(static_cast(this)); + next_inline_query_time_ = Time::now(); } void InlineQueriesManager::tear_down() { diff --git a/td/telegram/InlineQueriesManager.h b/td/telegram/InlineQueriesManager.h index 33c5baa45..b7d39e884 100644 --- a/td/telegram/InlineQueriesManager.h +++ b/td/telegram/InlineQueriesManager.h @@ -126,7 +126,7 @@ class InlineQueriesManager : public Actor { Promise promise; }; - double next_inline_query_time_ = -1.0; + double next_inline_query_time_ = 0.0; unique_ptr pending_inline_query_; NetQueryRef sent_query_; diff --git a/td/telegram/PasswordManager.cpp b/td/telegram/PasswordManager.cpp index 1d0a732be..7010ee6dc 100644 --- a/td/telegram/PasswordManager.cpp +++ b/td/telegram/PasswordManager.cpp @@ -724,8 +724,8 @@ void PasswordManager::cache_secret(secure_storage::Secret secret) { secret_ = std::move(secret); const int32 max_cache_time = 3600; - secret_expire_date_ = Time::now() + max_cache_time; - set_timeout_at(secret_expire_date_); + secret_expire_time_ = Time::now() + max_cache_time; + set_timeout_at(secret_expire_time_); } void PasswordManager::drop_cached_secret() { @@ -734,8 +734,10 @@ void PasswordManager::drop_cached_secret() { } void PasswordManager::timeout_expired() { - if (Time::now() >= secret_expire_date_) { + if (Time::now() >= secret_expire_time_) { drop_cached_secret(); + } else { + set_timeout_at(secret_expire_time_); } } diff --git a/td/telegram/PasswordManager.h b/td/telegram/PasswordManager.h index 74625f2f6..a57881ee5 100644 --- a/td/telegram/PasswordManager.h +++ b/td/telegram/PasswordManager.h @@ -151,7 +151,7 @@ class PasswordManager : public NetQueryCallback { }; optional secret_; - double secret_expire_date_ = 0; + double secret_expire_time_ = 0; TempPasswordState temp_password_state_; Promise create_temp_password_promise_; diff --git a/td/telegram/QueryCombiner.cpp b/td/telegram/QueryCombiner.cpp index 8e799d5ce..e4e29f20a 100644 --- a/td/telegram/QueryCombiner.cpp +++ b/td/telegram/QueryCombiner.cpp @@ -13,6 +13,10 @@ namespace td { +QueryCombiner::QueryCombiner(Slice name, double min_delay) : next_query_time_(Time::now()), min_delay_(min_delay) { + register_actor(name, this).release(); +} + void QueryCombiner::add_query(int64 query_id, Promise> &&send_query, Promise &&promise) { LOG(INFO) << "Add query " << query_id; auto &query = queries_[query_id]; diff --git a/td/telegram/QueryCombiner.h b/td/telegram/QueryCombiner.h index 79933cde6..76fb926bf 100644 --- a/td/telegram/QueryCombiner.h +++ b/td/telegram/QueryCombiner.h @@ -21,9 +21,7 @@ namespace td { // combines identical queries into one request class QueryCombiner : public Actor { public: - explicit QueryCombiner(Slice name, double min_delay = 0) : min_delay_(min_delay) { - register_actor(name, this).release(); - } + QueryCombiner(Slice name, double min_delay); void add_query(int64 query_id, Promise> &&send_query, Promise &&promise); @@ -36,7 +34,7 @@ class QueryCombiner : public Actor { int32 query_count_ = 0; - double next_query_time_ = 0.0; + double next_query_time_; double min_delay_; std::queue delayed_queries_; diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 73ec2dd59..0092b1b58 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -1860,9 +1860,9 @@ void StickersManager::reload_installed_sticker_sets(bool is_masks, bool force) { } auto &next_load_time = next_installed_sticker_sets_load_time_[is_masks]; - if (!td_->auth_manager_->is_bot() && next_load_time >= 0 && (next_load_time < Time::now() || force)) { + if (!td_->auth_manager_->is_bot() && next_load_time >= -9e9 && (next_load_time < Time::now() || force)) { LOG_IF(INFO, force) << "Reload sticker sets"; - next_load_time = -1; + next_load_time = -1e10; td_->create_handler()->send(is_masks, installed_sticker_sets_hash_[is_masks]); } } @@ -1872,10 +1872,10 @@ void StickersManager::reload_featured_sticker_sets(bool force) { return; } - if (!td_->auth_manager_->is_bot() && next_featured_sticker_sets_load_time_ >= 0 && - (next_featured_sticker_sets_load_time_ < Time::now() || force)) { + auto &next_load_time = next_featured_sticker_sets_load_time_; + if (!td_->auth_manager_->is_bot() && next_load_time >= -9e9 && (next_load_time < Time::now() || force)) { LOG_IF(INFO, force) << "Reload trending sticker sets"; - next_featured_sticker_sets_load_time_ = -1; + next_load_time = -1e10; td_->create_handler()->send(featured_sticker_sets_hash_); } } @@ -4985,9 +4985,9 @@ void StickersManager::reload_recent_stickers(bool is_attached, bool force) { } auto &next_load_time = next_recent_stickers_load_time_[is_attached]; - if (!td_->auth_manager_->is_bot() && next_load_time >= 0 && (next_load_time < Time::now() || force)) { + if (!td_->auth_manager_->is_bot() && next_load_time >= -9e9 && (next_load_time < Time::now() || force)) { LOG_IF(INFO, force) << "Reload recent " << (is_attached ? "attached " : "") << "stickers"; - next_load_time = -1; + next_load_time = -1e10; td_->create_handler()->send(false, is_attached, recent_stickers_hash_[is_attached]); } } @@ -5405,10 +5405,10 @@ void StickersManager::reload_favorite_stickers(bool force) { return; } - if (!td_->auth_manager_->is_bot() && next_favorite_stickers_load_time_ >= 0 && - (next_favorite_stickers_load_time_ < Time::now() || force)) { + auto &next_load_time = next_favorite_stickers_load_time_; + if (!td_->auth_manager_->is_bot() && next_load_time >= -9e9 && (next_load_time < Time::now() || force)) { LOG_IF(INFO, force) << "Reload favorite stickers"; - next_favorite_stickers_load_time_ = -1; + next_load_time = -1e10; td_->create_handler()->send(false, get_favorite_stickers_hash()); } } diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index dc018fca7..0726fff5b 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -630,10 +630,10 @@ class StickersManager : public Actor { vector recent_sticker_ids_[2]; vector favorite_sticker_ids_; - double next_installed_sticker_sets_load_time_[2] = {0, 0}; - double next_featured_sticker_sets_load_time_ = 0; - double next_recent_stickers_load_time_[2] = {0, 0}; - double next_favorite_stickers_load_time_ = 0; + double next_installed_sticker_sets_load_time_[2] = {-1e10, -1e10}; + double next_featured_sticker_sets_load_time_ = -1e10; + double next_recent_stickers_load_time_[2] = {-1e10, -1e10}; + double next_favorite_stickers_load_time_ = -1e10; int32 installed_sticker_sets_hash_[2] = {0, 0}; int32 featured_sticker_sets_hash_ = 0; diff --git a/tdnet/td/net/GetHostByNameActor.cpp b/tdnet/td/net/GetHostByNameActor.cpp index 7ac5bac4a..884da3ddf 100644 --- a/tdnet/td/net/GetHostByNameActor.cpp +++ b/tdnet/td/net/GetHostByNameActor.cpp @@ -130,8 +130,8 @@ void GetHostByNameActor::run(string host, int port, bool prefer_ipv6, Promisesecond; auto begin_time = Time::now(); + auto &value = cache_[prefer_ipv6].emplace(ascii_host, Value{{}, begin_time - 1.0}).first->second; if (value.expires_at > begin_time) { return promise.set_result(value.get_ip_port(port)); }