Add AppConfig cache.

This commit is contained in:
levlam 2023-02-15 23:33:30 +03:00
parent 1064fb2d6a
commit c8c8b63e2b
3 changed files with 55 additions and 10 deletions

View File

@ -95,7 +95,7 @@ class SaveAppLogLogEvent {
void parse(ParserT &parser) {
auto buffer = parser.template fetch_string_raw<BufferSlice>(parser.get_left_len());
TlBufferParser buffer_parser{&buffer};
input_app_event_out_ = telegram_api::make_object<telegram_api::inputAppEvent>(buffer_parser);
input_app_event_out_ = telegram_api::inputAppEvent::fetch(buffer_parser);
}
};

View File

@ -904,8 +904,30 @@ class ConfigRecoverer final : public Actor {
}
};
template <class StorerT>
void ConfigManager::AppConfig::store(StorerT &storer) const {
td::store(version_, storer);
td::store(hash_, storer);
config_->store(storer);
}
template <class ParserT>
void ConfigManager::AppConfig::parse(ParserT &parser) {
td::parse(version_, parser);
if (version_ != CURRENT_VERSION) {
version_ = 0;
return parser.set_error("Invalid config version");
}
td::parse(hash_, parser);
auto buffer = parser.template fetch_string_raw<BufferSlice>(parser.get_left_len());
TlBufferParser buffer_parser{&buffer};
config_ = telegram_api::JSONValue::fetch(buffer_parser);
}
ConfigManager::ConfigManager(ActorShared<> parent) : parent_(std::move(parent)) {
lazy_request_flood_control_.add_limit(20, 1);
log_event_parse(app_config_, G()->td_db()->get_binlog_pmc()->get("app_config")).ignore();
}
void ConfigManager::start_up() {
@ -994,7 +1016,7 @@ void ConfigManager::try_request_app_config() {
return;
}
auto query = G()->net_query_creator().create_unauth(telegram_api::help_getAppConfig(0));
auto query = G()->net_query_creator().create_unauth(telegram_api::help_getAppConfig(app_config_.hash_));
query->total_timeout_limit_ = 60 * 60 * 24;
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), actor_shared(this, 1));
}
@ -1266,15 +1288,23 @@ void ConfigManager::on_result(NetQueryPtr res) {
auto app_config_ptr = result_ptr.move_as_ok();
if (app_config_ptr->get_id() == telegram_api::help_appConfigNotModified::ID) {
fail_promises(promises, Status::Error(500, "Receive unexpected response"));
fail_promises(unit_promises, Status::Error(500, "Receive unexpected response"));
return;
if (app_config_.version_ == 0) {
LOG(ERROR) << "Receive appConfigNotModified";
fail_promises(promises, Status::Error(500, "Receive unexpected response"));
fail_promises(unit_promises, Status::Error(500, "Receive unexpected response"));
return;
}
} else {
CHECK(app_config_ptr->get_id() == telegram_api::help_appConfig::ID);
auto app_config = telegram_api::move_object_as<telegram_api::help_appConfig>(app_config_ptr);
process_app_config(app_config->config_);
app_config_.version_ = AppConfig::CURRENT_VERSION;
app_config_.hash_ = app_config->hash_;
app_config_.config_ = std::move(app_config->config_);
G()->td_db()->get_binlog_pmc()->set("app_config", log_event_store(app_config_).as_slice().str());
}
CHECK(app_config_ptr->get_id() == telegram_api::help_appConfig::ID);
auto app_config = telegram_api::move_object_as<telegram_api::help_appConfig>(app_config_ptr);
process_app_config(app_config->config_);
for (auto &promise : promises) {
promise.set_value(convert_json_value_object(app_config->config_));
promise.set_value(convert_json_value_object(app_config_.config_));
}
set_promises(unit_promises);
return;

View File

@ -105,6 +105,19 @@ class ConfigManager final : public NetQueryCallback {
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:
struct AppConfig {
static constexpr int32 CURRENT_VERSION = 1;
int32 version_ = 0;
int32 hash_ = 0;
telegram_api::object_ptr<telegram_api::JSONValue> config_;
template <class StorerT>
void store(StorerT &storer) const;
template <class ParserT>
void parse(ParserT &parser);
};
ActorShared<> parent_;
int32 config_sent_cnt_{0};
bool reopen_sessions_after_get_config_{false};
@ -129,6 +142,8 @@ class ConfigManager final : public NetQueryCallback {
bool is_set_archive_and_mute_request_sent_ = false;
bool last_set_archive_and_mute_ = false;
AppConfig app_config_;
vector<SuggestedAction> suggested_actions_;
size_t dismiss_suggested_action_request_count_ = 0;
std::map<int32, vector<Promise<Unit>>> dismiss_suggested_action_queries_;
@ -148,7 +163,7 @@ class ConfigManager final : public NetQueryCallback {
void try_request_app_config();
void process_app_config(tl_object_ptr<telegram_api::JSONValue> &config);
void process_app_config(telegram_api::object_ptr<telegram_api::JSONValue> &config);
void do_set_ignore_sensitive_content_restrictions(bool ignore_sensitive_content_restrictions);